Feb 19, 2008

Conditional branches of Haskell.

I'm studying Haskell.

Haskell has some conditional statements.

1. pattern matching

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


2. guard

fact n | n==0 = 1
| otherwise = n * fact (n-1)


3. if

fact n = if n==0 then 1
else n * fact (n-1)


4. case

fact n = case n of
0 -> 1
_ -> n * fact (n-1)

No comments: