Skip to content

Commit 16b8c2a

Browse files
authored
Merge pull request #173 from cherryblossom000/minor-fmt
brackets in nested >>=
2 parents b6b4154 + d237307 commit 16b8c2a

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

_chapters/haskell2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ student = UserId 1337
4848

4949
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.
5050

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.
5252
</div>
5353

5454
## Pattern Matching

_chapters/monad.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,40 @@ The first argument it expects is a value in a context `m a`. What if that we ap
137137
138138
```haskell
139139
> 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
142142
```
143143
144144
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:
145145
146146
```haskell
147147
safeSolve :: Float -> Float -> Float -> Maybe (Float, Float)
148148
safeSolve a b c =
149-
safeSqrt (b*b - 4 * a * c) >>= \s ->
150-
safeDiv (-b + s) (2*a) >>= \x1 ->
151-
safeDiv (-b - s) (2*a) >>= \x2 ->
152-
pure (x1,x2)
149+
safeSqrt (b*b - 4 * a * c) >>= \s -> (
150+
safeDiv (-b + s) (2*a) >>= \x1 -> (
151+
safeDiv (-b - s) (2*a) >>= \x2 -> (
152+
pure (x1,x2)
153+
)
154+
)
155+
)
153156
154157
> safeSolve 1 3 2
155158
Just (-1.0,-2.0)
156159
> safeSolve 1 1 2
157160
Nothing
158161
```
159162
163+
We actually don't need all the brackets, so we could have written `safeSolve` as the following:
164+
165+
```haskell
166+
safeSolve :: Float -> Float -> Float -> Maybe (Float, Float)
167+
safeSolve a b c =
168+
safeSqrt (b*b - 4 * a * c) >>= \s ->
169+
safeDiv (-b + s) (2*a) >>= \x1 ->
170+
safeDiv (-b - s) (2*a) >>= \x2 ->
171+
pure (x1,x2)
172+
```
173+
160174
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:
161175
162176
```haskell
@@ -337,13 +351,15 @@ do
337351
which is itself syntactic sugar for:
338352
339353
```haskell
354+
['a'..'d'] >>= \i -> ([1..4] >>= \j -> pure (i,j))
355+
-- without the brackets:
340356
['a'..'d'] >>= \i -> [1..4] >>= \j -> pure (i,j)
341357
```
342358
343359
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`:
344360
345361
```haskell
346-
[(i,j) | i<-['a'..'d'], j<-[1..4], j `mod` 2 == 0]
362+
[(i, j) | i <- ['a'..'d'], j <- [1..4], j `mod` 2 == 0]
347363
```
348364
349365
This comprehension syntax desugars to a `do`-block using the `guard` function from `Control.Monad` like so:

_sass/minima/custom-styles.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#play-theme-song {
2+
color: var(--minima-site-title-color);
3+
}
4+
15
.cheatsheet, .glossary {
26
background-color: #f3f0f0;
37
padding: 8px 12px;

0 commit comments

Comments
 (0)