F12, then click Console
F12, then click Console
Data looks like
Lists and Tuples are useful data structures
Everything in Haskell has a TypeHere are some Type declarations.
These are your greatest weapons.
head :: [a] -> a -- gets the first element of a list tail :: [a] -> [a] -- gets everything but the first element last :: [a] -> a -- gets the last element of a list init :: [a] -> [a] -- gets everything but the last element (++) :: [a] -> [a] -> [a] -- concatenates two lists together (:) :: a -> [a] -> [a] -- prepends an element to a list fst :: (a,b) -> a -- gets the first element of a tuple snd :: (a,b) -> b -- gets the second element of a tuple
-- This example uses 'succ' to get next letter cipher :: String -> Int-> String cipher "" n = "" cipher str n = rotate (head str) n : cipher (tail str) n rotate :: Char -> Int -> Char rotate c 0 = c rotate c n = rotate (next c) (n-1) next :: Char -> Char next c = if c=='z' then 'a' else succ c
A function can have multiple patterns
Almost like overloading methods in Java or C++
guess :: Int -> [Char] guess 42 = "correct!" guess x = "wrong guess!"
Each pattern has the same type declaration
Patterns are matched in order, top-down
Only the first matched pattern is evaluated
The patterns must exhaust the entire domain
fib :: Int -> Int fib n = fib(n-1) + fib(n-2) fib 0 = 1 fib 1 = 1
The base case is never hit.
The first pattern eats up everything!∞ loop
You can even match lists using Construct
head (firstItem : everythingElse) = firstItem
tail (x:xs) = xs
Write a function to detect if a list is a palindrome
(clear answer) (show answer)
isPal :: Eq a => [a] -> Bool
We can define
fst with pattern matching
fst :: (a,b) -> a fst (x,y) = x
We can specify when a value is unused.
The "_" symbol is called a wildcard in Haskell.
This is how it's used:
head (x:_) = x tail (_:xs) = xs
When GHCi is angry, it produces error messages through the
error :: [Char] -> a
The official implementation of
head :: [a] -> a head (x:_) = x head  = error "Prelude.head: empty list"
Guards are clean
Just like with pattern matching, order matters.
A guard is introduced by the
And it's followed by a Bool expression.
Then followed by the function body
guessMyNumber x | x > 27 = "Too high!" | x < 27 = "Too low!" | otherwise = "Correct!"
otherwiseis just a fancy word for
Guards are very powerful.
Anything done with pattern matching can be done with guards.
head' :: [a] -> a head' xs | null xs = error "list is empty" | otherwise = xs !! 0
!!is a function that gives an element at an index
These are not like your typical Java variables
In Java or C++, you can redefine variables:
x = 1; ... x = 2;
Mathematically, this makes no sense.
Haskell variables are immutable.
Once defined, they can't change.
They can be used with the
slope (x1,y1) (x2,y2) = let dy = y2-y1 dx = x2-x1 in dy/dx
Or with the
slope (x1,y1) (x2,y2) = dy/dx where dy = y2-y1 dx = x2-x1
In Haskell, indentation matters.
Code which is part of some expression should be indented further in than the beginning of that expression*
Don't use tab. Use spaces '
Level-1 Level-2 Level-3 Level-3 Level-3 Level-2 Level-3 Level-3 Level-1 Level-2 Level-2 Level-1
Convert between metric and imperial.
convert :: (Double, [Char]) -> (Double, [Char])
Prelude> convert (1, "m") (1.09361, "yd") Prelude> convert (1, "L") (0.264172, "gal") Prelude> convert (1, "kg") (2.20462, "lb")