Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Type annotation (optional, same for each implementation)
- factorial :: (Integral a) => a -> a
- -- Using recursion (with the "ifthenelse" expression)
- factorial n = if n < 2
- then 1
- else n * factorial (n - 1)
- -- Using recursion (with pattern matching)
- factorial 0 = 1
- factorial n = n * factorial (n - 1)
- -- Using recursion (with guards)
- factorial n
- | n < 2 = 1
- | otherwise = n * factorial (n - 1)
- -- Using a list and the "product" function
- factorial n = product [1..n]
- -- Using fold (implements "product")
- factorial n = foldl (*) 1 [1..n]
- -- Point-free style
- factorial = foldr (*) 1 . enumFromTo 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement