Sharedhaskell.ipynbOpen in CoCalc

Welcome Haskell on CoCalc

https://www.haskell.org/

What I learned in 30 minutes

4 + 5 + 1
10

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

add a b = a + b
add 2 2
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.

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.

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
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.

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 ...

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]

Linear Algebra via hmatrix

import Numeric.LinearAlgebra

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

b = (2><1)
 [ 5
 , 6 :: R ]
disp 3 a
2x2 1 2 3 4
disp 3 b
2x1 5 6
let x = linearSolve a b
x
Just (2><1) [ -3.9999999999999987 , 4.499999999999999 ]
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 ]
let (u,s,v) = svd c
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

Installed Libraries

import System.Process

callCommand "cabal list --installed"
* Cabal Default available version: [ Not available from any configured repository ] Installed versions: 1.22.5.0 Homepage: http://www.haskell.org/cabal/ License: BSD3 * GLURaw Default available version: [ Not available from any configured repository ] Installed versions: 1.5.0.1 Homepage: http://www.haskell.org/haskellwiki/Opengl License: BSD3 * GLUT Default available version: [ Not available from any configured repository ] Installed versions: 2.7.0.1 Homepage: http://www.haskell.org/haskellwiki/Opengl License: BSD3 * HTTP Default available version: [ Not available from any configured repository ] Installed versions: 4000.2.23 Homepage: https://github.com/haskell/HTTP License: BSD3 * HUnit Default available version: [ Not available from any configured repository ] Installed versions: 1.3.0.0 Homepage: http://hunit.sourceforge.net/ License: BSD3 * ObjectName Default available version: [ Not available from any configured repository ] Installed versions: 1.1.0.1 Homepage: https://github.com/svenpanne/ObjectName License: BSD3 * OpenGL Default available version: [ Not available from any configured repository ] Installed versions: 2.12.0.1 Homepage: http://www.haskell.org/haskellwiki/Opengl License: BSD3 * OpenGLRaw Default available version: [ Not available from any configured repository ] Installed versions: 2.4.1.0 Homepage: http://www.haskell.org/haskellwiki/Opengl License: BSD3 * QuickCheck Default available version: [ Not available from any configured repository ] Installed versions: 2.8.1 Homepage: https://github.com/nick8325/quickcheck License: BSD3 * StateVar Default available version: [ Not available from any configured repository ] Installed versions: 1.1.0.2 Homepage: https://github.com/haskell-opengl/StateVar License: BSD3 * array Default available version: [ Not available from any configured repository ] Installed versions: 0.5.1.0 License: BSD3 * async Default available version: [ Not available from any configured repository ] Installed versions: 2.0.2 Homepage: https://github.com/simonmar/async License: BSD3 * attoparsec Default available version: [ Not available from any configured repository ] Installed versions: 0.13.0.1 Homepage: https://github.com/bos/attoparsec License: BSD3 * base Default available version: [ Not available from any configured repository ] Installed versions: 4.8.2.0 License: BSD3 * bin-package-db Default available version: [ Not available from any configured repository ] Installed versions: 0.0.0.0 License: BSD3 * binary Default available version: [ Not available from any configured repository ] Installed versions: 0.7.5.0 Homepage: https://github.com/kolmodin/binary License: BSD3 * bytestring Default available version: [ Not available from any configured repository ] Installed versions: 0.10.6.0 Homepage: https://github.com/haskell/bytestring License: BSD3 * case-insensitive Default available version: [ Not available from any configured repository ] Installed versions: 1.2.0.5 Homepage: https://github.com/basvandijk/case-insensitive License: BSD3 * containers Default available version: [ Not available from any configured repository ] Installed versions: 0.5.6.2 License: BSD3 * deepseq Default available version: [ Not available from any configured repository ] Installed versions: 1.4.1.1 License: BSD3 * directory Default available version: [ Not available from any configured repository ] Installed versions: 1.2.2.0 License: BSD3 * fgl Default available version: [ Not available from any configured repository ] Installed versions: 5.5.2.3 License: BSD3 * filepath Default available version: [ Not available from any configured repository ] Installed versions: 1.4.0.0 Homepage: https://github.com/haskell/filepath#readme License: BSD3 * ghc Default available version: [ Not available from any configured repository ] Installed versions: 7.10.3 Homepage: http://www.haskell.org/ghc/ License: BSD3 * ghc-prim Default available version: [ Not available from any configured repository ] Installed versions: 0.4.0.0 License: BSD3 * hashable Default available version: [ Not available from any configured repository ] Installed versions: 1.2.3.3 Homepage: http://github.com/tibbe/hashable License: BSD3 * haskeline Default available version: [ Not available from any configured repository ] Installed versions: 0.7.2.1 Homepage: http://trac.haskell.org/haskeline License: BSD3 * haskell-src Default available version: [ Not available from any configured repository ] Installed versions: 1.0.2.0 License: BSD3 * hoopl Default available version: [ Not available from any configured repository ] Installed versions: 3.10.0.2 Homepage: http://ghc.cs.tufts.edu/hoopl/ License: BSD3 * hpc Default available version: [ Not available from any configured repository ] Installed versions: 0.6.0.2 License: BSD3 * html Default available version: [ Not available from any configured repository ] Installed versions: 1.0.1.2 License: BSD3 * integer-gmp Default available version: [ Not available from any configured repository ] Installed versions: 1.0.0.0 License: BSD3 * mtl Default available version: [ Not available from any configured repository ] Installed versions: 2.2.1 Homepage: http://github.com/ekmett/mtl License: BSD3 * network Default available version: [ Not available from any configured repository ] Installed versions: 2.6.2.1 Homepage: https://github.com/haskell/network License: BSD3 * network-uri Default available version: [ Not available from any configured repository ] Installed versions: 2.6.0.3 Homepage: https://github.com/haskell/network-uri License: BSD3 * old-locale Default available version: [ Not available from any configured repository ] Installed versions: 1.0.0.7 License: BSD3 * old-time Default available version: [ Not available from any configured repository ] Installed versions: 1.1.0.3 License: BSD3 * parallel Default available version: [ Not available from any configured repository ] Installed versions: 3.2.1.0 License: BSD3 * parsec Default available version: [ Not available from any configured repository ] Installed versions: 3.1.9 Homepage: https://github.com/aslatter/parsec License: BSD3 * pretty Default available version: [ Not available from any configured repository ] Installed versions: 1.1.2.0 Homepage: http://github.com/haskell/pretty License: BSD3 * primitive Default available version: [ Not available from any configured repository ] Installed versions: 0.6.1.0 Homepage: https://github.com/haskell/primitive License: BSD3 * process Default available version: [ Not available from any configured repository ] Installed versions: 1.2.3.0 License: BSD3 * random Default available version: [ Not available from any configured repository ] Installed versions: 1.1 License: BSD3 * regex-base Default available version: [ Not available from any configured repository ] Installed versions: 0.93.2 Homepage: http://sourceforge.net/projects/lazy-regex License: BSD3 * regex-compat Default available version: [ Not available from any configured repository ] Installed versions: 0.95.1 Homepage: http://sourceforge.net/projects/lazy-regex License: BSD3 * regex-posix Default available version: [ Not available from any configured repository ] Installed versions: 0.95.2 Homepage: http://sourceforge.net/projects/lazy-regex License: BSD3 * rts Default available version: [ Not available from any configured repository ] Installed versions: 1.0 License: BSD3 * scientific Default available version: [ Not available from any configured repository ] Installed versions: 0.3.4.4 Homepage: https://github.com/basvandijk/scientific License: BSD3 * split Default available version: [ Not available from any configured repository ] Installed versions: 0.2.2 License: BSD3 * stm Default available version: [ Not available from any configured repository ] Installed versions: 2.4.4.1 License: BSD3 * syb Default available version: [ Not available from any configured repository ] Installed versions: 0.6 Homepage: http://www.cs.uu.nl/wiki/GenericProgramming/SYB License: BSD3 * template-haskell Default available version: [ Not available from any configured repository ] Installed versions: 2.10.0.0 License: BSD3 * terminfo Default available version: [ Not available from any configured repository ] Installed versions: 0.4.0.1 Homepage: https://github.com/judah/terminfo License: BSD3 * text Default available version: [ Not available from any configured repository ] Installed versions: 1.2.2.0 Homepage: https://github.com/bos/text License: BSD3 * tf-random Default available version: [ Not available from any configured repository ] Installed versions: 0.5 License: BSD3 * time Default available version: [ Not available from any configured repository ] Installed versions: 1.5.0.1 Homepage: https://github.com/haskell/time License: BSD3 * transformers Default available version: [ Not available from any configured repository ] Installed versions: 0.4.2.0 License: BSD3 * unix Default available version: [ Not available from any configured repository ] Installed versions: 2.7.1.0 Homepage: https://github.com/haskell/unix License: BSD3 * unordered-containers Default available version: [ Not available from any configured repository ] Installed versions: 0.2.5.1 Homepage: https://github.com/tibbe/unordered-containers License: BSD3 * vector Default available version: [ Not available from any configured repository ] Installed versions: 0.11.0.0 Homepage: https://github.com/haskell/vector License: BSD3 * xhtml Default available version: [ Not available from any configured repository ] Installed versions: 3000.2.1 Homepage: https://github.com/haskell/xhtml License: BSD3 * zlib Default available version: [ Not available from any configured repository ] Installed versions: 0.6.1.1 License: BSD3