Skip to content

Instantly share code, notes, and snippets.

@d0minicw0ng
Last active August 29, 2015 14:04
Show Gist options
  • Save d0minicw0ng/550880f419807486f2f1 to your computer and use it in GitHub Desktop.
Save d0minicw0ng/550880f419807486f2f1 to your computer and use it in GitHub Desktop.
Recusions in Haskell
maximum' :: (Ord a) => [a] -> a
maximum' [] = error "maximum of empty list"
maximum' [x] = x
maximum' (x:xs)
| x > maxTail = x
| otherwise = maxTail
where maxTail = maximum' xs
-- using max
-- maximum' (x:xs) = max x (maximum' xs)
replicate' :: (Num i, Ord i) => i -> a -> [a]
replicate' n x
| n <= 0 = []
| otherwise = x:replicate' (n-1) x
take' :: (Num i, Ord i) => i -> [a] -> [a]
take' n _
| n <= 0 = []
take' _ [] = []
take' n (x:xs) = x : take' (n-1) xs
reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment