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

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures           Code

Example: binary search

bs x lst low high = pp
    mid = (div (high+low) 2)
    pp = if low > high then
         else if x == (lst !! mid) then
         else if x <  (lst !! mid) then
            bs x lst low (mid-1)
            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.