What is a way to implement similar functionality in Haskell of List comprehensions with guards in F#
for example:
factors :: Int -> [Int]
factors = [x | x <-[1 .. n], n 'mod' x == 0]
fac开发者_StackOverflow中文版tors 15
[1,3,5,15]
and
posInt :: Int -> [Int]
posInt = [n | n > 0]
posInt 5
[5]
posInt 0
[]
gradbot is right. Faithfully converting posInt
would look like:
let posInt n = [if n > 0 then yield n]
let factors n = [for x in 1 .. n do if n % x = 0 then yield x]
As Kvb showed you can use a guard without a sequence.
let posInt n = [if n > 0 then yield n]
On a side note:
Since list are not lazy in F# you have to use a sequence for an infinite series.
let posInfinite = seq {1 .. Int32.MaxValue}
You can't make an infinite sequence of increasing integers with list comprehension alone. You have to use recursion or another built in function like unfold. .Net does have an arbitrary length integer type called BigInteger. You can use it by just adding an "I
" as the type on an integer. This example will return a true infinite sequence of integers.
let posInfinite = Seq.unfold (fun i -> Some(i, i + 1I)) 1I
See the answer to
how do i translate this Haskell to F#?
that sketches the general way to turn a Haskell list comprehension into F# code.
(Copying here for easy reference:
More generally I think Haskell list comprehensions have the form suggested by the example below, and the corresponding F# is shown.
// Haskell
// [e(x,y) | x <- l1, y <- l2, pred(x,y)]
// F#
[for x in l1 do
for y in l2 do
if pred(x,y) then
yield e(x,y)]
)
精彩评论