If you want to see the tutorial we were working with checkout:
https://github.com/abramhindle/hands-on-haskell
https://github.com/abramhindle/hands-on-haskell/blob/master/tutorial.lhs
Haskell Tutorial (C) 2013 Abram Hindle CC-BY 3.0 For Edmonton Functional Programmers Users Group
Hi this is a literate program
Code is written with > in front of it. This is called bird style.
How do we write a function with args?
How do we call mul?
Or
type in :type mul
*Main> :type mul mul :: Num a => a -> a -> a
a is of typeclass Num (numbers)
and mul is a function that takes an a, takes an a, and produce an a
OR mul takes a Num a and produces a function that takes a Num a and produces a Num a
*Main> :type double double :: Integer -> Integer
So we made a 1 argument function by partially providing all the arguments to mul. This is currying! It allows for easy function composition, maybe it is a little crazy but you see it too much in haskell to ignore.
So we can make functions and it seems we have numbers, but we can make our own types as well.
But we can't print it until we say we can show it using "deriving Show"
We can make functions with these datatypes too
This isn't 2 functions, this is pattern matching.
*Main> :t opposite opposite :: Dirs -> Dirs
So lets imagine we have a robot and it can face N E S W and it can turn left or right
We can declare a new datatype
We add a bunch of typeclasses Eq for equality Show for printing Ord for Ordinal (they are in order) Enum of enumeration which means we can use succ (successor) and pred (predecessor)
Typeclasses are like inheritenance on data types.
We can write a new function
So now we've got data types and pattern matching.
Now let's cover some data types real quick
We can use these types in other types like say lists of integers.
odds :: [Integer] <--- list of Integers
How do we navigate lists or use them?
What about iteration?
What about mapping?
(* 2) ? :t (* 2) (* 2) :: Num a => a -> a
This is overcomplicated
Writing better functions
Note order doesn't matter!
Where lets you define stuff out of order let implies an order
Recursion is easy
infinite!
No comments:
Post a Comment