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

   
removenum::Eq a => a -> [a] -> [a]
removenum p [] = []
removenum p (x:y) = 
   if (x == p) then removenum p y 
   else x:(removenum p y)
 -  Returns the input list (2nd argument) with occurrences of p removed. If the input list is empty, the empty list is returned. Otherwise, observe that due to the pattern (x:y), x is the head element in the input list and y is the tail of the list. Then, if p equals x, removenum p y is called, effectively removing that occurrence of x. But if p does not equal x then x is placed at the head of the list returned by removenum p y, effectively reinstating its presence in the list.
 
removeall::Eq a => a -> [[a]] -> [[a]]
removeall p y =
   if (y == [[]]) then 
      [[]]
   else if (tail y == []) then 
      [(removenum p (y !! 0))]
   else 
      [(removenum p (y !! 0))] ++ 
      removeall p (drop 1 y)
 -  The input list y is a list of lists of atomic tokens. For example
   [[],[6],[2,7],[6],[1,6],[],[4,5]]::[[Int]]
Input p is an atom or the instantiation of a function that produces an atom. Returns y with all occurrences of p removed. The action of this procedure is similar to that of removenum except that y !! 0 identifies the head list in y, removenum is used to remove occurrences of p from the head list, and the append operator ++ is used instead of : because lists are being conjoined.