From edc8320df3178b15613c8ab127ac2cf7b3dbf410 Mon Sep 17 00:00:00 2001
From: Nadia Polikarpova A. B. Type error Answer: A C. D. E. Answer: D A. B. Type error Answer: B B. C. D. Answer: A C. D. E. Answer: DAssignments
| [HW4](https://classroom.github.com/a/mSIQkN07) | Nano | 11/22 |
| [HW5](https://classroom.github.com/a/7B-CwVM8) | Type Classes | 12/6 |
-->
+Practice Exams
+
+
diff --git a/docs/lectures/03-datatypes.html b/docs/lectures/03-datatypes.html
index b00c37f..458c810 100644
--- a/docs/lectures/03-datatypes.html
+++ b/docs/lectures/03-datatypes.html
@@ -362,6 +362,7 @@ QUIZ
>>> volume circ0
500
@@ -441,6 +442,7 @@ QUIZ
MkCircle :: Double -> Double -> Circle
MkCircle :: Double -> Double -> Double -> Circle
MkCircle :: (Double, Double, Double) -> Circle
@@ -486,6 +488,7 @@ QUIZ
>>> volume (MkCuboid 10 20 30)
6000
@@ -628,6 +631,7 @@ QUIZ
Circle
MkCircle
(Double, Double, Double)
@@ -793,6 +797,7 @@ QUIZ
(Int, IntList)
Int -> IntList -> IntList
IntList -> IntList
@@ -877,6 +882,9 @@ EXERCISE: Append
append :: List -> List -> List
+Nil ys = ys
+ append Cons x xs) ys = Cons x (append xs ys) append (
@@ -934,6 +942,7 @@
(D) data ITree = ILeaf Int | INode ITree ITree
(E) data ITree = ILeaf Int | INode Int ITree ITree
Answer: C
@@ -947,12 +956,12 @@
-- | Binary tree of integers
-data ITree = ILeaf | INode Int ITree ITree
-
-= INode 1
- t1234 INode 2 (INode 3 ILeaf ILeaf) ILeaf)
- (INode 4 ILeaf ILeaf) (
-- | Binary tree of integers
+data ITree = ILeaf | INode Int ITree ITree
+
+= INode 1
+ t1234 INode 2 (INode 3 ILeaf ILeaf) ILeaf)
+ (INode 4 ILeaf ILeaf) (
@@ -961,8 +970,9 @@
height :: Tree -> Int
-= ?? height t
height :: Tree -> Int
+Leaf = 0
+ height Node _ l r) = 1 + max (height l) (height r) height (
@@ -979,7 +989,7 @@
(4.0 + 2.9) * (3.78 - 5.92)
What is a Haskell datatype to represent these expressions?
-data Expr = ???
data Expr = ???
@@ -988,10 +998,10 @@
data Expr = Num Float
-| Add Expr Expr
- | Sub Expr Expr
- | Mul Expr Expr
data Expr = Num Float
+| Add Expr Expr
+ | Sub Expr Expr
+ | Mul Expr Expr
@@ -1002,13 +1012,16 @@
With expressions defined as follows:
-data Expr = Num Float
-| Add Expr Expr
- | Sub Expr Expr
- | Mul Expr Expr
data Expr = Num Float
+| Add Expr Expr
+ | Sub Expr Expr
+ | Mul Expr Expr
write a function to evaluate an expression
-eval :: Expr -> Float
-= ??? eval e
eval :: Expr -> Float
+Num f) = f
+ eval (Add e1 e2) = eval e1 + eval e2
+ eval (Sub e1 e2) = eval e1 - eval e2
+ eval (Mul e1 e2) = eval e1 * eval e2 eval (
@@ -1034,13 +1047,13 @@
Our IntList
datatype can only store Int
s :-(
What if we want to store Char
s or Double
s?
data CharList
-= CNil
- | CCons Char CharList
-
-data DoubleList
-= DNil
- | DCons Char DoubleList
data CharList
+= CNil
+ | CCons Char CharList
+
+data DoubleList
+= DNil
+ | DCons Char DoubleList
@@ -1068,11 +1081,11 @@
Here are the three types: What is common? What is different?
-data IList = INil | ICons Int IList
-
-data CList = CNil | CCons Char CList
-
-data DList = DNil | DCons Double DList
data IList = INil | ICons Int IList
+
+data CList = CNil | CCons Char CList
+
+data DList = DNil | DCons Double DList
@@ -1084,17 +1097,17 @@
-- | A list of elements of type `a`
-data List a = Nil | Cons a (List a)
-- | A list of elements of type `a`
+data List a = Nil | Cons a (List a)
We can recover original types as instances of List
:
iList :: List Int -- list where 'a' = 'Int'
-= Cons 1 (Cons 2 (Cons 3 Nil))
- iList
-cList :: List Char -- list where 'a' = 'Char'
-= Cons 'a' (Cons 'b' (Cons 'c' Nil))
- cList
-dList :: List Double -- list where 'a' = 'Double'
-= Cons 1.1 (Cons 2.2 (Cons 3.3 Nil)) dList
iList :: List Int -- list where 'a' = 'Int'
+= Cons 1 (Cons 2 (Cons 3 Nil))
+ iList
+cList :: List Char -- list where 'a' = 'Char'
+= Cons 'a' (Cons 'b' (Cons 'c' Nil))
+ cList
+dList :: List Double -- list where 'a' = 'Double'
+= Cons 1.1 (Cons 2.2 (Cons 3.3 Nil)) dList
@@ -1110,13 +1123,14 @@
data List a = Nil | Cons a (List a)
data List a = Nil | Cons a (List a)
What is the type of Cons
?
A. Int -> List -> List
B. Int -> List Int -> List Int
C. Char -> List Char -> List Char
D. a -> List -> List
E. a -> List a -> List a
Answer: E
@@ -1129,11 +1143,11 @@
Look at the types of the constructors
->>> :type Nil
-Nil :: List a
>>> :type Nil
+Nil :: List a
That is, Nil
is a value of any kind of list, and
>>> :type Cons
-Cons :: a -> List a -> List a
>>> :type Cons
+Cons :: a -> List a -> List a
Cons
takes an a
and a List a
and returns a List a
.
@@ -1151,9 +1165,9 @@
Let us refactor the append
function to work on List
s:
data List a = Nil | Cons a (List a)
append :: ??? -- What is the type of `append`?
-= ??? append
data List a = Nil | Cons a (List a)
append :: ??? -- What is the type of `append`?
+= ??? append
@@ -1170,9 +1184,9 @@
The append
function on List
s is polymorphic:
append :: List a -> List a -> List a
-Nil ys = ys
- append Cons x xs) ys = Cons x (append xs ys) append (
append :: List a -> List a -> List a
+Nil ys = ys
+ append Cons x xs) ys = Cons x (append xs ys) append (
append
doesn’t care about the actual values in the list
append
on lists of any kind>>> append (Cons 1 (Cons 2 Nil)) (Cons 3 Nil) -- a = Int
-Cons 1 (Cons 2 (Cons 3 Nil))
-
->>> append (Cons 'a' (Cons 'b' Nil)) (Cons 'c' Nil) -- a = Char
-Cons 'a' (Cons 'b' (Cons 'c' Nil))
-
->>> append (Cons 1.1 (Cons 2.2 Nil)) (Cons 'a' Nil) -- a = ?
-???
>>> append (Cons 1 (Cons 2 Nil)) (Cons 3 Nil) -- a = Int
+Cons 1 (Cons 2 (Cons 3 Nil))
+
+>>> append (Cons 'a' (Cons 'b' Nil)) (Cons 'c' Nil) -- a = Char
+Cons 'a' (Cons 'b' (Cons 'c' Nil))
+
+>>> append (Cons 1.1 (Cons 2.2 Nil)) (Cons 'a' Nil) -- a = ?
+???
@@ -1206,18 +1220,18 @@
This is exactly how Haskell’s “built-in” lists are defined:
-data [a] = [] | (:) a [a]
-
-data List a = Nil | Cons a (List a)
data [a] = [] | (:) a [a]
+
+data List a = Nil | Cons a (List a)
Nil
is called []
Cons
is called :
Many list manipulating functions e.g. in Data.List
are polymorphic
- Can be reused across all kinds of lists.
(++) :: [a] -> [a] -> [a]
-head :: [a] -> a
-tail :: [a] -> [a]
(++) :: [a] -> [a] -> [a]
+head :: [a] -> a
+tail :: [a] -> [a]
diff --git a/docs/lectures/03-tailrec.html b/docs/lectures/03-tailrec.html
index 3a65226..0cc5503 100644
--- a/docs/lectures/03-tailrec.html
+++ b/docs/lectures/03-tailrec.html
@@ -325,6 +325,7 @@
A. Yes
B. No
Answer: B
@@ -335,7 +336,12 @@
Let’s write a tail-recursive factorial!
facTR :: Int -> Int
-= ... facTR n
diff --git a/site.hs b/site.hs
index 1cff2e4..81aff93 100644
--- a/site.hs
+++ b/site.hs
@@ -53,7 +53,7 @@ main = hakyll $ do
match "lectures/00-*" $ crunchWithCtxCustom "final" postCtx
match "lectures/01-*" $ crunchWithCtxCustom "final" postCtx
match "lectures/02-*" $ crunchWithCtxCustom "final" postCtx
- match "lectures/03-*" $ crunchWithCtxCustom "lecture" postCtx
+ match "lectures/03-*" $ crunchWithCtxCustom "final" postCtx
match "lectures/04-*" $ crunchWithCtxCustom "lecture" postCtx
-- match "lectures/05-*" $ crunchWithCtxCustom "final" postCtx
-- match "lectures/06-*" $ crunchWithCtxCustom "final" postCtx