You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _chapters/haskell2.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ student = UserId 1337
48
48
49
49
The `newtype` keyword is used to define a type that has *exactly* one constructor with *exactly* one field. It is primarily used for creating a distinct type from an existing type with *zero runtime* overhead. This can be useful for adding type safety to your code by creating new types that are distinct from their underlying types or giving types a greater semantic meaning, e.g., a UserId compared to an Int.
50
50
51
-
The data keyword is used to define an algebraic data type (ADT). This allows for the creation of complex data structures that can have multiple constructors. Each constructor can take zero or more arguments, and these arguments can be of any type.
51
+
The `data` keyword is used to define an algebraic data type (ADT). This allows for the creation of complex data structures that can have multiple constructors. Each constructor can take zero or more arguments, and these arguments can be of any type.
Copy file name to clipboardExpand all lines: _chapters/monad.md
+23-7Lines changed: 23 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,26 +137,40 @@ The first argument it expects is a value in a context `m a`. What if that we ap
137
137
138
138
```haskell
139
139
> x = 1::Float
140
-
> :t (Just x>>=)
141
-
(Just x>>=) :: (Float -> Maybe b) -> Maybe b
140
+
> :t (Just x>>=)
141
+
(Just x>>=) :: (Float -> Maybe b) -> Maybe b
142
142
```
143
143
144
144
So GHCi is telling us that the next argument has to be a function that takes a `Float` as input, and gives back anything in a `Maybe`. Our `safeSqrt` definitely fits this description, as does `safeDiv` partially applied to a `Float`. So, here’s a `safeSolve` which uses `(>>=)` to remove the need for `case`s:
Note that Haskell has a special notation for such multi-line use of bind, called “`do` notation”. The above code in a `do` block looks like:
161
175
162
176
```haskell
@@ -337,13 +351,15 @@ do
337
351
which is itself syntactic sugar for:
338
352
339
353
```haskell
354
+
['a'..'d'] >>= \i -> ([1..4] >>= \j -> pure (i,j))
355
+
-- without the brackets:
340
356
['a'..'d'] >>= \i -> [1..4] >>= \j -> pure (i,j)
341
357
```
342
358
343
359
List comprehensions can also include conditional expressions which must evaluate to true for terms to be included in the list result. For example, we can limit the above comprehension to only pairs with even `j`:
0 commit comments