CoCalc Public Fileshaskell.ipynbOpen with one click!
Author: Harald Schilly
Views : 1083

Welcome Haskell on CoCalc

https://www.haskell.org/

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"
* Cabal Synopsis: A framework for packaging Haskell software Default available version: 2.2.0.1 Installed versions: 1.24.2.0 Homepage: http://www.haskell.org/cabal/ License: BSD3 * array Synopsis: Mutable and immutable arrays Default available version: 0.5.2.0 Installed versions: 0.5.1.1 License: BSD3 * base Synopsis: Basic libraries Default available version: 4.11.1.0 Installed versions: 4.9.1.0 License: BSD-3-Clause * binary Synopsis: Binary serialisation for Haskell values using lazy ByteStrings Default available version: 0.8.6.0 Installed versions: 0.8.3.0 Homepage: https://github.com/kolmodin/binary License: BSD3 * bytestring Synopsis: Fast, compact, strict and lazy byte strings with a list interface Default available version: 0.10.8.2 Installed versions: 0.10.8.1 Homepage: https://github.com/haskell/bytestring License: BSD3 * containers Synopsis: Assorted concrete container types Default available version: 0.6.0.1 Installed versions: 0.5.7.1 License: BSD3 * deepseq Synopsis: Deep evaluation of data structures Default available version: 1.4.3.0 Installed versions: 1.4.2.0 License: BSD3 * directory Synopsis: Platform-agnostic library for filesystem operations Default available version: 1.3.3.1 Installed versions: 1.3.0.0 License: BSD3 * filepath Synopsis: Library for manipulating FilePaths in a cross platform way. Default available version: 1.4.2.1 Installed versions: 1.4.1.1 Homepage: https://github.com/haskell/filepath#readme License: BSD3 * ghc Synopsis: The GHC API Default available version: 8.4.3 Installed versions: 8.0.2 Homepage: http://www.haskell.org/ghc/ License: BSD3 * ghc-boot Synopsis: Shared functionality between GHC and its boot libraries Default available version: 8.4.3 Installed versions: 8.0.2 License: BSD3 * ghc-boot-th Synopsis: Shared functionality between GHC and the @[email protected] library Default available version: 8.4.3 Installed versions: 8.0.2 License: BSD3 * ghc-prim Synopsis: GHC primitives Default available version: 0.5.2.0 Installed versions: 0.5.0.0 License: BSD-3-Clause * ghci Synopsis: The library supporting GHC's interactive interpreter Default available version: 8.4.3 Installed versions: 8.0.2 License: BSD3 * haskeline Synopsis: A command-line interface for user input, written in Haskell. Default available version: 0.7.4.3 Installed versions: 0.7.3.0 Homepage: https://github.com/judah/haskeline License: BSD3 * hoopl Synopsis: A library to support dataflow analysis and optimization Default available version: 3.10.2.2 Installed versions: 3.10.2.1 Homepage: https://github.com/haskell/hoopl License: BSD3 * hpc Synopsis: Code Coverage Library for Haskell Default available version: 0.6.0.3 Installed versions: 0.6.0.3 License: BSD3 * integer-gmp Synopsis: Integer library based on GMP Default available version: 1.0.2.0 Installed versions: 1.0.0.1 License: BSD3 * pretty Synopsis: Pretty-printing library Default available version: 1.1.3.6 Installed versions: 1.1.3.3 Homepage: http://github.com/haskell/pretty License: BSD3 * process Synopsis: Process libraries Default available version: 1.6.4.0 Installed versions: 1.4.3.0 License: BSD3 * rts Default available version: [ Not available from any configured repository ] Installed versions: 1.0 License: BSD3 * template-haskell Synopsis: Support library for Template Haskell Default available version: 2.13.0.0 Installed versions: 2.11.1.0 License: BSD3 * terminfo Synopsis: Haskell bindings to the terminfo library. Default available version: 0.4.1.2 Installed versions: 0.4.0.2 Homepage: https://github.com/judah/terminfo License: BSD3 * time Synopsis: A time library Default available version: 1.9.2 Installed versions: 1.6.0.1 Homepage: https://github.com/haskell/time License: BSD3 * transformers Synopsis: Concrete functor and monad transformers Default available version: 0.5.5.0 Installed versions: 0.5.2.0 License: BSD3 * unix Synopsis: POSIX functionality Default available version: 2.7.2.2 Installed versions: 2.7.2.1 Homepage: https://github.com/haskell/unix License: BSD3 * xhtml Synopsis: An XHTML combinator library Default available version: 3000.2.2.1 Installed versions: 3000.2.1 Homepage: https://github.com/haskell/xhtml License: BSD3
In [ ]: