## Learning to Code // a better name is a WIP.

possibly: How to work from anywhere / pants optional!

I am assuming that by teaching someone else (you) that I will also be learning. Lernen durch Lehren is a fancy french term for this process.

What the heck is this?

This is a Jupyter Notebook. It is the evolution of IPython notebook project. It is used primarily for Data-science. You will be able to run your own code an correct all the typos you find.

We will be learning Haskell as your first language. Haskell is kind of the grand daddy of the ML (not machine-learning) family of programming languages. The IHaskell kernel allows us to use a subset of Haskell in the notebook.

The help menu items above gives you all the info you need to start using notebooks.



In [1]:

-- this is a code cell- see the In []: to the left.
-- comments like these are ignored by the compiler.
-- to evaluate the code below "Hello..." select this cell by clicking
-- inside this gray area.
-- now hold down the **ctrl** key and hit the **enter** key.

"Hello from IHaskell"   -- this line of code should run.

--try it






1. in the empty cell below
2. enter 40 + 2 or any other simple math thing you would like
3. then evaluate it with 'Ctrl+Enter'
4. for some reason programmers like the number 42.


In [ ]:



This is little more interesting...

Lets define the Fibonacci sequence



In [ ]:

import Data.Char   --only need the print SWEET!

fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)

-- notice how the function name 'fib' is used in defining the function.

"The fibonacci of 20 is..."

fib 20     -- this will run the function

-- select the cell by clicking it and then Ctrl+Enter to run it.


Find another example of Fibonacci numbers and run it below.


In [ ]:

-- my fib function:


Lets look at a *type*


In [ ]:

:type fib   -- evaluate this the same way here.
-- lets see what a type definition looks like.



The Haskell programming language in based on lambda calculus.

It is a typed lambda calculus.

Types are quite important.

Here is a little deep shit stuff.

Type inference (the compiler infers the types for you) is done by the compiler by following simple typing rules.

This one is for function application.

It states that if f is a function that maps arguments of type A to results of type B , and e is an expression of type A , then the application f.e has type B :

$\frac{f\ ::\ A\ \rightarrow\ B\ \quad e\ ::\ A}{f\ e\ ::\ B}$

Summary

Functional programming is writing expressions that include values and can be applied to arguments and reduced into a new value.

All functions take just one argument and return one result. That result can also be another function. This is called currying.

Functions are a mapping of inputs to outputs. And they will ALWAYS return the same output/ given the same inputs. /not the case for all programming languages.

Haskell is a typed lambda calculus.



In [3]:

let myNum = 1 :: Integer
let myVal f = f + myNum

:t myVal
:t myNum




/* Styles used for the Hoogle display in the pager */
.hoogle-doc {
display: block;
}
.hoogle-code {
display: block;
font-family: monospace;
white-space: pre;
}
.hoogle-text {
display: block;
}
.hoogle-name {
color: green;
font-weight: bold;
}
font-weight: bold;
}
.hoogle-sub {
display: block;
margin-left: 0.4em;
}
.hoogle-package {
font-weight: bold;
font-style: italic;
}
.hoogle-module {
font-weight: bold;
}
.hoogle-class {
font-weight: bold;
}
.get-type {
color: green;
font-weight: bold;
font-family: monospace;
display: block;
white-space: pre-wrap;
}
.show-type {
color: green;
font-weight: bold;
font-family: monospace;
margin-left: 1em;
}
.mono {
font-family: monospace;
display: block;
}
.err-msg {
color: red;
font-style: italic;
font-family: monospace;
white-space: pre;
display: block;
}
#unshowable {
color: red;
font-weight: bold;
}
.err-msg.in.collapse {
}
.highlight-code {
white-space: pre;
font-family: monospace;
}
.suggestion-warning {
font-weight: bold;
color: rgb(200, 130, 0);
}
.suggestion-error {
font-weight: bold;
color: red;
}
.suggestion-name {
font-weight: bold;
}
myVal :: Integer -> Integer

/* Styles used for the Hoogle display in the pager */
.hoogle-doc {
display: block;
}
.hoogle-code {
display: block;
font-family: monospace;
white-space: pre;
}
.hoogle-text {
display: block;
}
.hoogle-name {
color: green;
font-weight: bold;
}
font-weight: bold;
}
.hoogle-sub {
display: block;
margin-left: 0.4em;
}
.hoogle-package {
font-weight: bold;
font-style: italic;
}
.hoogle-module {
font-weight: bold;
}
.hoogle-class {
font-weight: bold;
}
.get-type {
color: green;
font-weight: bold;
font-family: monospace;
display: block;
white-space: pre-wrap;
}
.show-type {
color: green;
font-weight: bold;
font-family: monospace;
margin-left: 1em;
}
.mono {
font-family: monospace;
display: block;
}
.err-msg {
color: red;
font-style: italic;
font-family: monospace;
white-space: pre;
display: block;
}
#unshowable {
color: red;
font-weight: bold;
}
.err-msg.in.collapse {
}
.highlight-code {
white-space: pre;
font-family: monospace;
}
.suggestion-warning {
font-weight: bold;
color: rgb(200, 130, 0);
}
.suggestion-error {
font-weight: bold;
color: red;
}
.suggestion-name {
font-weight: bold;
}
myNum :: Integer