Variables and functions
ghci
:quit
:load <file>
variables
<name> = <value>
r = 5
Use
let var = <value>
in ghci.Variables are immutable and can’t be define twice.
Variables can be defined in any order because of immutable.
comment
--
{- ... -}
functions
<name> <parameters…> =
1 | area r = pi * r ^ 2 |
- Parentheses are omit.
1 | area 5 * 3 -- (area 5) * 3 area (5 * 3) -- area 15 |
Functions take precedence over all other operators
where
: define function-local variables
1 | heron a b c = sqrt (s * (s - a) * (s - b) * (s - c)) |
Truth values
Comparions
>\<\>=\<=
equal:
==
not equal:
/=
Boolean
Bool:
True/False
operations
and:
(&&)
or:
(||)
not
Infix operators
- Haskell allows two-argument functions to be written as infix operators placed between their arguments.
1 | 4 + 9 == 13 --True |
Guards
1 | absolute x |
catch-all gurad:
otherwise = True
where
1 | numOfRealSolutions a b c |
Type basics
:type
in ghci
Strings and chars
'h' :: Char
"hello" :: [Char]
"hello" ++ "world"
Function types
not :: Bool -> Bool
chr :: Int -> Char
ord :: Char -> Int
conversion char to its numeric form in :module Data.Char
xor :: Bool -\> Bool -\> Bool
1 | xor p q = (p || q) && not(p && q)` |
Type annotations
1 | uppercase, lowercase :: String -> String |
Lists and tuples
Lists
[1, 2, 3]
Elements in a list must have the same type.
consing:
<newEl>:<list>
Can only cons(
:
) something onto a list, not onto an element.<array> ++ <array>
Tuples
Tuples have a fixed number of elements.
Elements in tuple can have different types.
pairs(2-tuples)
triples(3-tuples)
Retrieving values
fst :: (a, b) -> a
snd :: (a, b) -> b
head :: [a] -> a
Calling this with a empty list will case a error, use less.
a
is a type variable.
tail :: [a] -> a
Type basics II
Num class
(+) :: (Num a) => a -> a -> a
Num is a type class: a group of types which includes all number types.
(Num a) =>
restricts a to a number types (instances of Num).
Numeric types
Int
Integer
supports arbitrarily large values — at the cost of some efficiency
Double/Float
Monomorphic trouble
1 | (/) :: (Fractional a) => a -> a -> a |
Other classes
Foldable
: [] ‘s upper classEq
: the class of types of values can be compared for equality.
1 | (==) :: (Eq a) => a -> a -> Bool |
Next Steps
if/then/else
1 | mySignum x = |
- Haskell requires both a then and an else clause.
Pattern matching
1 | pts :: Int -> Int |
The argument is match against the patterns on the left side.
_
is the wildcard meaning whatever.The named parameter is like
_
, the only difference is it have a name which can be used.
1 | -- example: (||) |
Tuple and list patterns
1 | fst' :: (a, b) -> a |
1 | head :: [a] -> a |
let binding
1 | roots a b c = |
Building voabulary
Composing functions with (.)
.
1 | squareOfF x = (square . f) x -- or squareOfF = square . f |
Libraries
Prelude
the core library loaded by default in every Haskell program.
Import modules
import Data.List
testPermutations = permutations "Prelude"
:m +Data.List
Simple input and output
Type
printLn :: String -> IO ()
IO ()
an action which returns
()
unit type:
()
do: an suger for >>=
1 | main = do |
do assume the block return a action.
<-
can’t be used in the last lineif
‘s two branches have the same type
1 | if condition |
- let bindings in do block can omit
in
1 | main = do |
return :: Monad m => a -> m a
turn a non-action return into action
Can omit the do keyword if there’s only one IO action following it.