Haskell Cheatsheet
47 essential methods for coding interviews
Showing 47 of 47 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
map | map :: (a -> b) -> [a] -> [b] | Apply a function to every element in a list | O(n) | essential |
filter | filter :: (a -> Bool) -> [a] -> [a] | Keep only elements that satisfy a predicate | O(n) | essential |
foldl | foldl :: (b -> a -> b) -> b -> [a] -> b | Left fold - reduce list from left with accumulator | O(n) | essential |
foldr | foldr :: (a -> b -> b) -> b -> [a] -> b | Right fold - reduce list from right with accumulator | O(n) | essential |
head | head :: [a] -> a | Get the first element of a list | O(1) | essential |
tail | tail :: [a] -> [a] | Get all elements except the first | O(1) | essential |
take | take :: Int -> [a] -> [a] | Take first n elements from a list | O(n) | essential |
drop | drop :: Int -> [a] -> [a] | Drop first n elements from a list | O(n) | essential |
zip | zip :: [a] -> [b] -> [(a, b)] | Combine two lists into list of pairs | O(min(n,m)) | essential |
zipWith | zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] | Combine two lists using a function | O(min(n,m)) | essential |
length | length :: [a] -> Int | Get the number of elements in a list | O(n) | essential |
reverse | reverse :: [a] -> [a] | Reverse a list | O(n) | essential |
(++) | (++) :: [a] -> [a] -> [a] | Concatenate two lists | O(n) | essential |
(:) | (:) :: a -> [a] -> [a] | Prepend element to list (cons) | O(1) | essential |
elem | elem :: Eq a => a -> [a] -> Bool | Check if element exists in list | O(n) | essential |
null | null :: [a] -> Bool | Check if list is empty | O(1) | essential |
words | words :: String -> [String] | Split string into words by whitespace | O(n) | essential |
lines | lines :: String -> [String] | Split string into lines | O(n) | essential |
show | show :: Show a => a -> String | Convert value to string representation | O(n) | essential |
read | read :: Read a => String -> a | Parse string to value | O(n) | essential |
maybe | maybe :: b -> (a -> b) -> Maybe a -> b | Extract value from Maybe with default and transform | O(1) | essential |
fromMaybe | fromMaybe :: a -> Maybe a -> a | Extract value from Maybe with default | O(1) | essential |
sum | sum :: Num a => [a] -> a | Sum all elements in list | O(n) | essential |
maximum | maximum :: Ord a => [a] -> a | Find largest element | O(n) | essential |
minimum | minimum :: Ord a => [a] -> a | Find smallest element | O(n) | essential |
div | div :: Integral a => a -> a -> a | Integer division (floor) | O(1) | essential |
mod | mod :: Integral a => a -> a -> a | Modulo operation | O(1) | essential |
sort | sort :: Ord a => [a] -> [a] | Sort list in ascending order | O(n log n) | essential |
(.) | (.) :: (b -> c) -> (a -> b) -> a -> c | Function composition | O(1) | essential |
($) | ($) :: (a -> b) -> a -> b | Function application (low precedence) | O(1) | essential |
any | any :: (a -> Bool) -> [a] -> Bool | Check if any element satisfies predicate | O(n) | essential |
all | all :: (a -> Bool) -> [a] -> Bool | Check if all elements satisfy predicate | O(n) | essential |
concat | concat :: [[a]] -> [a] | Flatten a list of lists into a single list | O(n) | common |
concatMap | concatMap :: (a -> [b]) -> [a] -> [b] | Map a function and concatenate results (flatMap) | O(n*m) | common |
takeWhile | takeWhile :: (a -> Bool) -> [a] -> [a] | Take elements while predicate is true | O(n) | common |
replicate | replicate :: Int -> a -> [a] | Create list with n copies of element | O(n) | common |
intercalate | intercalate :: [a] -> [[a]] -> [a] | Join lists with separator | O(n) | common |
catMaybes | catMaybes :: [Maybe a] -> [a] | Extract all Just values from list | O(n) | common |
mapMaybe | mapMaybe :: (a -> Maybe b) -> [a] -> [b] | Map function and keep only Just results | O(n) | common |
either | either :: (a -> c) -> (b -> c) -> Either a b -> c | Apply function based on Either value | O(1) | common |
sortBy | sortBy :: (a -> a -> Ordering) -> [a] -> [a] | Sort with custom comparison function | O(n log n) | common |
flip | flip :: (a -> b -> c) -> b -> a -> c | Swap function arguments | O(1) | common |
id | id :: a -> a | Identity function - returns its argument | O(1) | common |
lookup | lookup :: Eq a => a -> [(a, b)] -> Maybe b | Find value by key in association list | O(n) | common |
nub | nub :: Eq a => [a] -> [a] | Remove duplicate elements | O(n^2) | common |
group | group :: Eq a => [a] -> [[a]] | Group consecutive equal elements | O(n) | common |
partition | partition :: (a -> Bool) -> [a] -> ([a], [a]) | Split list by predicate into (pass, fail) | O(n) | common |