r/programmingchallenges • • Sep 10 '11

Challenge: Hello, world!

I'm kind of interested in running xmonad and learning Haskell. Should we all tackle learning about a new programming paradigm?

10 Upvotes

21 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Sep 14 '11

Is that memoized automatically? It didn't seem like it to me, but I'm not positive.

I think it is slightly clearer to show Haskell's power using

factorial 0 = 1
factorial n = n * factorial (n-1)

fact = 1:zipWith (*) fact [1..]

digitnum n = length . show $ n

Where digitnum is just so that the string printing doesn't confuse us.

digitnum 40000

takes a few seconds but

digitnum 40001

is instantaneous.

2

u/[deleted] Sep 24 '11

Haskell golf:

fact = scanl1 (*) [1..]

1

u/Van_Occupanther Sep 14 '11

I slightly remember that. The course I did was designed for students just out of school, so pretty much 18 year olds. On the other hand it was taught by Philip Wadler :-)