lec6-2x2

lec6-2x2 - Todays Plan CSE 130 Fall 2011 Programming...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 130 : Fall 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions Thursday, October 13, 2011 Today’s Plan • A little more practice with recursion – Base Pattern -> Base Expression – Induction Pattern -> Induction Expression • Higher-Order Functions – or, why “take” and “return” functions ? Thursday, October 13, 2011 Recursion • A way of life • A different way to view computation – Solutions for bigger problems – From solutions for sub-problems Why know about it ? 1. Often far simpler, cleaner than loops – But not always… 2. Forces you to factor code into reusable units – Only way to “reuse” loop is via cut-paste Thursday, October 13, 2011 Example : Factorial let rec fac n = if n=0 then 1 else n * fac (n-1) ;; Base Expression Inductive Expression Induction Condition Thursday, October 13, 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
“last thing” function does is a recursive call Tail Recursion NOT TR let rec fac n = if n=0 then 1 else n * fac (n-1) ;; bad because height of stack = O(n) Thursday, October 13, 2011 “last thing” function does is a recursive call NOT TR let rec fac n = if n=0 then 1 else n * fac (n-1) ;; bad because height of stack = O(n) Thursday, October 13, 2011 “last thing” function does is a recursive call Tail Recursive Factorial let rec fac n = Thursday, October 13, 2011 Example : List Maximum let rec listMax xs = match xs with | [] -> 0 | x::xs’ -> max x (listMax xs’) Base Expression Inductive Expression NOT TR bad because height of stack = O(#listsize) Thursday, October 13, 2011
Background image of page 2
Example : List Maximum let max x y = if x > y then x else y let listMax l = let rec helper cur l = match l with [] -> cur | h :: t -> helper (max cur h ) t in helper 0 l ;; Find maximum element in +ve int list … in a more ML-ish way Base Expression Inductive Expression Base pattern Ind. pattern Thursday, October 13, 2011 News • PA2 due tomorrow @ 5PM • PA3 goes up tomorrow – Due 10/21 • Midterm 11/1 – In class – Open book etc. – Practice materials on webpage Thursday, October 13, 2011 Today’s Plan • A little more practice with recursion – Base Pattern -> Base Expression – Induction Pattern -> Induction Expression • Higher-Order Functions – or, why “take” and “return” functions ?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/11/2011 for the course CSE 130 taught by Professor Jhala during the Fall '06 term at UCSD.

Page1 / 10

lec6-2x2 - Todays Plan CSE 130 Fall 2011 Programming...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online