f# - How to make this code more compact and idiomatic? -


Hello all.

I am a C # programmer, in my spare time, F # I have written the following small programs for image conversion in 2D.

  Open System Let xx = y; & Gt; List.map (fun-ye -> x |> List.map ((*) you)). & Gt; List IPI (Function l -> [1.7] in [q.i -> 0] @ l @ [q for 1. (L.Length - i - 1 - -> gt; 0]). & Gt; List.reduce (fun r -> List.zip r c;> List.map (fun (a, b) -> a + b) y = [2; 3; 1; 4] Let x = [4; 1; 2; 3] printfn "% A" (convolve y x)  

My question is: Is the above code a idiomatic F #? Can it be made more compact? (I.e. there is a small way to prepare a list of 0 (for this purpose I have used the list comprehension in my code)). Any change that can improve its performance?

Any help would be greatly appreciated. Thank you.

Edit:

Thank you Brian I did not find your first suggestion. My code appears after applying your second suggestion. (I also explain the inventory-operation operation.)

  How to fill the list of open systems list = How to do this with = [for 1. i. In. Many - & gt; With what] let y x = y | & Gt; List.map (fun-ye -> x |> List.map ((*) you)). & Gt; List.mapi (fun i l -> (Fill list i 0) @ l @ (Fill list (l.Length - i - 1) 0)). & Gt; List.reduce (List.map2 (+)) y = [2; 3; 1; 4] Let x = [4; 1; 2; 3] printfn "% a" (convolve y x)  

Anything else can be improved? Waiting for more suggestions ...

As Brian mentioned, @ < / Code> is generally problematic, because the operator can not be efficiently implemented for functional lists - it needs to be copied the whole first list

I think Brian suggested a The sequence generator was to write which would generate the list at a time, but this little bit And more complex. You have to convert the array to the list and then write something:

  let xx = y; & Gt; List.map (Interesting -> gt;> List.map ((*) You) |> Array.ofList |> List.map (Interesting IL -> Arrayind (2 * l.Length - 1) (fun n -> if n  i- n - i> gt = l. lenthith then 0 and l. [N - i])) | | List.reduce (Array.map2 ( +)) In general, if performance is a matter of significant concern, then you will probably need to use arrays in some way (because this kind of problem can be solved by reaching the elements by the best index) arrays use A little bit more difficult (come To correct the indexing), but the exact approach to F #.  

However, if you want to write it using the list, here are some options that you can use sequence expressions everywhere Are, which look like this:

  let y (x: _ list) = [i, v1 in x |> List.zip [0 .. x.langith - 1] Fill in the i0 for Y2 in the list! -> Fill in the list! (X. Long-i-1) 0]] | | List.reduce (List.map2 (+ ))  

... or you want two Cards can also be folded and nested sequence expression ( yield! to generate zero and lists in the lambda function) that you are going to List.mapi :

  yx = y | & Gt; List.map (fun-ye -> x |> List.map ((*) you)). & Gt; List. Measurements (funny i l -> [for _ _ in 1] .. I generate to generate in i .. (el lalenth-i-1) generate 0]). & Gt; List.reduce (List.map2 (+))  

Comments