-
using stack to compile/manage
-
type
stack ghcito get interactive session -
With emacs:
-
use
haskell-mode; install with melpa as explained in the ReadMe file forhaskell-mode -
use
inf-haskellfor interactive session: put(require 'inf-haskell)in.emacsand then, with a.hshaskell file loaded in a buffer, useC-c C-lto get interactive session with that module automatically loaded. -
At command line, used
stack install intero; added the emacs melpa packageintero, and then added the following to my~/.emacsfile:(add-hook 'haskell-mode-hook 'intero-mode) (defun haskell-mode-setup () (setq haskell-process-type 'stack-ghci)) (add-hook 'haskell-mode-hook 'interactive-haskell-mode) (add-hook 'haskell-mode-hook 'haskell-mode-setup) (require 'inf-haskell)
-
-
Use
:l myfile.hsto load a file -
Use
:t objectto get the type of an object
-- comments follow two hyphens
6 /= 3 -- not equal
-- define a function
doubleMe x = x + x
-- string = list of characters
['c', 'a', 't'] == "cat"
-- : operator to add to beginning of list
'c' : ['a', 't']
-- ++ operator to join two lists
['c'] ++ ['a', 't']-- useful functions on lists
x = "karl has a cat"
head x
tail x
last x
init x
length x
null x
reverse x
take 5 x
drop 5 x
maximum x
minimum x
sum [3..10] -- integers 3 to 10
product [8,10..20] -- even numbers 8 to 20
'k' `elem` x -- contained in?-- list comprehensions
[t*3 | t <- [5..10]]
[t*3 | t <- [5..14], t `mod` 3 == 0]Recursive function example, using "patterns"; return -999 if input
is negative.
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = if n < 0
then error "n should be non-negative"
else (fib (n-1)) + (fib (n-2))