5. gyakorlat
Gyakorlás - (++), concat, elem, isPrefixOf, nub
isPrefixOf :: (Eq a) => [a] -> [a] -> Bool
isPrefixOf [] _ = True
isPrefixOf _ [] = False
isPrefixOf (a:as) (b:bs) = a == b && isPrefixOf as bs
Őrfeltétel - Guards
fact :: Integer -> Integer
fact n
| n <= 0 = 1
| otherwise = n * fact (n - 1)
fact' :: Integer -> Integer
fact' x | x <= 0 = 0
fact' x = x * fact (x-1)
otherwise = True - literally
insert :: Ord a => a -> [a] -> [a]
insert a [] = [a]
insert a list@(x:xs)
| a <= x = a:list
| otherwise = x:insert a xs
combinations :: Int -> [a] -> [[a]]
combinations 0 _ = [[]]
combinations n _ | n < 0 = []
combinations _ [] = []
combinations n (x:xs) = [x:ys | ys <- combinations (n-1) xs] ++ combinations n xs
Rekurzió: Olyan függvény ami önmaga definícióját használja fel önmaga definiálásához.