Aphorisms in Haskell, using only functions in base and with a particular bend towards showing the use of lists for control flow. Many of these are adapted from here.

Apply a function over a tuple:

both :: (a -> b) -> (a, a) -> (b, b) both = join (***)

Turn a probability density into a cumulative density:

cdf :: (Num a) => [a] -> [a] cdf = drop 2 . (scanl (+) 0) . ((:) 0)

Enumerate all strings on an alphabet:

allStrings :: [a] -> [[a]] allStrings = sequence <=< (inits . repeat)

Repeat every element in a list n times:

repeatList :: Int -> [a] -> [a] repeatList = (=<<) . replicate

Enumerate the Fibonacci numbers:

fibonacci :: (Integral a) => [a] fibonacci = 1 : 1 : zipWith (+) fibonacci (tail fibonacci)

Enumerate all rational numbers:

rationals :: (Integral a) => [Ratio a] rationals = fix ((1%1 :) . (>>= \x -> [1+x, 1/(1+x)]))

Return all subsets of a list:

allSubsets :: [a] -> [[a]] allSubsets = filterM (pure [True, False])

Accept only numbers and Latin letters:

isAlphaNum :: Char -> Bool isAlphaNum = or . sequence [isDigit, isAsciiUpper, isAsciiLower]

Return all pairs of elements on a list:

pairs :: [a] -> [(a, a)] pairs = ((,) <$>) >>= (<*>)

Compute the Cartesian product of two lists:

cartesian :: [a] -> [a] -> [[a]] cartesian = (sequence .) . (\x y -> [x, y])

Type-level naturals:

type Nat = Just ()