From c4c77483f5dc90dd10fc145bac6f238d7ee9ccea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hurlin?= Date: Mon, 11 Dec 2023 10:29:56 +0100 Subject: [PATCH] Small updates --- slides/Course02.hs | 25 +++++++++++++--------- slides/Course03.hs | 6 +++--- slides/check.sh | 2 +- slides/course-02.md | 52 ++++++++++++++++++--------------------------- slides/course-03.md | 6 +++--- slides/course-04.md | 3 ++- 6 files changed, 45 insertions(+), 49 deletions(-) diff --git a/slides/Course02.hs b/slides/Course02.hs index 71c1392..b0e734f 100644 --- a/slides/Course02.hs +++ b/slides/Course02.hs @@ -61,8 +61,8 @@ instance Collection Maybe where size = \case Nothing -> 0; Just _ -> 1 toList = \case Nothing -> []; Just x -> [x] -instance Mappable [] where - map f = \case [] -> []; (x : xs) -> f x : Course02.map f xs +instance Mappable Maybe where + map _f = undefined data Either a b = -- | The left case, commonly used as the error case @@ -76,14 +76,6 @@ readEither :: Read a => String -> Either String a readEither _ = undefined -instance Collection (Either a) where - size _ = 1 - isEmpty _ = False - toList = \case Left _ -> []; Right b -> [b] - -instance Mappable (Either a) where - map f = \case Left a -> Left a; Right b -> Right (f b) - data Account = Account { balance :: Int, email :: String, @@ -105,6 +97,19 @@ mkAccount2 email = Account { .. } -- Take fields from enclosing scope setBalance :: Int -> Account -> Account setBalance n account = account { balance = n } -- functional update +data Interval a = Interval { + start :: a, + end :: a +} + +instance Collection Interval where + size _ = undefined + isEmpty _ = undefined + toList _ = undefined + +instance Mappable Interval where + map _f _ = undefined + class Semigroup a where -- | Associative binary operation (<>) :: a -> a -> a diff --git a/slides/Course03.hs b/slides/Course03.hs index 87c86ac..2850494 100644 --- a/slides/Course03.hs +++ b/slides/Course03.hs @@ -82,9 +82,9 @@ data Account = Account { dubious :: [Account] -> [String] dubious accounts = accounts - & filter (\Account{balance} -> balance > 1000000) - & filter (\Account{email} -> "ponzi" `isInfixOf` email) - & filter (\Account{name} -> case name of Nothing -> True; Just _ -> False) + & filter (\account -> account.balance > 1000000) + & filter (\account -> "ponzi" `isInfixOf` account.email) + & filter (\account -> case account.name of Nothing -> True; Just _ -> False) & map email diff --git a/slides/check.sh b/slides/check.sh index 056688d..03cd9ff 100755 --- a/slides/check.sh +++ b/slides/check.sh @@ -3,7 +3,7 @@ # Continously build the *.hs files extract from # snippets of the *.md files -ls *.md *.cabal | grep course | entr "./build_hs.sh" & +ls *.md | grep course | entr "./extract_hs.sh && cabal build" & declare -r HS_CHECKER_PID="$!" diff --git a/slides/course-02.md b/slides/course-02.md index f26ce4a..72859ef 100644 --- a/slides/course-02.md +++ b/slides/course-02.md @@ -405,8 +405,8 @@ instance Collection Maybe where size = \case Nothing -> 0; Just _ -> 1 toList = \case Nothing -> []; Just x -> [x] -instance Mappable [] where - map f = \case [] -> []; (x : xs) -> f x : Course02.map f xs +instance Mappable Maybe where + map _f = undefined ``` --- @@ -437,35 +437,6 @@ Right 0 Left "Prelude.read: no parse" # Not the best error message ``` --- - -**Cette fin de slide naze, parceque la syntaxe `Collection (Either a)` apparaƮt trop bizarre** - -* Abstracting over `Either` - - -```hs -instance Collection (Either a) where - size _ = undefined - isEmpty _ = undefined - toList _ = undefined - -instance Mappable (Either a) where - map f _ = undefined -``` - -??? - -```hs -instance Collection (Either a) where - size _ = 1 - isEmpty _ = False - toList = \case Left _ -> []; Right b -> [b] - -instance Mappable (Either a) where - map f = \case Left a -> Left a; Right b -> Right (f b) -``` - --- # Types: records @@ -509,6 +480,25 @@ setBalance n account = account { balance = n } -- functional update --- +# Abstracting over a record: `Interval` + +```hs +data Interval a = Interval { + start :: a, + end :: a +} + +instance Collection Interval where + size _ = undefined + isEmpty _ = undefined + toList _ = undefined + +instance Mappable Interval where + map _f _ = undefined +``` + +--- + # Common abstractions ```hs diff --git a/slides/course-03.md b/slides/course-03.md index 27fa1a5..e7be40b 100644 --- a/slides/course-03.md +++ b/slides/course-03.md @@ -316,9 +316,9 @@ data Account = Account { dubious :: [Account] -> [String] dubious accounts = accounts - & filter (\Account{balance} -> balance > 1000000) - & filter (\Account{email} -> "ponzi" `isInfixOf` email) - & filter (\Account{name} -> case name of Nothing -> True; Just _ -> False) + & filter (\account -> account.balance > 1000000) + & filter (\account -> "ponzi" `isInfixOf` account.email) + & filter (\account -> case account.name of Nothing -> True; Just _ -> False) & map email ``` diff --git a/slides/course-04.md b/slides/course-04.md index b102f13..e5794b3 100644 --- a/slides/course-04.md +++ b/slides/course-04.md @@ -365,6 +365,8 @@ class Functor f where # Be lazy, rely on the compiler +From course 2: + ```hs data Interval a = Interval { start :: a, @@ -373,7 +375,6 @@ data Interval a = Interval { deriving Functor ``` -- What can this type represent? - Write `Functor Interval` ---