From edc8320df3178b15613c8ab127ac2cf7b3dbf410 Mon Sep 17 00:00:00 2001 From: Nadia Polikarpova Date: Sun, 29 Oct 2023 16:39:10 -0700 Subject: [PATCH] update page --- assignments.md | 3 +- docs/assignments.html | 10 +-- docs/lectures/03-datatypes.html | 146 +++++++++++++++++--------------- docs/lectures/03-tailrec.html | 8 +- site.hs | 2 +- 5 files changed, 95 insertions(+), 74 deletions(-) diff --git a/assignments.md b/assignments.md index deb2ab6..dcccf50 100644 --- a/assignments.md +++ b/assignments.md @@ -26,12 +26,13 @@ If you get an error when creating a team, **try a different team name** - diff --git a/docs/assignments.html b/docs/assignments.html index 1137b61..0d08cc5 100644 --- a/docs/assignments.html +++ b/docs/assignments.html @@ -274,12 +274,12 @@

Assignments

| [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

A. 500

B. Type error

+

Answer: A




@@ -441,6 +442,7 @@

QUIZ

C. MkCircle :: Double -> Double -> Circle

D. MkCircle :: Double -> Double -> Double -> Circle

E. MkCircle :: (Double, Double, Double) -> Circle

+

Answer: D




@@ -486,6 +488,7 @@

QUIZ

>>> volume (MkCuboid 10 20 30)

A. 6000

B. Type error

+

Answer: B




@@ -628,6 +631,7 @@

QUIZ

B. Circle

C. MkCircle

D. (Double, Double, Double)

+

Answer: A




@@ -793,6 +797,7 @@

QUIZ

C. (Int, IntList)

D. Int -> IntList -> IntList

E. IntList -> IntList

+

Answer: D




@@ -877,6 +882,9 @@

EXERCISE: Append




+
append :: List -> List -> List
+append Nil ys = ys
+append (Cons x xs) ys = Cons x (append xs ys)    




@@ -934,6 +942,7 @@

QUIZ: Binary trees

(D) data ITree = ILeaf Int | INode ITree ITree

(E) data ITree = ILeaf Int | INode Int ITree ITree


+

Answer: C




@@ -947,12 +956,12 @@

QUIZ: Binary trees

Binary tree -
-- | Binary tree of integers
-data ITree = ILeaf | INode Int ITree ITree
-
-t1234 = INode 1 
-          (INode 2 (INode 3 ILeaf ILeaf) ILeaf) 
-          (INode 4 ILeaf ILeaf)
+
-- | Binary tree of integers
+data ITree = ILeaf | INode Int ITree ITree
+
+t1234 = INode 1 
+          (INode 2 (INode 3 ILeaf ILeaf) ILeaf) 
+          (INode 4 ILeaf ILeaf)




@@ -961,8 +970,9 @@

QUIZ: Binary trees



Functions on trees

-
height :: Tree -> Int
-height t = ??
+
height :: Tree -> Int
+height Leaf = 0
+height (Node _ l r) = 1 + max (height l) (height r)




@@ -979,7 +989,7 @@

Example: Calculator

  • (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 @@

    Example: Calculator




    -
    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 @@

    Example: Calculator


    EXERCISE: Eval

    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
    +eval (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




    @@ -1034,13 +1047,13 @@

    Algebraic Data Types

    Parameterized Types

    Our IntList datatype can only store Ints :-(

    What if we want to store Chars or Doubles?

    -
    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 @@

    Don’t Repeat Yourself!


    A Refactored List

    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 Refactored List



    Refactored using Type Parameter

    -
    -- | 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' 
    -iList = Cons 1 (Cons 2 (Cons 3 Nil))
    -
    -cList :: List Char     -- list where 'a' = 'Char' 
    -cList = Cons 'a' (Cons 'b' (Cons 'c' Nil))
    -
    -dList :: List Double   -- list where 'a' = 'Double' 
    -dList = Cons 1.1 (Cons 2.2 (Cons 3.3 Nil))
    +
    iList :: List Int      -- list where 'a' = 'Int' 
    +iList = Cons 1 (Cons 2 (Cons 3 Nil))
    +
    +cList :: List Char     -- list where 'a' = 'Char' 
    +cList = Cons 'a' (Cons 'b' (Cons 'c' Nil))
    +
    +dList :: List Double   -- list where 'a' = 'Double' 
    +dList = Cons 1.1 (Cons 2.2 (Cons 3.3 Nil))




    @@ -1110,13 +1123,14 @@

    Refactored using Type Parameter



    QUIZ

    -
    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 @@

    QUIZ


    Polymorphic Data has Polymorphic Constructors

    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 @@

    Polymorphic Data has Poly

    Polymorphic Functions

    Let us refactor the append function to work on Lists:

    -
    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 @@

    Polymorphic Functions


    Polymorphic Functions over Polymorphic Data

    The append function on Lists is polymorphic:

    -
    append :: List a -> List a -> List a
    -append Nil ys         = ys
    -append (Cons x xs) ys = Cons x (append xs ys)
    +
    append :: List a -> List a -> List a
    +append Nil ys         = ys
    +append (Cons x xs) ys = Cons x (append xs ys)

    append doesn’t care about the actual values in the list

    -
    >>> 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 @@

    Polymorphic Functions over

    Built-in Lists?

    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)

    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 @@

    QUIZ: Is this function tail recurs

    A. Yes

    B. No


    +

    Answer: B




    @@ -335,7 +336,12 @@

    QUIZ: Is this function tail recurs

    Tail recursive factorial

    Let’s write a tail-recursive factorial!

    facTR :: Int -> Int
    -facTR n = ... 
    +facTR n = loop 1 n + where + loop :: Int -> Int -> Int + loop acc n + | n <= 1 = acc + | otherwise = loop (acc * n) (n - 1)




    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