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

Lambda calculus, Type theory, Formal semantics, Program analysis

Prev     Next     All lectures           Code

On the way to Topological Sort

 ```remdups::Eq a => [a] -> [a] remdups x = y where y = if (x == []) then [] else (x !! 0) : remdups (removenum (x !! 0) (drop 1 x)) ``` - Input is a list x of numbers. Removes all duplicate occurrences of any number appearing in x, keeping the leftmost occurrence only. If x is [] then [] is returned. Otherwise, the first element of x is placed at the head of the output list, x is removed from the remaining elements of x and remdups is applied to that result. ```flatten::Eq a => [[a]] -> [a] flatten lst = y where y = if (lst == []) then [] else (lst !! 0) ++ (flatten (drop 1 lst)) ``` - The input list lst is a list of lists of atomic tokens. For example ``` [[],[6],[2,7],[6],[1,6],[],[4,5]]::[[Int]] ``` Returns a list of atoms all of which are in lst. The order of the atoms in the output list is the same as the order of elements in lst. For example, ``` ghci> flatten [[],[6],[2,7],[6],[1,6],[],[4,5]] [6,2,7,6,1,6,4,5] ghci> remdups (flatten [[],[6],[2,7],[6],[1,6],[],[4,5]]) [6,2,7,1,4,5] ```