:l baby
to loadbaby.hs
:t (+)
to show function type- parentheses required for infix functions
*
is an infix function (because it's punctuation)- so
5 * 3
is parsed exactly as* 5 3
, which is really(* 5) 3
- so
- [1] ++ [3]
- 1:[3]
- homogeneous:
List a
- unlike lists, heterogeneous: (a, b); (a, b, c)
let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
- helpful
- but you should know types
- e.g. search
[a]
to find list functions - search
(a, b)
to find tuple functions - Data.x is a good marker for base data type functions
- e.g. search
http://learnyouahaskell.com/types-and-typeclasses
Eq a
- show/read
- Pattern matching!!! wow
- interestingly, syntax sugar for
case
expressions
- interestingly, syntax sugar for
- Guards
- Can use
_
as fallback to indicate we don't care otherwise
is literally equivalent toTrue
where
to define private sub-functions- but often better as their own function
let...in
, but I barely use that
lucky :: (Integral a) => a -> String
lucky 7 = "LUCKY NUMBER SEVEN!"
lucky x = "Sorry, you're out of luck, pal!"
first :: (a, b, c) -> a
first (x, _, _) = x
-- The (x:xs) makes sense, I promise
-- It's the same as `1:[3]` above
-- Note that `head' [1..]` works! Doesn't spin forever.
head' :: [a] -> a
head' (x:xs) = x
-- case
head'' :: [a] -> a
head'' xs = case xs of [] -> error "No head for empty lists!"
(x:_) -> x
- https://github.com/gabebw/dotfiles/blob/master/ghci
- https://github.com/gabebw/dotfiles/blob/master/haskeline
- Lists: https://exercises.upcase.com/exercises/head-tail-init-last
- Pattern matching exercise: https://exercises.upcase.com/exercises/pattern-matching