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

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures           Code

Example: binary search

   
bs x lst low high = pp
  where
    mid = (div (high+low) 2)
    pp = if low > high then
            -1
         else if x == (lst !! mid) then
            mid
         else if x <  (lst !! mid) then
            bs x lst low (mid-1)
		   else
            bs x lst (mid+1) high

search x lst = bs x lst 0 (length lst)
 -  Search an ordered list of numbers for a given number and return its location in the list if it is found, otherwise return -1. This example shows direct recursion in Haskell and the use of an if-then-else construct.