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
[]
TonyAbell
  • 1,310
  • 4
  • 19
  • 28

3 Answers3

4
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
  • 13,732
  • 5
  • 36
  • 69
  • Well, Seq.initInfinite returns int32 as the type for the constructor. – gradbot Dec 17 '09 at 18:01
  • 1
    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
  • 2
    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
4

gradbot is right. Faithfully converting posInt would look like:

let posInt n = [if n > 0 then yield n]
kvb
  • 54,864
  • 2
  • 91
  • 133
4

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

)

Community
  • 1
  • 1
Brian
  • 117,631
  • 17
  • 236
  • 300