What I learned in 30 minutes
4 + 5 + 1
That above is an infix function, but you can also define your own function like that:
add a b = a + b
add 2 2
99 `add` 991
Reduce operations (reduce()
in python, etc.) are foldl
.
This function takes the first function, the starting value, and folds the function by adding it from the left.
foldl add 0 [1..100]
Here, the tutorial shows me how to define the infamous fib
function … otherwise
is actually just true
and hence always the last fallthrough case.
fib x | x < 2 = 1 | otherwise = fib (x - 1) + fib (x - 2)
5-th fibonacci number is 8 (well, we’re the cool guys, and count from 0)
fib 5
Use show
to print a string and “.”-combine this via a $
-sign (for lowering the precedence) to apply the argument to the fib
function.
show . fib $ 0 show . fib $ 1 show . fib $ 2
map
applies a function over a list – where the ..
create it in a lazy fashion …
map fib [0..20]
import Numeric.LinearAlgebra a = (2><2) [ 1, 2 , 3, 4 ] :: Matrix R b = (2><1) [ 5 , 6 :: R ]
disp 3 a
disp 3 b
let x = linearSolve a b x
let c = (5><3) [1..] :: Matrix R c
let (u,s,v) = svd c
disp 3 u
import System.Process callCommand "cabal list --installed"