haskell:
Concatenate two lists: {{c1:: LIST1 ++ LIST2
}}
Prepend an element to a list: {{c1:: ELEMENT:LIST
}}
Get an element from a list by index: {{c1:: LIST !! INDEX
}}
Get the first element of a list: {{c1:: head LIST
}}
Get all but the first element of a list: {{c1:: tail LIST
}}
Get the last element element of a list: {{c1:: last LIST
}}
Get all but the last element of a list: {{c1:: init LIST
}}
Get the length of a list: {{c1:: length LIST
}}
Check that a list is empty: {{c1:: null LIST
}}
Check that a list contains an element: {{c1:: ELEMENT `elem` LIST
}}
haskell: Get a list from a range of an enumerable: {{c1:: [FIRST..LAST]
}}
haskell: Build a list comprehension:
{{c1:: [OUTPUT_FUNCTION | INPUT_SET[, ...][, PREDICATE, ...]
]}}
haskell: Using list comprehensions, build a list of odd integers under 10:
{{c1:: [x | x <- [1..10], odd x]
]}}
haskell:
Extract the first element of a pair tuple: {{c1:: fst TUPLE
}}
Extract the second element of a pair tuple: {{c1:: snd TUPLE
}}
haskell: Create a list of tuples for each pair of elements in two lists: {{c1:: zip LIST1 LIST2
}}
haskell: ghci: Show the type of an expression: {{c1:: :t EXPRESSION
}}
cs: {{c1::A function that can be applied to values of different types}} is a {{c2::polymorphic function}}
haskell: {{c1::=>}} is called a {{c2::Class Constraint}}
haskell: Define the factorial function with Pattern Matching:
{{c1::
factorial :: (Integral a) => a -> a
factorial 0 = 1
factorial n = n * factorial (n - 1)
}}
haskell: Define the max function with Function Guards:
{{c1::
max' :: (Ord a) => a -> a -> a
max' a b
| a > b = a
| otherwise = b
}}
haskell: {{c1::Define or use prefix functions with infix notation}} by {{c2::enclosing the function in backticks}}
Examples:
{{c2::
myCompare :: (Ord a) => a -> a -> Ordering
a `myCompare` b
| a > b = GT
| a == b = EQ
| otherwise = LT
ghci> 3 `myCompare` 2 GT
}}
haskell: Easily bind variables for functions bodies and guards with {{c1::where}}
haskell: Define let bindings: {{c1:: let BINDINGS in EXPRESSION
}}
haskell: Define a case expression:
{{c1::
case EXPRESSION of PATTERN -> RESULT
PATTERN -> RESULT
[...]
}}
cs: {{c1::Currying}} is {{c2::the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument}}
haskell: {{c1::Partially apply or use infix functions with prefix notation}} by {{c2::enclosing the function in parentheses}}
Examples:
divideByTen = {{c2::(/10)}}
plus = {{c2::(+)}}
cs: {{c1::A fold}} is {{c2::a function that processes a data-structure to build a return value}}
haskell: {{c1::Fold a list with the first or last element as the accumulator}} with {{c2::foldl1 or foldr1}}
haskell: {{c1::Fold a list while creating a new list with the accumulators}} with {{c2::scanl or scanr}}
haskell: {{c1::Compose two or more functions}} with {{c2::.}}
haskell: Define the Bool type: {{c1:: data Bool = True | False
}}
haskell: In data Bool = True | False
, True
and False
are called {{c1::Value Constructors}}
haskell: Define a Person type with a first and last name and with record syntax:
{{c1::
data Person = Person { firstname :: String
, lastname :: String
} deriving (Show)
}}
haskell: In data Maybe a = Nothing | Just a
, Maybe
is called a {{c1::Type Constructor}}
haskell: Alias a type with {{c1:: type ALIAS = ORIGINAL_TYPE
}}
haskell: By convention, in the Either type, the errors use the {{c1::Left}} value constructor while the results use the {{c1::Right}} value constructor
{{c1::Mnemonic: “right” also means “correct”}}
haskell: Define the Eq typeclass:
{{c1::
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
x == y = not (x /= y)
x /= y = not (x == y)
}}
haskell: Make data TrafficLight = Red | Yellow | Green
an instance of the Eq typeclass:
{{c1::
instance Eq TrafficLight where
Red == Red = True
Green == Green = True
Yellow == Yellow = True
_ == _ = False
}}
haskell: ghci: Get typeclasses’ functions and instances: {{c1:: :i TYPE|TYPECLASS
}}
cs: A Functor is a {{c1::type that can have a function applied to its value without changing the type}}
haskell: A Functor is a type that implements {{c1::fmap}}
haskell: ghci: Get the kind of a type: {{c1:: :k TYPE
}}
haskell: Read and print a string:
{{c1::
main = do
string <- getLine
putStrLn string
}}
haskell: The result of a do block is the {{c1::result of the last action in the block}}
haskell: Bind a monad to a name: {{c1:: NAME <- MONAD
}}
haskell: Inject a value in a monad: {{c1:: return VALUE
}}
haskell: Bind variables in do
contexts:
{{c1::
main = do
let var1 = value
var2 = value
}}
haskell: Transform a list of IO actions in a single IO action: {{c1::sequence [ACTION1, ACTION2]}}
haskell: Map a function over a list and sequence that list: {{c1::mapM FUNCTION LIST}}
haskell: The functor laws are: {{c1::
- fmap id FUNCTOR = id FUNCTOR
- fmap (f . g) FUNCTOR = fmap f (fmap g FUNCTOR)
}}
haskell: An applicative is a type that implements {{c1::pure and <*>}}
haskell: {{c1::Apply a function in an applicative to a value in an applicative}} = {{c2::<*>}}
haskell: {{c1::f <$> x}} = {{c2::fmap f x}}
haskell: {{c1::(,)}} = {{c2::\x y -> (x,y)}}
haskell: {{c1::liftA2 f x y}} = {{c2::(fmap f x) <*> y}}
haskell: A monoid is a type that implements {{c1::mempty, mappend and mconcat}}
haskell: The monoid laws are: {{c1::
- mempty `mappend` x = x
- x `mappend` mempty = x
- (x `mappend` y) `mappend` z = x `mappend` (y `mappend` z)
}}
haskell: {{c1::<>}} = {{c2::mappend}}