Take the 2-minute tour ×

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]

factors 15


posInt     ::   Int -> [Int]
posInt     =    [n | n > 0]

posInt 5

posInt 0
share|improve this question
Could be of use. stackoverflow.com/questions/1888451/list-comprehension-in-f –  gradbot Dec 17 '09 at 17:41
add comment

3 Answers

up vote 3 down vote accepted

gradbot is right. Faithfully converting posInt would look like:

let posInt n = [if n > 0 then yield n]
share|improve this answer
This returns a list of a single element n if it is greater than zero. –  gradbot Dec 17 '09 at 18:23
add comment

See the answer to


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)]


share|improve this answer
add comment
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.


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
share|improve this answer
That infinite series example is not that infinite... –  R. Martinho Fernandes Dec 17 '09 at 17:53
Well, Seq.initInfinite returns int32 as the type for the constructor. –  gradbot Dec 17 '09 at 18:01
The fact that int32 is bounded doesn't mean you can't make an infinite list : seq { while true do yield 0 } is an infinite list of 0s. –  R. Martinho Fernandes Dec 17 '09 at 18:03
In F# and Haskell the default type of int is int32. I simply made a bounded version. –  gradbot Dec 17 '09 at 18:22
for the posInt example I am not looking to create an infinite list. Just a singleton list, where the list is empty if a number <= 0 is given, else a list of a single int. The answer provided by kvb best fits what I was looking for on this example. –  TonyAbell Dec 17 '09 at 18:47
add comment

Your Answer


