20-CS-4003-001 Organization of Programming Languages Fall 2017
Lists

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures           Code

Lists and List Operations

   
p1 = [1,2,11,8,3,9,10]  -  p1 is a list of 6 integers
p2 = p1 !! 2  -  p2 is 11 (show the third element starting the count at 1)
p3 = drop 2 p1  -  p3 is [11,8,3,9,10] (first two elements removed)
p4 = take 2 p1  -  p4 is [1,2] (all but first two elements removed)
p5 = last p1  -  p5 is 10 (last element of list)
p6 = head p1  -  p6 is 1 (first element of list)
p7 = init p1  -  p7 is [1,2,11,8,3,9] (all but last element of list)
p8 = -2 : p1  -  p8 is [-2,1,2,11,8,3,9,10] (-2 placed at head of list)
p9 = [1] : [[2],[3],[4]]  -  p9 is [[1],[2],[3],[4]] (object of same type enters the list)
p10 = [1] ++ [2,3,4,5]  -  p10 is [1,2,3,4,5] (append the list [2,3,4,5] to [1])
p11 = [1..10]  -  p11 is [1,2,3,4,5,6,7,8,9,10]
p12 = [1..]  -  p12 is the infinite list [1,2,...] (haskell tries to print it all!!!)
p13 = [1,4..10]  -  p13 is [1,4,7,10]
p14 = length p1  -  p13 is 7 (number of elements in 1st level of list)
p15 = [null p1, null []]  -  p15 is [False,True] (check whether list is empty)
p16 = reverse p1  -  p16 is [10,9,3,8,11,2,1] (reverse a list)
p17 = maximum p1  -  p17 is 11 (maximum in a list)
p18 = minimum p1  -  p18 is 1 (minimum in a list)
p19 = sum p1  -  p19 is 44 (sum of all numbers in a list)
p20 = product p1  -  p20 is 47520 (product of all numbers in a list)
p21 = [elem 11 p1, elem 7 p1]  -  p21 is [True,False] (is an object an element of a list)
p22 = cycle [1,2,3]  -  (take 10 p22) is [1,2,3,1,2,3,1,2,3,1] (repeat pattern forever)
p23 = repeat [1,2]  -  (take 5 p23) is [[1,2],[1,2],[1,2],[1,2],[1,2]] (repeat pattern forever)
p24 = replicate 3 [1,2]  -  p24 is [[1,2],[1,2],[1,2]] (repeat pattern set number of times)