Mercurial > repos > other > SevenLanguagesInSevenWeeks
comparison 7-Haskell/README.txt @ 94:eb868f089bd1
Add notes and examples on data types and monads
Still to do: Day 3 exercises
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 23 Jun 2019 20:13:55 +0100 |
parents | 7e4afb129bef |
children |
comparison
equal
deleted
inserted
replaced
93:39084e2b8744 | 94:eb868f089bd1 |
---|---|
55 This world view can be made more apparent with the following: | 55 This world view can be made more apparent with the following: |
56 let prod x y = x * y | 56 let prod x y = x * y |
57 let double = prod 2 | 57 let double = prod 2 |
58 let triple = prod 3 | 58 let triple = prod 3 |
59 double and triple are partially applied functions, and the only (sane) way to be able to do that is if "prod x y" is a two-part function! The book says "When Haskell computes prod 2 4, it is really computing "(prod 2) 4". [insert anonymous functions]". This is called currying. | 59 double and triple are partially applied functions, and the only (sane) way to be able to do that is if "prod x y" is a two-part function! The book says "When Haskell computes prod 2 4, it is really computing "(prod 2) 4". [insert anonymous functions]". This is called currying. |
60 | |
61 Haskell has "data types" that use the 'data' keyword to specify an allowed set of values, and "types" that reference data types, e.g.: | |
62 data Suit = Spades | Hearts | |
63 data Rank = Ten | Jack | Queen | King | Ace | |
64 type Card = (Rank, Suit) | |
65 type Hand = [Card] | |
66 Suit can be Spades or Hearts, Rank can be Ten through Ace (no ordering implied here because we don't use "deriving Ord"), a Card is a tuple of a rank and a suit, and a Hand is a list of cards. | |
67 This will fail to print values in the console unless we add "deriving (Show)" to the end of the "data …" lines. | |
68 | |
69 The book isn't clear what makes a "data" vs a "type", but it appears at first to be about whether it refers to values or other "classes" ('data' types). | |
70 Except that on the next page the "Triplet" definition doesn't meet that criteria. | |
71 The Haskell wiki (https://wiki.haskell.org/Type) says types are defined using "data" and then "type" introduces synonyms. | |
72 The examples are: | |
73 data Maybe a = Just a | Nothing | |
74 (Define "Maybe" as a polymorphic of type variable "a" as "Just a thing of that type 'a'" or "Nothing") | |
75 data Tree a = Branch (Tree a) (Tree a) | Leaf a | |
76 (Define "Tree" as a polymorphic of type variable "a" as a Branch of two trees of type a or a Leaf of type a - you'd assume Branch and Leaf are defined elsewhere, but apparently this is enough to define them implicitly!) | |
77 | |
78 Note: The wiki defines Card as a "data", not as a "type". It also does it quite differently, using "Card = Card { value :: Rank, suit :: Suit } …", which seems recursive at first but is probably just like "class MyClass { public MyClass(var a) { … } }". | |
79 Also from the wiki, if you "deriving" something like Ord or Enum then you need to use "instance [derived thing] [my type] where … [implementation]" | |
80 | |
81 Haskell also includes "classes", but they're not like Object-Oriented classes. They define which operations work on which inputs (e.g. Bool + Bool doesn't work, but Integer + Integer does). It's a bit more like an interface in that it defines functionality | |
82 and guarantees functions exist BUT with optional default implementation. For example, in the following definition: | |
83 | |
84 class Eq a where | |
85 (==), (/=) :: a -> a -> Bool | |
86 x /= y = not (x == y) | |
87 x == y = not (x /= y) | |
88 | |
89 This means nothing on its own, but any data type "deriving Eq" will work with "==" and "/=" and you only need to implement one for your type to get the other for free. | |
90 | |
91 Monads. Monads are about function composition. They let you do the equivalent of "v = func1(v); v = func1(v); v = func2(v); return v" from imperative languages. | |
92 You can do this pattern with a multi-line "let … in [return_value]" *but* you can't reuse variable names. | |
93 | |
94 Monads have a type constructor that takes a container, a function called "return" to wrap a function in the container, and a bind function called ">>=" that unwraps and chains the function. See monad.hs. | |
95 | |
96 A built-in use of monads is the "do" notation, which gives an imperative style on the surface but is implemented with monads: | |
97 | |
98 tryIo = do putStr "Enter your name: " ; | |
99 line <- getLine ; | |
100 let { backwards = reverse line } ; | |
101 return ("Hello. Your name backwards is " ++ backwards) | |
102 | |
103 That's a function call "tryIo" that uses "do" to look imperitive. This is just syntactic sugar so that you don't have to type ">>=" so often. The semi-colons are optional, and you can wrap the code in "{}" | |
104 | |
105 A list is already defined as a monad using "instance". The definitions are simple enough: | |
106 | |
107 instance Monad [] where | |
108 m >>= f = concatMap f m | |
109 return x = [x] | |
110 | |
111 So we define a monad on the list (top line) then define the binding, which applies the function to every value in the list, and then the return value is a list of the value. | |
112 This allows lists to be used in "do …" blocks to write a "for each item in an array return a value" function, such as: | |
113 let cartesian (xs, ys) = do x <- xs; y <- ys; return (x, y) | |
114 Here, we take in two lists (xs and ys) and for each pair x and y we return the tuple BUT the monad definition and the "do" operation means that we get a list returned: | |
115 Main> cartesian ([1..2], [3..4]) | |
116 [(1,3), (1,4), (2,3), (2,4)] | |
117 The book says this is "an alternate computational strategy" to list comprehension, but it reads almost entirely like list comprehension. The only difference in this example is that the "return" is at the end. c.f.: | |
118 let cartesian_list (xs, ys) = [(x,y) | x <- xs, y <- ys] | |
119 The password cracking example is a little more complex but could still probably be done with list comprehension. | |
120 | |
121 "Maybe" is also a monad on the Maybe data type. The type is defined as "Nothing" or "Just a". The Monad returns is an alias for "Just" and so the result is "Just x" and there are two ways of binding the function. | |
122 Nothing >>= f = Nothing | |
123 (Just x) >>= f = f x | |
124 i.e. there are no "null pointer exception"-type problems for chaining function calls as applying a function to Nothing safely returns Nothing. |