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: source_md/functors-applicative-functors-and-monoids.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -223,7 +223,7 @@ So `a -> b -> c` can be written as `a -> (b -> c)`, to make the currying more ap
223
223
In the same vein, if we write `fmap :: (a -> b) -> (f a -> f b)`, we can think of `fmap` not as a function that takes one function and a functor and returns a functor, but as a function that takes a function and returns a new function that's just like the old one, only it takes a functor as a parameter and returns a functor as the result.
224
224
It takes an `a -> b` function and returns a function `f a -> f b`.
225
225
This is called *lifting* a function.
226
-
Let's play around with that idea by using GHCI's `:t` command:
226
+
Let's play around with that idea by using GHCi's `:t` command:
227
227
228
228
```{.haskell:hs}
229
229
ghci> :t fmap (*2)
@@ -241,7 +241,7 @@ When we say *a functor over numbers*, you can think of that as *a functor that h
241
241
The former is a bit fancier and more technically correct, but the latter is usually easier to get.
242
242
:::
243
243
244
-
This is even more apparent if we partially apply, say, `fmap (++"!")` and then bind it to a name in GHCI.
244
+
This is even more apparent if we partially apply, say, `fmap (++"!")` and then bind it to a name in GHCi.
245
245
246
246
You can think of `fmap` as either a function that takes a function and a functor and then maps that function over the functor, or you can think of it as a function that takes a function and lifts that function so that it operates on functors.
247
247
Both views are correct and in Haskell, equivalent.
Copy file name to clipboardExpand all lines: source_md/higher-order-functions.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -130,7 +130,7 @@ From the definition of sections, `(-4)` would result in a function that takes a
130
130
However, for convenience, `(-4)` means minus four.
131
131
So if you want to make a function that subtracts 4 from the number it gets as a parameter, partially apply the `subtract` function like so: `(subtract 4)`.
132
132
133
-
What happens if we try to just do `multThree 3 4` in GHCI instead of binding it to a name with a *let* or passing it to another function?
133
+
What happens if we try to just do `multThree 3 4` in GHCi instead of binding it to a name with a *let* or passing it to another function?
134
134
135
135
```{.haskell:hs}
136
136
ghci> multThree 3 4
@@ -140,9 +140,9 @@ ghci> multThree 3 4
140
140
• In a stmt of an interactive GHCi command: print it
141
141
```
142
142
143
-
GHCI is telling us that the expression produced a function of type `a -> a` but it doesn't know how to print it to the screen.
143
+
GHCi is telling us that the expression produced a function of type `a -> a` but it doesn't know how to print it to the screen.
144
144
Functions aren't instances of the `Show` typeclass, so we can't get a neat string representation of a function.
145
-
When we do, say, `1 + 1` at the GHCI prompt, it first calculates that to `2` and then calls `show` on `2` to get a textual representation of that number.
145
+
When we do, say, `1 + 1` at the GHCi prompt, it first calculates that to `2` and then calls `show` on `2` to get a textual representation of that number.
146
146
And the textual representation of `2` is just the string `"2"`, which then gets printed to our screen.
147
147
148
148
## Some higher-orderism is in order {#higher-orderism}
@@ -392,7 +392,7 @@ First, we'll begin by mapping the `(^2)` function to the infinite list `[1..]`.
392
392
Then we filter them so we only get the odd ones.
393
393
And then, we'll take elements from that list while they are smaller than 10,000.
394
394
Finally, we'll get the sum of that list.
395
-
We don't even have to define a function for that, we can do it in one line in GHCI:
395
+
We don't even have to define a function for that, we can do it in one line in GHCi:
396
396
397
397
```{.haskell:ghci}
398
398
ghci> sum (takeWhile (<10000) (filter odd (map (^2) [1..])))
Up until now, we've always loaded our functions into GHCI to test them out and play with them.
30
+
Up until now, we've always loaded our functions into GHCi to test them out and play with them.
31
31
We've also explored the standard library functions that way.
32
32
But now, after eight or so chapters, we're finally going to write our first *real* Haskell program!
33
33
Yay!
@@ -208,14 +208,14 @@ We can also use a *do* block to glue together a few I/O actions and then we can
208
208
Either way, they'll be performed only if they eventually fall into `main`.
209
209
210
210
Oh, right, there's also one more case when I/O actions will be performed.
211
-
When we type out an I/O action in GHCI and press return, it will be performed.
211
+
When we type out an I/O action in GHCi and press return, it will be performed.
212
212
213
213
```{.haskell:hs}
214
214
ghci> putStrLn "HEEY"
215
215
HEEY
216
216
```
217
217
218
-
Even when we just punch out a number or call a function in GHCI and press return, it will evaluate it (as much as it needs) and then call `show` on it and then it will print that string to the terminal using `putStrLn` implicitly.
218
+
Even when we just punch out a number or call a function in GHCi and press return, it will evaluate it (as much as it needs) and then call `show` on it and then it will print that string to the terminal using `putStrLn` implicitly.
219
219
220
220
Remember *let* bindings?
221
221
If you don't, refresh your memory on them by reading [this section](syntax-in-functions.html#let-it-be).
@@ -429,8 +429,8 @@ True
429
429
```
430
430
431
431
As you can see, it's a very handy function.
432
-
Remember how we talked about how I/O actions are performed only when they fall into `main` or when we try to evaluate them in the GHCI prompt?
433
-
When we type out a value (like `3` or `[1,2,3]`) and press the return key, GHCI actually uses `print` on that value to display it on our terminal!
432
+
Remember how we talked about how I/O actions are performed only when they fall into `main` or when we try to evaluate them in the GHCi prompt?
433
+
When we type out a value (like `3` or `[1,2,3]`) and press the return key, GHCi actually uses `print` on that value to display it on our terminal!
Well, when we evaluate an I/O action in GHCI, it's performed and then its result is printed out, unless that result is `()`, in which case it's not printed out.
538
-
That's why evaluating `putStrLn "hehe"` in GHCI just prints out `hehe` (because the contained result in `putStrLn "hehe"` is `()`).
539
-
But when we do `getLine` in GHCI, the result of that I/O action is printed out, because `getLine` has a type of `IO String`.
537
+
Well, when we evaluate an I/O action in GHCi, it's performed and then its result is printed out, unless that result is `()`, in which case it's not printed out.
538
+
That's why evaluating `putStrLn "hehe"` in GHCi just prints out `hehe` (because the contained result in `putStrLn "hehe"` is `()`).
539
+
But when we do `getLine` in GHCi, the result of that I/O action is printed out, because `getLine` has a type of `IO String`.
540
540
541
541
Because mapping a function that returns an I/O action over a list and then sequencing it is so common, the utility functions `mapM`{.label .function} and `mapM_`{.label .function} were introduced.
542
542
`mapM` takes a function and a list, maps the function over the list and then sequences it.
@@ -626,7 +626,7 @@ In this section, we learned the basics of input and output.
626
626
We also found out what I/O actions are, how they enable us to do input and output and when they are actually performed.
627
627
To reiterate, I/O actions are values much like any other value in Haskell.
628
628
We can pass them as parameters to functions and functions can return I/O actions as results.
629
-
What's special about them is that if they fall into the `main` function (or are the result in a GHCI line), they are performed.
629
+
What's special about them is that if they fall into the `main` function (or are the result in a GHCi line), they are performed.
630
630
And that's when they get to write stuff on your screen or play Yakety Sax through your speakers.
631
631
Each I/O action can also encapsulate a result with which it tells you what it got from the real world.
632
632
@@ -1920,7 +1920,7 @@ This is cool because it won't cause the memory usage to skyrocket and the 32 KiB
1920
1920
1921
1921
If you look through the [documentation](https://hackage.haskell.org/package/bytestring/docs/Data-ByteString-Lazy.html) for `Data.ByteString.Lazy`, you'll see that it has a lot of functions that have the same names as the ones from `Data.List`, only the type signatures have `ByteString` instead of `[a]` and `Word8` instead of `a` in them.
1922
1922
The functions with the same names mostly act the same as the ones that work on lists.
1923
-
Because the names are the same, we're going to do a qualified import in a script and then load that script into GHCI to play with bytestrings.
1923
+
Because the names are the same, we're going to do a qualified import in a script and then load that script into GHCi to play with bytestrings.
Copy file name to clipboardExpand all lines: source_md/modules.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,14 +37,14 @@ When you do `import Data.List`, all the functions that `Data.List` exports becom
37
37
`nub` is a function defined in `Data.List` that takes a list and weeds out duplicate elements.
38
38
Composing `length` and `nub` by doing `length . nub` produces a function that's the equivalent of `\xs -> length (nub xs)`.
39
39
40
-
You can also put the functions of modules into the global namespace when using GHCI.
41
-
If you're in GHCI and you want to be able to call the functions exported by `Data.List`, do this:
40
+
You can also put the functions of modules into the global namespace when using GHCi.
41
+
If you're in GHCi and you want to be able to call the functions exported by `Data.List`, do this:
42
42
43
43
```{.haskell:ghci}
44
44
ghci> :m + Data.List
45
45
```
46
46
47
-
If we want to load up the names from several modules inside GHCI, we don't have to do `:m +` several times, we can just load up several modules at once.
47
+
If we want to load up the names from several modules inside GHCi, we don't have to do `:m +` several times, we can just load up several modules at once.
48
48
49
49
```{.haskell:ghci}
50
50
ghci> :m + Data.List Data.Map Data.Set
@@ -945,7 +945,7 @@ Because `Data.Map` exports functions that clash with the `Prelude` and `Data.Lis
945
945
import qualified Data.Map as Map
946
946
```
947
947
948
-
Put this import statement into a script and then load the script via GHCI.
948
+
Put this import statement into a script and then load the script via GHCi.
949
949
950
950
Let's go ahead and see what `Data.Map` has in store for us!
951
951
Here's the basic rundown of its functions.
@@ -1158,7 +1158,7 @@ Put this import statement in a script:
1158
1158
import qualified Data.Set as Set
1159
1159
```
1160
1160
1161
-
And then load the script via GHCI.
1161
+
And then load the script via GHCi.
1162
1162
1163
1163
Let's say we have two pieces of text.
1164
1164
We want to find out which characters were used in both of them.
Copy file name to clipboardExpand all lines: source_md/starting-out.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ GHCi, version 9.2.4: https://www.haskell.org/ghc/ :? for help
17
17
ghci>
18
18
```
19
19
20
-
Congratulations, you're in GHCI!
20
+
Congratulations, you're in GHCi!
21
21
22
22
Here's some simple arithmetic.
23
23
@@ -50,7 +50,7 @@ Pretty cool, huh?
50
50
Yeah, I know it's not but bear with me.
51
51
A little pitfall to watch out for here is negating numbers.
52
52
If we want to have a negative number, it's always best to surround it with parentheses.
53
-
Doing `5 * -3` will make GHCI yell at you but doing `5 * (-3)` will work just fine.
53
+
Doing `5 * -3` will make GHCi yell at you but doing `5 * (-3)` will work just fine.
54
54
55
55
Boolean algebra is also pretty straightforward.
56
56
As you probably know, `&&` means a boolean *and*, `||` means a boolean *or*.
@@ -96,10 +96,10 @@ Well, if we try the first snippet, we get a big scary error message!
96
96
```
97
97
98
98
Yikes!
99
-
What GHCI is telling us here is that `"llama"` is not a number and so it doesn't know how to add it to 5.
99
+
What GHCi is telling us here is that `"llama"` is not a number and so it doesn't know how to add it to 5.
100
100
Even if it wasn't `"llama"` but `"four"` or `"4"`, Haskell still wouldn't consider it to be a number.
101
101
`+` expects its left and right side to be numbers.
102
-
If we tried to do `True == 5`, GHCI would tell us that the types don't match.
102
+
If we tried to do `True == 5`, GHCi would tell us that the types don't match.
103
103
Whereas `+` works only on things that are considered numbers, `==` works on any two things that can be compared.
104
104
But the catch is that they both have to be the same type of thing.
105
105
You can't compare apples and oranges.
@@ -189,7 +189,7 @@ The function name is followed by parameters separated by spaces.
189
189
But when defining functions, there's a `=` and after that we define what the function does.
190
190
Save this as `baby.hs` or something.
191
191
Now navigate to where it's saved and run `ghci` from there.
192
-
Once inside GHCI, do `:l baby`.
192
+
Once inside GHCi, do `:l baby`.
193
193
Now that our script is loaded, we can play with the function that we defined.
194
194
195
195
```{.haskell: .ghci}
@@ -211,7 +211,7 @@ doubleUs x y = x*2 + y*2
211
211
212
212
Simple.
213
213
We could have also defined it as `doubleUs x y = x + x + y + y`.
214
-
Testing it out produces pretty predictable results (remember to append this function to the `baby.hs` file, save it and then do `:l baby` inside GHCI).
214
+
Testing it out produces pretty predictable results (remember to append this function to the `baby.hs` file, save it and then do `:l baby` inside GHCi).
215
215
216
216
```{.haskell: .ghci}
217
217
ghci> doubleUs 4 9
@@ -761,7 +761,7 @@ ghci> [ [ x | x <- xs, even x ] | xs <- xxs]
761
761
```
762
762
763
763
You can write list comprehensions across several lines.
764
-
So if you're not in GHCI, it's better to split longer list comprehensions across multiple lines, especially if they're nested.
764
+
So if you're not in GHCi, it's better to split longer list comprehensions across multiple lines, especially if they're nested.
765
765
766
766
## Tuples {#tuples}
767
767
@@ -882,7 +882,7 @@ ghci> triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]
882
882
```
883
883
884
884
We're just drawing from three lists and our output function is combining them into a triple.
885
-
If you evaluate that by typing out `triangles` in GHCI, you'll get a list of all possible triangles with sides under or equal to 10.
885
+
If you evaluate that by typing out `triangles` in GHCi, you'll get a list of all possible triangles with sides under or equal to 10.
886
886
Next, we'll add a condition that they all have to be right triangles.
887
887
We'll also modify this function by taking into consideration that side b isn't larger than the hypotenuse and that side a isn't larger than side b.
0 commit comments