Author: Harald Schilly
Views : 746

What I learned in 30 minutes

In [1]:
4 + 5 + 1

10

That above is an infix function, but you can also define your own function like that:

In [2]:
add a b = a + b

In [3]:
add 2 2

4
In [4]:
99 add 991

1090

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.

In [5]:
foldl add 0 [1..100]

5050

Here, the tutorial shows me how to define the infamous fib function … otherwise is actually just true and hence always the last fallthrough case.

In [6]:
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)

In [7]:
fib 5

8

Use show to print a string and "."-combine this via a $-sign (for lowering the precedence) to apply the argument to the fib function. In [8]: show . fib$ 0
show . fib $1 show . fib$ 2

"1"
"1"
"2"

map applies a function over a list -- where the .. create it in a lazy fashion ...

In [9]:
map fib [0..20]

[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946]
In [ ]:



## Linear Algebra via hmatrix

In [10]:
import Numeric.LinearAlgebra

a = (2><2)
[ 1, 2
, 3, 4 ] :: Matrix R

b = (2><1)
[ 5
, 6 :: R ]

In [11]:
disp 3 a

2x2 1 2 3 4
In [12]:
disp 3 b

2x1 5 6
In [13]:
let x = linearSolve a b
x

Just (2><1) [ -3.9999999999999987 , 4.499999999999999 ]
In [14]:
let c = (5><3) [1..] :: Matrix R
c

(5><3) [ 1.0, 2.0, 3.0 , 4.0, 5.0, 6.0 , 7.0, 8.0, 9.0 , 10.0, 11.0, 12.0 , 13.0, 14.0, 15.0 ]
In [15]:
let (u,s,v) = svd c

In [16]:
disp 3 u

5x5 -0.101 0.768 0.614 -0.105 0.110 -0.249 0.488 -0.696 0.185 0.426 -0.396 0.208 -0.274 -0.453 -0.721 -0.543 -0.072 0.180 0.770 -0.274 -0.690 -0.352 0.176 -0.397 0.460
In [ ]:



## Installed Libraries

In [17]:
import System.Process

callCommand "cabal list --installed"