Docsity

Function Types - Organization of Programming Languages - Lecture Notes, Study notes of Programming Languages

Partial preview of the text

Download Function Types - Organization of Programming Languages - Lecture Notes and more Study notes Programming Languages in PDF only on Docsity!

Today ...

  • Exam overview
  • Function types (cont)
  • Recursion and Haskell

Reminder ...

  • Exam 1 Thursday (Feb. 7th)
  • HW 3 due Tuesday
  • Quiz 3 due Thursday

Exam Overview

Basics:

  • about 6–8 questions
  • closed book, notes, etc.
  • worth 623 % of grade

Topics:

  • everything we’ve discussed in class to this point (including today)
  • Haskell features (e.g., static typing, type inference, etc.)
  • bindings
  • basic types and typeclasses
  • lists and tuples
  • functions and function types
  • possibly some questions on reading

Parentheses in function types

Parentheses are important in function types

Prelude> any even [1, 3 .. 11] False

Prelude> :type any any :: (a -> Bool) -> [a] -> Bool

  • The first argument is a function from a to Bool

For example

Prelude> let any1 = any 1 ... No instance for (Num (a -> Bool)) arrising from the the literal ’1’

  • Why doesn’t this work? ... 1 is not a function from a to Bool

Another partial function application example

Prelude> :type even even :: (Integral a) => a -> Bool

Prelude> let anyEven = any even

Prelude> :type anyEven anyEven :: [Integer] -> Bool

Prelude> :type any even any even :: (Integral a) => [a] -> Bool

Recursion

Basic idea:

  • Solve a problem by breaking the problem into smaller subproblems
  • Where the subproblems are of exactly the same type as the bigger problem

Sum the elements of a list

  1. The sum of the empty list is 0 ... “Base Case”
  2. The sum of a non-empty list is first elem plus sum of remaining list

One way to write this in Haskell mySum xs = if null xs then 0 else head xs + mySum (tail xs)

Why recursion?

  • Just as powerful as iteration (e.g., for/while loops)
  • Often much easier to write, read, and understand with recursion
    • e.g., the “natural” way to define functions in mathematics

Iteration in Haskell is performed through recursion

  • this means you’ll become a recursion expert!

Examples

Drop

Recall that drop n xs returns a list with first n elems of xs removed

Prelude> drop 2 "foobar" "obar"

Prelude> drop 4 "foobar" "ar"

Prelude> drop 4 [1,2] []

Prelude> drop 0 [1, 2] 0

Prelude> drop 7 [] []

Prelude> drop (-2) "foo" "foo"

Q: What are the base cases?

  • Empty list (nothing to drop)
  • n ≤ 0 (nothing to drop)

Q: How can we define our own version of drop?

myDrop n xs = if n <= 0 || null xs then xs else myDrop (n-1) (tail xs)

  • if either base case, return list
  • otherwise, call myDrop on smaller n and smaller xs

Q: What is the type of myDrop?

  • Hint 1: (<=) :: (Ord a) => a -> Bool
  • Hint 2: (-) :: (Num a) => a -> a -> a

myDrop :: (Num a, Ord a) => a -> [b] -> [b]

Another example ...

append [1] (tail [2,3,4]) ⇒ 1 : append [] (tail [2,3,4]) ⇒ 1 : (tail [2,3,4])

  • The result isn’t evaluated ... because of lazy evaluation
  • This result is called a “thunk” (aka “suspension” or “delayed computation”; similar to a “future”)

Binary Search

Binary search is a classic example of recursion ...

  • We’ll use quot for “integer division” (e.g., 3 quot 2 ⇒ 1)
  • We’ll also use (!!) for list index (e.g., xs !! 0 returns first elem) binSearch x ys = let mid = (length ys) quot 2 in if null ys then False else if x == (ys !! mid) then True else if x < (ys !! mid) then binSearch x (take mid ys) else binSearch x (drop (mid+1) ys)
  • Note: Better off doing linear search here ... why?
    • Because (!!) is a recursive function (and so O(n))
    • Alternatively, can use arrays