0

I have this code:

divisors n = 1:[y|y<-[2..(n `div` 2)], n `mod` y == 0]

writeList l = do print "Start"
                 print l

Then, i want to call the function with strict argument; i tried:

writeList $! (divisors 12345678)

and

(divisors 12345678) \`seq\` (writeList (divisors 12345678))

But it does not behave as if it has strict argument: i.e. after the "Start" I have to wait for the evaluation of (divisors 12345678)

What am I missing?

Aslan986
  • 9,984
  • 11
  • 44
  • 75
  • 4
    You are missing the difference between whnf and nf. – Thomas M. DuBuisson Sep 12 '12 at 20:06
  • 1
    Also, using `seq` (or `deepseq`) like that will just throw away the result of evaluating the first argument. You need to introduce explicit sharing, e.g. ```let ds = divisors 12345678 in ds `seq` writeList ds```. – hammar Sep 12 '12 at 20:20
  • @hamar: thank you for your help. Is `writeList $!! (divisors 12345678)` correct? – Aslan986 Sep 12 '12 at 20:25

1 Answers1

1

seq(or $!) evalueates only for first head normal form, a (:) constructor in your case, you can use deepseq(or $!!) for evaluating to normal form.

Fedor Gogolev
  • 10,391
  • 4
  • 30
  • 36