20-CS-4003-001 Organization of Programming Languages Fall 2018
Functions

Lambda calculus, Type theory, Formal semantics, Program analysis

Prev     Next     All lectures           Code    More Code

Lambda - Anonymous Functions

 f1::Int -> Intf1 = (\x -> x+1) - (f1 10) is 11((\x -> x+1) 10) is 11 f2::[Int -> Int]f2 = [ (\x -> x+i) | i <- [1..10] ] - f2 is a list of procedures: Integer input, Integer outputith procedure adds i+1 to the input argument e.g. ((f2 !! 0) 3) v1::[Int]v1 = [ (g i) | g <- f2 | i <- [1..10] ] - v1 is [2,4,6,8,10,12,14,16,18,20] v2::[Int]v2 = [ (g i)     | g <- [ (\x -> x+k) | k <- [1..10] ]     | i <- [1..10] ] - v2 is [2,4,6,8,10,12,14,16,18,20] f3::[[Int] -> [Int -> Int]]f3 = [ (\x -> [ (\y -> y+i) | i <- x ])     | j <- [1..10] ] - f3 is a list of procedures that map lists of integers to procedures that map integers to integers e.g. (((f3 !! 0) [1,2,3]) !! 0) 3 v3a::[Int] -> [Int -> Int]v3a = (f3 !! 0) - v3a is a procedure that maps a list of integers to a list of procedures that map integers to integers v3b::[Int -> Int]v3b = (f3 !! 0) ([1,2,3]::[Int]) - v3b is a list of procedures that map an integer to an integer v3c::Int -> Intv3c = ((f3 !! 0) ([1,2,3]::[Int]) !! 0) - v3c is a procedure that maps an integer to an integer v3d::Intv3d = (v3c 10) - v3d is 11 f4::(t1 -> t) -> t1 -> tf4 = (\x -> (\y -> (x y))) - f4 is a curried function that returns a procedure of one argument f5::[t] -> tf5 = (f4 last) - f5 returns the last element of a list(f5 [1,2,3]) is 3 f6::[a] -> [a]f6 = (f4 tail) - f6 returns the tail of a list(f6 [1,2,3]) is [2,3] f7::[t] -> tf7 = (f4 head) - f7 returns the head of a list(f7 [1,2,3]) is 1 append::[t] -> [t] -> [t]append = (f4 head) - f7 returns the head of a list(f7 [1,2,3]) is 1 f8::(t1 -> t2 -> t) -> t1 -> t2 -> t(\x -> (\y z -> (x y z))) - f8 returns a procedure of two arguments f9::[t] -> [t] -> [t]f9 = (f8 append) - f9 appends two lists(f9 [1,2,3] [4,5,6]) is [1,2,3,4,5,6] plus::Int -> Int -> Intplus x y = x + y - plus returns a procedure that adds two numbers f10::Int -> Int -> Intf10 = (f8 plus) - f10 adds two numbers(f10 23 34) is 57