Wednesday, 20 May 2015
Interactive Haskell
When this is all installed you can run a REPL via GHCi which is the Glasgow Haskell Compiler Interactive and which opens up in a DOS box... or there is a Windows version of the same thing which opens in a regular Windows - er - window... and rather smoothly I find I can start the same thing inside Emacs.
In Emacs you can start the interpreter and load the file you're working in with C-c C-l, or just start the interactive mode with C-c C-b.
So, running the Haskell REPL, what can you do?
Firstly there are REPL commands: these start with a colon character. So firstly you can change to the folder where your code is going to be:
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :cd \users\polly\documents\projects\haskell
Prelude> :!dir
Volume in drive C is Windows
Volume Serial Number is 20F4-549E
Directory of C:\users\polly\documents\projects\haskell
02/05/2015 11:12 <DIR> .
02/05/2015 11:12 <DIR> ..
02/05/2015 11:14 978 #notes.txt#
02/05/2015 10:47 1,950,052 hello.exe
02/05/2015 10:46 544 hello.hi
02/05/2015 10:44 31 hello.hs
02/05/2015 10:46 1,772 hello.o
02/05/2015 11:05 860 notes.txt
02/05/2015 10:56 354 notes.txt~
11 File(s) 1,959,067 bytes
2 Dir(s) 88,516,657,152 bytes free
In here you can load your file and run it by calling the main function:
Prelude> :load hello
Ok, modules loaded: Main.
Prelude Main> main
Hello World
Prelude Main>
What else can you do in here? Your basic infix arithmetic operators:
Prelude Main> 2 + 2
4
Function calls require no syntax at all:
Prelude Main> sqrt 10
3.1622776601683795
Round brackets and commas give you tuples: functions fst (first) and snd (second)
work on two-element tuples:
Prelude Main> fst (1,2)
1
Prelude Main> snd (1,2)
2
Square brackets and commas give you lists, with : as the cons operator:
Prelude Main> 1 : [2,3,4]
[1,2,3,4]
The identifier it gives you the result of the previous operation:
Prelude Main> tail it
[2,3,4]
Prelude Main> head it
2
head is the CAR function and tail is the CDR. Strings are lists of characters:
Prelude Main> head "Hello"
'H'
Prelude Main> tail "Hello"
"ello"
++ concatenated lists, including strings:
Prelude Main> 'H' : "ello"
"Hello"
Prelude Main> "H" ++ "ello"
"Hello"
You can try some functions on characters, if you first import the required library of character functions, so:
Prelude> import Data.Char
Prelude Data.Char> map toUpper "hello"
"HELLO"
The notation [x..y] gives you a list of elements in the range from x to y.
We have the familiar list operations map, filter and foldr (fold-right):
Prelude Main> map sqrt [1..10]
[1.0,1.4142135623730951,1.7320508075688772,2.0,2.23606797749979,2.44948974278317
8,2.6457513110645907,2.8284271247461903,3.0,3.1622776601683795]
Prelude Main> filter even [1..10]
[2,4,6,8,10]
Prelude Main> foldr (+) 0 [1..10]
55
The next most valuable REPL command is :t, which tells you the type of an expression.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment