Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Tuesday, 15 September 2015
( Haskell )
  • All Haskell functions are pure
    • Cannot modify state.. functions can’t write to local or global variable, can’t write to a file, or write to the command line
    • Cannot depend on state..function can’t read a file, or can any kind of user input
    • Given the same arguments, always returns the same output

Recursion – Power2

A function which calls itself

pow2 n = if n == 0 -- the base case of the recursion then 1 else 2 * (pow2 (n-1))

pow2 0 = 1
pow2 1 = 2
pow2 2 = 4
pow2 3 = 8

static void Main(string[] args) { // write a function that returns the pow2 // eg pow2 1 = 2 // pow2 2 = 4 // pow2 3 = 8 var result = Pow2(1); var result2 = Pow2(2); var result3 = Pow2(3); Console.WriteLine($"{result}, {result2}, {result3}"); } // Imperative programming we'd normally use a loop rather than recursion static int Pow2(int n) { var x = 1; for (var i = 0; i < n; i++) x *= 2; return x; }

There are no loops in Haskell, so recursion is used all the time.

Recursion – Repeat String

repeatString str n = if n == 0 then "" -- base case, return the empty string else str ++ (repeatString str (n-1))

and

static string RepeatString(string str, int n) { string result = ""; for (int i = 0; i < n; i++) result = result + str; return result; }

Systematically replace Loops with Recursive functions

--pow2 n = -- if n == 0 -- the base case of the recursion -- then 1 -- else 2 * (pow2 (n-1)) --static int Pow2(int n) --{ -- var x = 1; -- for (var i = 0; i < n; i++) -- x *= 2; -- return x; --} pow2 n = pow2loop n 1 0 pow2loop n x i = -- x = 1, i = 0 if i <n then pow2loop n (x*2) (i+1) -- main part of the 'loop' else x

Can even though it is more complicated than first recursive function it is useful to know can always convert a loop to a recursive function
--repeatString str n = -- if n == 0 -- then "" -- base case, return the empty string -- else str ++ (repeatString str (n-1)) strRepeat n str = strRepeatLoop n str 1 strRepeatLoop n str i = if i <n then str ++ strRepeatLoop n (str) (i+1) else str

I find the first version is easier to understand.

Lists

x = [1,2,3] empty = [] y = 0 : x -- [0,1,2,3] : is the cons operator -- takes an element, and a list, and returns a new list added to the front str = "abcde" z = "hello " ++ "world" -- a list has to have the same type a = head[1,2,3] --1 b = tail[1,2,3] --2,3 c = head (tail [1,2,3]) -- 2 d = null [] -- True. checking if the list is empty e = null [1,2] -- False. The list isn't empty -- nums is the list of numbers to be doubled double nums = if null nums then [] -- base case of the recursion else (2 * (head nums)) : (double (tail nums)) -- get first element of nums with head -- first element of new list should be double this -- cons operator to add to front of list f = double[1,2,3] -- returns [2,4,6]

and

removeOdd nums = if null nums -- base case of recursion then [] -- return empty list else if (mod (head nums) 2) == 0 -- even then (head nums) : (removeOdd (tail nums)) -- if first element is even, include in output list else removeOdd (tail nums) -- if first element is odd, dont' include in output list g = removeOdd[1,2,3,4,5,6,7,8] -- returns [2,4,6,8]

Actually there is a better way of writing this function in 1 line!

Tuples

Provide a convenient way of packing up a few values so they can be passed around

-- tuple with 2 different types - number and string a = (1, "hello") -- list of numbers b = [1,2,3] -- tuple containing a list c = ("pi", 3.14159, [1,2,3], "four") -- common technique to return multiple values from a fn headAndLength list = (head list, length list) -- headAndLength [1,2,3] returns (1,3) -- first element of the tuple.. output is 1 d = fst(1, "hello") -- second element of the tuple..output is "hello" e = snd(1, "hello")

Pattern Matching

Nothing like it in most imperative programming languages.

-- first element of a tuple fst' (a,b) = a f = fst' (2,3) -- second element of a tuple snd' (a,b) = b g = snd' (3,4) -- how the null function which tests whether a list is -- empty -- list matches the empty list pattern null' [] = True -- list is non empty..add some value x to the front -- of some list xs **why does this look like a tuple** -- cons operator null' (x : xs) = False -- list contains something h = null' [1,2] -- list contains nothing i = null' [] head' (x:xs) = x head' [] = error "head of empty list" j = head' [1,2] k = head' [] double nums = if null nums then [] else (2 * (head nums)) : (double (tail nums)) l = double [1,2,3] double' [] = [] -- first element is x, remaining element is list xs -- first element of new list should be 2 * x -- remaining elements are computed recursively double' (x : xs) = (2 * x) : (double xs) m = double' [1,2,3,4] -- guards

asdf

-- guards of function argument -- (can look at values in the data) pow2 n -- guarding boolean expression | n == 0 = 1 | otherwise = 2 * (pow2 (n-1)) o = pow2(2) removeOdd [] = [] -- uses guard to select the even removeOdd (x : xs) | mod x 2 == 0 = x : (removeOdd xs) | otherwise = removeOdd xs -- returns [2,4,6] p = removeOdd [1,2,3,4,5,6] -- case expressions.. inside the function double'' nums = case nums of -- pattern matching [] -> [] (x : xs) -> (2 * x) : (double xs) q = double'' [1,2,3] -- pattern match on the result of a function -- this function just returns a list anyEven nums = case (removeOdd nums) of [] -> False (x : xs) -> True r = anyEven [1,3,5] s = anyEven [1,2,4,6,8] -- let fancySeven = let a = 3 -- in a subexpress where the bound a can be used in (2 * a) + 1 t = fancySeven fancyNine = let x = 4 y = 5 in x + y u = fancyNine numEven nums = -- evenNums is a list just of the even nums let evenNums = removeOdd nums -- return the length of the list in length evenNums -- returns 2 v = numEven [1,2,3,4] -- returns 3 w = numEven [1,2,3,4,5,6] -- where fancySeven' = (2 * a) + 1 where a = 3 x = fancySeven' -- whitespace -- don't use tabs!!!! pairMax p = max (fst p) -- (snd needs to start further right than max (snd p) y = pairMax (5,6) -- lazy function evaluation! -- in java/C# -- foo(alpha(1), beta(2)) -- we know order of execution -- alpha(1) and beta(2) are computer -- then resulting values are passed to foo -- lazy infinite lists intsFrom n = n : (intsFrom (n+1)) -- all possible integers from 1 ints = intsFrom 1 -- returns false (as the infinite list is not null) aa = null ints -- returns 1 ab = head ints -- takes first 10 elements of the list ac = take 10 ints evenInts = removeOdd ints ad = take 10 evenInts

asdf

-- functions are another type of value like int or string -- Higher Order Functions -- functions which accept functions as an argument -- or return a function -- pass3 takes an argument f, which is a function -- and passes 3 to the function returning the result pass3 f = f 3 add1 x = x + 1 -- returns 4.. add1 3 = 4 b = pass3 add1 -- compose is a higher order function -- takes 2 functions f and g, and a value x -- passes the value (g of x) to the function f compose f g x = f (g x) mult2 x = 2 * x c = compose add1 mult2 4 --9 add1 (mult2 4) always7 x = 7 d = always7 5 -- 7 :-) -- Partial application -- int foo(int x, int y, int x) -- return x + y + y -- foo_1_ = foo(1,2) -- illegal in java foo x y z = x + y + z -- so foo 1_2 is just a function foo_1_2 = foo 1 2 e = foo_1_2 3 -- returns 6 -- defines a value x and a function f -- passes x to the function f pass x f = f x -- partially apply.. pass3' will be a function that -- takes a single argument which is the last remaining -- argument of pass pass3' = pass 3 --Operators and just functions -- +,*.. -- to pass as a function just (+) f = (+) 5 3 -- 8 -- pass operator to higher order fun -- pass_3_4 takes an argument pass_3_4 f = f 3 4 g = pass_3_4 (+) -- 7 -- defining a new operator -- 2 arguments to new operator are 2 pairs -- (a,b) and (c,d) -- patter match (a,b) .+ (c,d) = (a+c, b+d) -- Map -- applies a function to every element in a list h = map length ["hello", "abc", "1234"] -- [5,3,4] -- add 1 to every element in the list of numbers i = map (1+) [1,3,5,7] -- [2,6,4,8] -- partially applying map function -- only giving the funcion to use double = map (2*) j = double [2,3,4] -- [4,6,8] -- Filter -- define a function notNull xs = not (null xs) k = filter notNull ["", "abc", "", "hello", ""] -- ["abc","hello"] -- 1:20 of 2:48 filter
| | # 
# Monday, 14 September 2015
( Haskell )

image

Functions – just need a space

image

No commas between args

image

Do need parenthesis

Function definition

let square x = x * x

image

-- multiplies the largest of a and b by x multiMax a b x = (max a b) * x

postOrNeg x = if x >= 0 then "Postivie" else "Negative"

image

not sure why it fails!

| | # 
# Sunday, 13 September 2015
( Haskell )

What is FP?

Seems like opinions differ

  • Erik Meijer – FP mathematical functions
  • A style of programming in which the basic method of computation is the application of functions to arguments
  • A functional language is on that supports and encourages the functional style
  • FP – allows rapid prototyping
  • Provide powerful problem solving tools

https://channel9.msdn.com/Series/C9-Lectures-Erik-Meijer-Functional-Programming-Fundamentals/Lecture-Series-Erik-Meijer-Functional-Programming-Fundamentals-Chapter-1

Good history lesson!

Pure lazy functional programming.. idea is that you use ideas in daily programming (not programming in Haskell)

GHCi – repl

image 
:quit   - to quit

Editors

Sublime, Notepad++
EclipseFP
leksah (written in Haskell)

First Program – Hello World!

string1 = "hello" string2 = "world" greeting = string1 ++ " " ++ string2

ghci hello.hs
greeting

Haskell source file – doesn’t need the let statement

image

| | # 
# Friday, 12 December 2014
( Haskell )

http://www.ndcvideos.com/#/app/video/2971

Interesting…”Throw everything away you know about programming… “

http://learnyouahaskell.com/

“Haskell is an advanced purely-functional programming language.”

| | #