[ACCEPTED]-list comprehension in F#-list-comprehension
Accepted answer
Nested for loops require a
do
.You need to 9 use
seq {..}
. The form{..}
withoutseq
doesn't work anymore.A 8
when
guard in afor
loop pattern is also not supported 7 anymore.print_any something
is deprecated. Useprintf "%A" something
instead.
This 6 code should work:
let evens n =
seq { for x in 1 .. n do if x%2=0 then yield x }
printf "%A" (evens 10)
let squarePoints n =
seq { for x in 1 .. n do
for y in 1 .. n -> x,y }
printf "%A" (squarePoints 3)
You can still use the ->
if 5 all you want to do is return a single value:
let vec1 = [1;2;3]
let vec2 = [4;5;6]
let products = [for x in vec1 do for y in vec2 -> x*y]
By 4 the way, I find it interesting to see how 3 F# evolved over time. Too bad the early 2 adopters have partially outdated books on 1 their shelves (not that I mind).
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.