minutes

advertisement
S0-6
x^2 – x = 0
x = 7,
…
x=9
S0-9
Insertion sort:
7
5
3
7
3
5
7---------------------|
7
3
7
5
7
List:
[]
a
:
element
x
list
Concepts introduced: constructor, polymorphism
insertsort :: [Integer] -> [Integer]
hastype
insert :: Integer -> [Integer] -> [Integer]
design pattern of functions:
to start with
try to split the problem into two cases [] and (:)
idea of recursion: assuming a sorted list and only worrying about putting the first
element in the right place
there is no know means of solving this problem
so we invent a new vocabulary (insert)
Exercise1:
member :: Integer -> [Integer] -> Bool
Task: define a function
Consume :: [A] -> B
Step 1: Solve the pattern for the empty list []
Consume [] = …
Step 2: assume that you already have the solution for xs, extend the intermediate
solution for x:xs
consume (x:xs) = … x … xs … consume xs …
you only have to program a step!
Program with expressions:
(a==b)==True is the same as (a==b)
(if c then True else d) is the same as (c || d)
Exercise2
smallest :: [Integer] -> Integer
xs
x
smallest (1: 2:[])
xs
x
= min 1 (smallest (2: []))
= min 1 (min 2 (smallest []))
= min 1 (min 2 maxBound)
= min 1 2
=1
S1-7
square x = x*x
square (square 17)
sq 17 * sq 17
17^2*sq 17
sq(17*17)
sq 17*17^2
(17^2)*(17^2)
17^2*17^2
17^4
S1-13
puzzle’ f = f “hello” ++ “world”
q s t = s ++ t
S1-19
7*5*6
mult (7,mult (5,6))
mult (mult (5,6),7)
S1-20
(f.g).h = f.(g.h)
((f.g).h) x
(f.g).h x)
f(g(h x))
=
=
=
S1-26
putStr :: String -> IO()
return :: a -> IO a
(>>=) :: IO a -> (a->IO b)-> IO b
>> :: IO a -> IO b -> IO b
readln :: IO String
(f.(g.h)) x
f((g.h) x)
f(g(h x))
sq 17^2
interaction =
putStr “first name:” >>
readln >>= \s ->
putStr “surname” >>
readln >>= \ t->
putStr (“your name is “ ++s ++t)
interaction =
do {putStr “…”
s <- readln
putStr “…”
t <- readln
putStr (“…”)}
S2-0
types
::
values
compiler time
run time
S3-18
revto [] y = y
revto (a:x) y = revto x (a:y)
a1 : a2 : a3 : a4 : []
foldr (+) e
a1 + a2 + a3 + a4 : e
foldr (+) 0 (fromTo 1 2)
= foldr (+) 0 (1:fromTo 2 2)
= 1 + foldr (+) 0 (fromTo 2 2)
= 1+ foldr (+) 0 (2:fr 3 2)
= 1+ (2+foldr (+) 0 (fr 3 2))
= 1+ (2+foldr (+) 0 [])
= 1+(2+0)
=3
S4-11
type Bool = forall a. a -> a -> a
true a b = a
false a b = b
if b = b
a&&b = if a b false
Thursday
data Nat = Zero | Succ
add :: Nat -> Nat -> Nat
add Zero n = n
add (Succ m) n = Succ (add m n)
data Nat (a) = Zero | Succ (a) (Nat (a))
add becomes append
data Bin = Nil
| Zero Bin
| One Bin
inc :: Bin -> Bin
inc Nil = One Nil
inc (Zero ds) = One ds
inc (One ds) = Zero (inc ds)
inc (One (Zero (One Nil)))
= Zero (inc (Zero (One Nil)))
= One (One (One Nil))
data Bin a = Nil
| Zero (Bin (a,a))
| One a (Bin (a,a))
inc :: a -> Bin a -> Bin a
inc a Nil = One a Nil
inc a (Zero ds) = One a ds
inc a (One b ds) = Zero (inc (a,b) ds)
S5-5
lookup :: Dict a b -> a -> Maybe b
… case lookup german “Has” of
Nothing ->
Just e ->
Friday
data Tree a = Empty
| Leaf a
| Fork (Tree a) (Tree a)
sampletree = Fork (Fork (Leaf 3) (Leaf 2)) (Leaf 1)
gen :: Integer -> Tree Integer
gen 0 = Empty
gen (n+1) = Fork (Leaf n) (gen n)
flatten Empty =
flatten (Leaf a) =
flatten (Fork l r) = flatten l `merge` flatten r
merge :: (Ord a) -> [a] -> [a] -> [a]
merge [] [] = []
merge [] (b:bs) = b: bs
merge (a:as) [] = a:as
merge (a:as) (b:bs) =
| a <= b = a:merge as (b:bs)
| otherwise = b:merge (a:as) bs
single [a] = True
single _ = False
treeify :: [a] -> Tree a
treeify as
| n == 0 = Empty
| n == 1 = Leaf (head as)
| otherwise = Fork (treeify (take m as))
(treeify (drop m as))
where n = length as
m = n`div` 2
mergesort = flatten.treeify
treeify [] = Empty
treeify (a:as) = insert a (treeify as)
treeify = foldr insert Empty
insert :: a -> Tre a -> Tree a
insert a Empty = Leaf a
insert a (Leaf b) = Fork (Leaf a) (Leaf b)
insert a (Fork l r) = Fork r (insert a l)
reverse :: [a] -> [a]
reverse [] = []
reverse (a:as) = reverse as ++ [a]
specification
reverseCat x y = reverse x ++ y
reverseCat [] y
{Spec}
= reverse [] ++ y
{Def. of reverse}
= [] ++ y
{Def. of ++}
= y
=
=
=
=
reverseCat (a:as) y
{Spec}
reverse (a:as) ++ y
{Def. reverse}
(reverse as ++ [a]) ++ y
{associativity of ++}
reverse as ++ ([a] ++ y)
{Spec}
reverseCat as (a:y)
reverseCat [] y = y
reverseCat (a:as) y = reverseCat as (a:y)
reverse x
{as ++ [] = as}
= reverse x ++ []
{Spec}
= reverseCat x []
Download