To install Haskell compiler on MacOS:
brew install ghc
To compile a Haskell file into executable: (the Haskell file must have a main function)
ghc my-haskell-with-main-func.hs
To run without a Haskell file compiling: (the Haskell file will execute without a main function)
ghc -e ':script my-haskell-without-main-func-each-line-is-a-command.hs'
Haskell interactive console:
ghci
To output a string
putStrLn "hello"
To create a Hello World Haskell program
main = putStrLn "hello"
To create a multi-line Haskell program
main = do
putStrLn "hello"
putStrLn "World"
To get stdin to a variable
t <- getLine
To cast a String value into Int
i = read "123" :: Int
To covert a Int to String
s = show i
To round Float to Int
i = round 1.5
To split a String to a List by space
wordList = words "a b c"
To get item by index from a List
wordList !! 2
To split a String to a List by new lines
lineList = lines "hello world\nhi there"
To print a variable
print "hello"
To create a list of integer sequence from 1 to 5
[1..5]
To create a list of integers
[1,2,3]
To define a lambda function
foo = \x -> x+1
To call a lambda function
foo 1
To map a List and apply a lambda function
map (\x -> x+1) [1 .. 2]
To filter a List and apply a lambda function
filter (\x -> x>0) [1,2,3,0,5,6]
To take unique values of a list
import Data.List
nub [1,2,2,3,3,4]
To sort a List
sort [2,3,1]
To compose functions, use .
(map (\x -> x+1) . nub) [1,2,2,2,2]
To pipe functions from right to left without compose, use $
map (\x -> x+1) $ nub [1,2,2,2,2]
To flatten / merge multiple Lists into one List
concat [[1,2],[3,4]]
To join a List of String to one String with a delimiter
intercalate " " ["a", "b", "c"]
To reduce, use foldr
foldr (\x s -> s+x) 100 [1,2,3]
foldr is kind of slow. Use foldl
foldl (\s v -> s+v) 100 [1,2,3]
To sum numbers in a List
sum [1,2,3]
To divide a List into a List of groups (Lists), each of which collects the repeated items in sequence:
groupBy (==) "aabbcccdddde"
Write a function that returns constant value
f = (const 1)
f 100
To get combination of of a list with itself (Note: a String is a List)
mapM (const "ABC") [1,1]
To comment
-- This is a comment
To take power of a number
x ** 2 -- will result in Float value
x ^ 2 -- will result in Int value
To take absolute value
abs x
To take modulo of two values (% doesn't work)
mod 5 3
To perform integer division
div 5 3
To compare values with not equals (!= doesn't work)
1 /= x
To get the max value in a List of values
maximum [1,2,3]
To concat two Strings
"Hello" ++ "World"
To concat two Lists
[1,2,3] ++ [4,5,6]
To reverse a List
reverse [1,2,3]
To take a for loop (
mapM_ (\i -> do {
print $ "hello" ++ show i
}) [1.. 5]
-- Note: what do {} is used, remember to add ; at the end of each line, except for the last line.
-- Note: use mapM_ for side effect, and expect mapM_ to return no value.
To use ternary operator
x = 2
y = if x > 1 then "YES" else "NO"
To define a variable in multi-line program
let { x = 2 };
To create a pair and get its left and right value, use a tuple:
a = (1,2)
v1 = fst a
v2 = snd a
To zip two Lists into a List of pairs (tuples)
zip [1,2,3] ['a','b','c']
To define a lambda function that takes a tuple as input parameter:
(\(a,b) -> a+b) (1,2)
Can I transpose a matrix? Yes, you can
import Data.List
transpose [[1,2],[3,4]]
Take length of a List
length [1,2,3]
Take the first few from a List
take 1 [1,2,3]
Remove the first few from a List
drop 1 [1,2,3]
Drop the last one from a List
init [1,2,3,4,5]
Get the first value in a List
head [1,2,3]
Get the last value in a List
last [1,2,3]
Traverse a List until it doesn't match a condition
takeWhile (\x -> x > 0) [1,2,3,0,5,6]
To get a String (List) of repeated values of a certain length
take 10 $ repeat '_'
To define a recursive function: (you can define what a function return when a certain value is received at the parameter.)
f 0 = 0
f 1 = 0
f 2 = 1
f n = f (n-1) + f (n-2)
To define a function f with multiple scenarios by conditions
f v
| v > 0 = ">0"
| v < 0 = "<0"
| otherwise = "0"
To dynamic programming with recursion, see a tutorial on Data.Function.Memoize for fibonacci.
For example https://gist.github.com/yuhanz/e1c6793d3e8cb39fac0fa0ab9685235a