brew install ghc
It comes with 2 programs:
ghc
: (Glasgow Haskell Compiler)ghci
: REPL (Read-Eval-Print Loop) for Haskell
Just run ghci
in the terminal to start the REPL.
To quit the REPL, type :quit
or :q
. (But I just do ctrl + d
, haha)
To write multiple line code in the REPL, use :{
and :}
.
:{
add :: Int -> Int -> Int
add x y = x + y
:}
The offical haskell recommend using GHCup as installer.
My experience installing ghcup is not that smooth:
- the doc recommend using
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
for downloading the install shell script and running it. Somehow it didn't work for me, after removing the--tlsv1.2
part, it worked. - you may need to manually append the path(usually
~/.ghcup/bin
) to thePATH
variable in your shell profile file.
-- this is a comment, no `;` at the end
print "Hello, World!"
-- declare var
greeting = "Hello, World!"
print greeting
-- get user input
print "What's your name"
name <- getLine
print "Hello " ++ name
- operators
True && False
False || True
1 == 2
1 /= 2 -- not equal
2 > 1
1 <= 2
2 - 1
2 + 1
2 * 3
5 / 2 -- 2.5
6 / 2 -- 3.0
mod 5 2
div 5 2 -- 2
2 ^ 3
ceiling 3.4
floor 3.4
round 3.4
round 3.5
max 2 4
min 2 4
- add
-- func is just a var, no special identifier, man I like it!
add :: Int -> Int -> Int -- type signature, optional
add x y = x + y
add 1 2 -- call the function
- partial application
increase = add 1
increase 2
convention: haskell file extension is .hs
create a file named main.hs
:
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n - 1)
sumList :: [Int] -> Int
sumList [] = 0
sumList (x:xs) = x + sumList xs
findMax :: [Int] -> Int
findMax [] = error "empty list"
findMax [x] = x
findMax (x:xs) = max x (findMax xs) -- max is a built-in function
main = do
print (factorial 5)
-- using $ to avoid parentheses
print $ sumList [1, 2, 3, 4, 5]
print $ findMax [1, 2, 3, 4, 5]
compile it:
ghc -o main main.hs
It'll generate 3 files:
main
: executablemain.hi
: intermediate file (like a header/declaration file?)main.o
: object file (for linking)
In Haskell's official website, there's a list of books and tutorials:
This is the book I'm reading: Learn You a Haskell for Great Good!
Learning from this book will be covered in basic-topics
folder.