Kihagyás

5. előadás

fact :: Integral a, Integral b => a -> b
fact n = product [1..n]

Tail recursion

fact' :: Integer -> Integer
fact' n = factH n 1
    where factH :: Integer -> Integer -> Integer
          factH 0 c = c
          factH n c = factH (n-1) (n*c)
length' :: [a] -> Int
length' arr = lengthH arr 0

lengthH :: [a] -> Int -> int
lengthH [] c = c
lengthH (_:xs) c = lengthH xs (c+1)

Currying

incBy4 :: Integer -> Integer
incBy4 n = (+) 4 n

Ez ugyan az, mint

incBy4 :: Integer -> Integer
incBy4 = (+) 4