From abcd8d7f7fb5090f18036f9e3abe145dd76e749f Mon Sep 17 00:00:00 2001 From: Nadia Polikarpova Date: Mon, 30 Oct 2023 08:47:13 -0700 Subject: [PATCH] update page --- docs/lectures/03-datatypes.html | 40 ++++++++++++++++++++++++--------- lectures/03-datatypes.md | 38 +++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/docs/lectures/03-datatypes.html b/docs/lectures/03-datatypes.html index 458c810..a01af79 100644 --- a/docs/lectures/03-datatypes.html +++ b/docs/lectures/03-datatypes.html @@ -1200,10 +1200,30 @@

Polymorphic Functions over 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 = ? -??? +Cons 'a' (Cons 'b' (Cons 'c' Nil)) +


+
+
+
+
+
+
+
+
+
+
+
+
+

+

QUIZ

+

With the type of append defined as

+

append :: List a -> List a -> List a

+

what would be result of

+
append (Cons 'a' (Cons 'b' Nil)) (Cons True Nil)
+

A. Cons 'a' (Cons 'b' (Cons True Nil))

+

B. Cons 'a' (Cons 'b' Nil)

+

C. Type error

+

Answer: C




@@ -1220,18 +1240,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/lectures/03-datatypes.md b/lectures/03-datatypes.md index e56c511..3b94e53 100644 --- a/lectures/03-datatypes.md +++ b/lectures/03-datatypes.md @@ -1416,11 +1416,45 @@ 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 = ? -??? +
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +## QUIZ + +With the type of `append` defined as + +`append :: List a -> List a -> List a` + +what would be result of + +```haskell +append (Cons 'a' (Cons 'b' Nil)) (Cons True Nil) ``` +**A.** `Cons 'a' (Cons 'b' (Cons True Nil))` + +**B.** `Cons 'a' (Cons 'b' Nil)` + +**C.** Type error + +(I) final + + *Answer:* C +