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
*[Stack] = managing Haskell projects, works with [Cabal] for you.
57
57
58
-
:point_right: Please, install these (or check if installed already) - you can follow instruction on official websites one by one or (better) install [Haskell Platform], which includes all of those and also most common packages.
58
+
:point_right: Please, install these (or check if installed already) - you can follow instruction on official websites one by one or (recommended and better) install [GHCup], which includes all of those and also most common packages.
59
59
60
60
### Editors and IDEs
61
61
@@ -66,21 +66,6 @@ There are several editors you may use for writing Haskell programs, most probabl
66
66
*[Atom with plugins](https://atom-haskell.github.io/overview/)
67
67
*[Visual Studio Code with plugins](https://medium.com/@dogwith1eye/setting-up-haskell-in-vs-code-with-stack-and-the-ide-engine-81d49eda3ecf)
68
68
69
-
Most probably you will need following stuff:
70
-
71
-
*[ghc-mod] = connector to [GHC] API for various stuff, alternatively you can use [HIE](https://github.com/haskell/haskell-ide-engine)
Install those with [Cabal] (by default it will install just for you to your profile, ensure that you have `~/.cabal/bin` in your `PATH`) or with [Stack]. The installation might take a while - it has a lot of dependencies and needs to build them from Haskell source code. If you want to install something with [Cabal] to all users, use `--global` flag.
@@ -106,19 +91,19 @@ Now you should have [GHC] installed from package or via Stack (and others as wel
106
91
107
92
```console
108
93
% ghc --version
109
-
The Glorious Glasgow Haskell Compilation System, version 8.0.2
94
+
The Glorious Glasgow Haskell Compilation System, version 9.4.8
110
95
% stack exec -- ghc --version
111
-
The Glorious Glasgow Haskell Compilation System, version 8.0.2
96
+
The Glorious Glasgow Haskell Compilation System, version 9.4.5
112
97
```
113
98
114
99
First, let's try the interactive environment and evaluate some basic expression in Haskell for the first time.
115
100
116
101
```
117
102
% ghci
118
-
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
119
-
Prelude> "Hello, world!"
103
+
GHCi, version 9.4.8: http://www.haskell.org/ghc/ :? for help
104
+
ghci> "Hello, world!"
120
105
"Hello, world!"
121
-
Prelude> putStrLn "Hello, world!"
106
+
ghci> putStrLn "Hello, world!"
122
107
Hello, world!
123
108
```
124
109
@@ -129,43 +114,43 @@ In prompt you see (by default) imported modules, in this case just `Prelude` - t
129
114
In Haskell you can use math operators as you are used to.
130
115
131
116
```
132
-
Prelude> 5 + 5
117
+
ghci> 5 + 5
133
118
10
134
-
Prelude> 5 + 5 * 3
119
+
ghci> 5 + 5 * 3
135
120
20
136
-
Prelude> (5 + 5) * 3
121
+
ghci> (5 + 5) * 3
137
122
30
138
-
Prelude> 2 ^ 8
123
+
ghci> 2 ^ 8
139
124
256
140
-
Prelude> 2 / 3
125
+
ghci> 2 / 3
141
126
0.6666666666666666
142
127
```
143
128
144
129
Integer division and modulo are done by functions. You can call functions in prefix notation (no brackets and no commas):
145
130
146
131
```
147
-
Prelude> div 7 2
132
+
ghci> div 7 2
148
133
3
149
-
Prelude> mod 7 2
134
+
ghci> mod 7 2
150
135
1
151
136
```
152
137
153
138
Same goes for logic and comparison (you might be used to `!=` or `<>` for not-equal, but `!` and `<>` are used for something else in Haskell we will find out during the course):
154
139
155
140
```
156
-
Prelude> 5 > 7
141
+
ghci> 5 > 7
157
142
False
158
-
Prelude> 5 == 7
143
+
ghci> 5 == 7
159
144
False
160
-
Prelude> 5 /= 7
145
+
ghci> 5 /= 7
161
146
True
162
-
Prelude> not (5 /= 7)
147
+
ghci> not (5 /= 7)
163
148
False
164
-
Prelude> False || True
149
+
ghci> False || True
165
150
True
166
-
Prelude> False && True
151
+
ghci> False && True
167
152
False
168
-
Prelude> not False && True
153
+
ghci> not False && True
169
154
True
170
155
```
171
156
@@ -174,20 +159,20 @@ True
174
159
A very useful thing in GHCi is that you can check the type of an expression.
175
160
176
161
```
177
-
Prelude> :type 2 ^ 8
162
+
ghci> :type 2 ^ 8
178
163
2 ^ 8 :: Num a => a
179
-
Prelude> :type 2 / 3
164
+
ghci> :type 2 / 3
180
165
2 / 3 :: Fractional a => a
181
166
```
182
167
183
168
The double semicolon `::` means "is of type" and you can use it for explicitly stating the type of your expressions. But this is not typecasting as you might know, you must conform the restriction, in this case, `Fractional a` (typeclasses will be covered deeply in next lessons).
184
169
185
170
```
186
-
Prelude> (2 / 3) :: Double
171
+
ghci> (2 / 3) :: Double
187
172
0.6666666666666666
188
-
Prelude> (2 / 3) :: Float
173
+
ghci> (2 / 3) :: Float
189
174
0.6666667
190
-
Prelude> (2 / 3) :: Int
175
+
ghci> (2 / 3) :: Int
191
176
192
177
<interactive>:2:2: error:
193
178
• No instance for (Fractional Int) arising from a use of ‘/’
@@ -200,11 +185,11 @@ You can see that error message exactly tells us what is wrong! `Int` is not an i
200
185
If you need to change type, find a suitable function. Some of them are in `Prelude`: `toInteger`, `fromInteger`, `toRational`, etc. Another quite important is `show` for showing anything as `String`. How these work will be covered later on in more detail as we get to typeclasses and polymorphism!
201
186
202
187
```
203
-
Prelude> :t toInteger (7::Int)
188
+
ghci> :t toInteger (7::Int)
204
189
toInteger (7::Int) :: Integer
205
-
Prelude> show (2/3)
190
+
ghci> show (2/3)
206
191
"0.6666666666666666"
207
-
Prelude> toRational (2/16)
192
+
ghci> toRational (2/16)
208
193
1 % 8
209
194
```
210
195
@@ -213,13 +198,13 @@ Similarly you can do such thing with functions, because we are in functional lan
213
198
The type signature is very math-like... Instance (type) of `Num` is for example `Integer` and you know functions from math which have type `Integer -> Integer` (domain and co-domain).
214
199
215
200
```
216
-
Prelude> :type abs
201
+
ghci> :type abs
217
202
abs :: Num a => a -> a
218
-
Prelude> abs (-5)
203
+
ghci> abs (-5)
219
204
5
220
-
Prelude> abs (-10.65)
205
+
ghci> abs (-10.65)
221
206
10.65
222
-
Prelude> abs "z"
207
+
ghci> abs "z"
223
208
224
209
<interactive>:26:1: error:
225
210
• No instance for (Num [Char]) arising from a use of ‘abs’
@@ -230,20 +215,20 @@ Prelude> abs "z"
230
215
The operators are functions as well - Haskell is functional language. All you need to do is put it in brackets. Plus takes two numbers and returns a number. You can then use `(+)` as a function in prefix notation and not infix.
231
216
232
217
```
233
-
Prelude> :type (+)
218
+
ghci> :type (+)
234
219
(+) :: Num a => a -> a -> a
235
-
Prelude> (+) 5 4
220
+
ghci> (+) 5 4
236
221
9
237
222
```
238
223
239
224
On the other hand you might want to use some functions in infix to improve readability and you need `` ` `` for that.
240
225
241
226
```
242
-
Prelude> :t div
227
+
ghci> :t div
243
228
div :: Integral a => a -> a -> a
244
-
Prelude> 5 `div` 3
229
+
ghci> 5 `div` 3
245
230
1
246
-
Prelude> 5 `mod` 3
231
+
ghci> 5 `mod` 3
247
232
2
248
233
```
249
234
@@ -252,21 +237,21 @@ Prelude> 5 `mod` 3
252
237
In GHCi you can name an expression with `let` and assignment.
253
238
254
239
```
255
-
Prelude> let x = 5
256
-
Prelude> :type x
240
+
ghci> let x = 5
241
+
ghci> :type x
257
242
x :: Num t => t
258
-
Prelude> let x = 5 :: Integer
259
-
Prelude> :type x
243
+
ghci> let x = 5 :: Integer
244
+
ghci> :type x
260
245
x :: Integer
261
246
```
262
247
263
248
You can create functions as well. Notice that the type is automatically inferred. It happens every time when possible and you don't explicitly state the type.
264
249
265
250
```
266
-
Prelude> let myFunc x y = 2 * x + y
267
-
Prelude> :t myFunc
251
+
ghci> let myFunc x y = 2 * x + y
252
+
ghci> :t myFunc
268
253
myFunc :: Num a => a -> a -> a
269
-
Prelude> myFunc 5 3
254
+
ghci> myFunc 5 3
270
255
13
271
256
```
272
257
@@ -297,7 +282,7 @@ isTriangle a b c = (a + b > c) && (a + c > b) && (b + c > a)
297
282
Now we can load it with `:load` to GHCi:
298
283
299
284
```
300
-
Prelude> :load FPCourse/files/01_test_haskell.hs
285
+
ghci> :load FPCourse/files/01_test_haskell.hs
301
286
[1 of 1] Compiling Main ( 01_test_haskell.hs, interpreted )
302
287
Ok, modules loaded: Main.
303
288
```
@@ -423,7 +408,7 @@ Let's do the *Hello, world!* app with [Stack]. First, verify that you have it in
423
408
424
409
```console
425
410
% stack --version
426
-
Version 1.9.3, Git revision 40cf7b37526b86d1676da82167ea8758a854953b (6211 commits) x86_64 hpack-0.31.1
411
+
Version 2.13.1, Git revision 8102bb8afce90fc954f48efae38b87f37cabc988 (9949 commits) aarch64 hpack-0.36.0
427
412
```
428
413
429
414
Then you can create a new project with default template:
@@ -459,10 +444,10 @@ Using cabal packages:
459
444
460
445
Selecting the best among 15 snapshots...
461
446
462
-
* Matches lts-13.8
447
+
* Matches lts-21.25
463
448
464
-
Selected resolver: lts-13.8
465
-
Initialising configuration using resolver: lts-13.8
449
+
Selected resolver: lts-21.25
450
+
Initialising configuration using resolver: lts-21.25
466
451
Total number of user packages considered: 1
467
452
Writing configuration to file: HelloWorld/stack.yaml
468
453
All done.
@@ -497,7 +482,7 @@ Now you don't use GHC directly, but call it via `stack build`:
497
482
498
483
```console
499
484
% stack build
500
-
No compiler found, expected minor version match with ghc-8.0.2 (x86_64) (based on resolver setting in /home/user/.stack/global-project/stack.yaml).
485
+
No compiler found, expected minor version match with ghc-9.4.8 (x86_64) (based on resolver setting in /home/user/.stack/global-project/stack.yaml).
501
486
To install the correct GHC into /home/user/.stack/programs/x86_64-linux/, try running "stack setup" or use the "--install-ghc" flag. To use your system GHC installation, run "stack config set system-ghc --global true", or use the "--system-ghc" flag.
502
487
```
503
488
@@ -507,7 +492,7 @@ As you see `stack` doesn't want to use system-wide installation of `ghc` but loc
507
492
% stack setup
508
493
Preparing to install GHC to an isolated location.
509
494
This will not interfere with any system-level installation.
510
-
Downloaded ghc-8.0.2.
495
+
Downloaded ghc-9.4.8.
511
496
Installed GHC.
512
497
stack will use a sandboxed GHC it installed
513
498
For more information on paths, see 'stack path' and 'stack exec env'
@@ -521,10 +506,10 @@ Using cabal packages:
521
506
522
507
Selecting the best among 11 snapshots...
523
508
524
-
* Matches lts-13.8
509
+
* Matches lts-21.25
525
510
526
-
Selected resolver: lts-13.8
527
-
Initialising configuration using resolver: lts-9.11
511
+
Selected resolver: lts-21.25
512
+
Initialising configuration using resolver: lts-21.25
528
513
Total number of user packages considered: 1
529
514
Writing configuration to file: stack.yaml
530
515
All done.
@@ -544,8 +529,8 @@ Building executable 'HelloWorld-exe' for HelloWorld-0.1.0.0..
544
529
[2 of 2] Compiling Paths_HelloWorld ( .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/HelloWorld-exe/autogen/Paths_HelloWorld.hs, .stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1/build/HelloWorld-exe/HelloWorld-exe-tmp/Paths_HelloWorld.o )
Installing library in /home/user/Projects/MI-AFP/tests/HelloWorld/.stack-work/install/x86_64-linux-tinfo6/lts-13.8/8.6.3/lib/x86_64-linux-ghc-8.6.3/HelloWorld-0.1.0.0-8b39YCi0nmn4QsoDKix2j8
548
-
Installing executable HelloWorld-exe in /home/user/Projects/MI-AFP/tests/HelloWorld/.stack-work/install/x86_64-linux-tinfo6/lts-13.8/8.6.3/bin
532
+
Installing library in /home/user/Projects/MI-AFP/tests/HelloWorld/.stack-work/install/x86_64-linux-tinfo6/lts-21.25/9.4.8/lib/x86_64-linux-ghc-8.6.3/HelloWorld-0.1.0.0-8b39YCi0nmn4QsoDKix2j8
533
+
Installing executable HelloWorld-exe in /home/user/Projects/MI-AFP/tests/HelloWorld/.stack-work/install/x86_64-linux-tinfo6/lts-21.25/9.4.8/bin
549
534
Registering library for HelloWorld-0.1.0.0..
550
535
stack build 6.16s user 0.94s system 96% cpu 7.329 total
551
536
```
@@ -647,13 +632,12 @@ For your first assignment, visit [MI-AFP/hw01](https://github.com/MI-AFP/hw01).
0 commit comments