views:

306

answers:

3

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
[1,3,5,15]

and

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

posInt 5
[5]

posInt 0
[]
+3  A: 
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
gradbot
That infinite series example is not that infinite...
Martinho Fernandes
Well, Seq.initInfinite returns int32 as the type for the constructor.
gradbot
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.
Martinho Fernandes
In F# and Haskell the default type of int is int32. I simply made a bounded version.
gradbot
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
+3  A: 

gradbot is right. Faithfully converting posInt would look like:

let posInt n = [if n > 0 then yield n]
kvb
This returns a list of a single element n if it is greater than zero.
gradbot
+3  A: 

See the answer to

http://stackoverflow.com/questions/865044/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)]

)

Brian