Installing Haskell on your Mac
1 | $ brew install ghc |
Every example has to start with Hello, World!, the first example is using the Haskell REPL ( or interactive. mode )
1 2 3 4 5 6 7 | $ ghci GHCi, version 9.4.4: https://www.haskell.org/ghc/ :? for help ghci> main = putStrLn "Hello World" ghci> main Hello World ghci> :quit Leaving GHCi. |
The alternative, more traditional approach is to use the Haskell compiler to create a binary. In a file called hello.hs add the following text
1 | main = putStrLn "Hello, World!" |
Then compile and execute the binary
1 2 3 4 5 6 7 8 | $ ghc hello.hs [1 of 2] Compiling Main ( hello.hs, hello.o ) [2 of 2] Linking hello $ ls hello hello.hi hello.hs hell $ ./hello Hello, World! $ |
Basic Syntax:
In Haskell, a function is defined using the following syntax:
1 | functionName arg1 arg2 = expression |
For example, to define a function that adds two numbers, you could write:
1 | add x y = x + y |
You can also define a function that takes no arguments, like this:
1 | greeting = "Hello, World!" |
To call a function, you simply write the function name followed by the arguments in parentheses:
1 | result = add 2 3 |
This will set the variable “result” to 5.
1 2 3 4 5 6 7 8 | -- Single line comment {- Multi line comments span multiple lines -} {-# … #-}. -- Compiler pragma, more on this later |
1 | -- Numbers |
1 | -- Characters |
1 | -- Strings |
1 | -- Tuples |
1 | -- Lists |
1 | -- Functions |
1 | -- Modules |
1 | -- Input / Output |
1 | -- Functor |
1 | -- Monads |
1 | -- Zipper |
1 | -- Cabal Build & Package Manager |
1 2 3 | -- Haskell Libraries https://downloads.haskell.org/ghc/latest/docs/libraries/ |
1 | -- |
1 | -- |
1 | -- |
1 | -- |