Docsity

Recursion - Programming Language - Lecture Slides, Slides of Computer Engineering and Programming

Toggle sidebar
bg1
CSE 130 : Spring 2012
Programming Languages
Lecture 5: Recursion
1
Monday, April 16, 2012
News
On webpage:
•PA #1 (due Friday 4/20)
• In Class Exercises, bring laptops
2
Monday, April 16, 2012
“Recursive” types
What are values of nat ?
type nat = Zero | Succ of nat
Zero
Succ
Succ
Succ
...
One nat contains another!
nat = recursive type
3
Monday, April 16, 2012
Next: Building datatypes
Three key ways to build complex types/values
1. “Each-of” types t1#*#t2
Value of T contains value of T1 and a value of T2
2. “One-of” types type#t#=#C1#of#t1#|#C2#of#t2
Value of T contains value of T1 or a value of T2
3. “Recursive” type type#t#=#...|#C#of#(...*t)
Value of T contains (sub)-value of same type T
4
Monday, April 16, 2012
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Recursion - Programming Language - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

CSE 130 : Spring 2012

Programming Languages

Lecture 5: Recursion

Monday, April 16, 2012 1

News

On webpage:

  • PA #1 ( due Friday 4/20 )
  • In Class Exercises, bring laptops

Monday, April 16, 2012 2 “Recursive” types

What are values of nat?

type nat = Zero | Succ of nat

Zero

Succ

Succ Succ

One nat contains another!

nat = recursive type

Next: Building datatypes

Three key ways to build complex types/values
1. “Each-of” types t1 * t
Value of T contains value of T1 and a value of T
2. “One-of” types type t = C1 of t1 | C2 of t
Value of T contains value of T1 or a value of T
3. “Recursive” type type t = ...| C of (...*t)
Value of T contains (sub)-value of same type T

Next: Lets get cosy with Recursion

Recursive Code Mirrors Recursive Data

Monday, April 16, 2012 5

of_int : int -> nat let if (^) recn <= of_int 0 then n = else^ Zero Succ (of_int (n-1))

Base Expression Inductive Expression

Base pattern Inductive pattern

of_int 1 ====> Succ (of_int 0) ====> Succ (Zero)

of_int 0 ====> Zero

of_int 2 ====> Succ (of_int 1) ====> Succ (Succ (Zero)) Monday, April 16, 2012 6 to_int : nat -> int

|^ let Zero^ rec to_int-> 0 n^ =^ match^ n^ with nductive pattern^ Base pattern^ |^ Succ^ m^ ->^1 +^^ Base Expression to_int^ m Inductive Expression

to_int (Succ Zero) ====> 1 + to_int Zero ====> 1 + 0 ====> 1

to_int Zero ====> 0

to_int (Succ (Succ Zero)) ====> 1 + to_int (Succ Zero) ====> 1 + 1 ====> 2

plus: nat-> nat -> nat

plus (Succ Zero) (Succ (Succ Zero)) ====> Succ (plus Zero (Succ (Succ Zero)) ====> Succ (Succ (Succ Zero))

plus Zero (Succ (Succ Zero)) ====> Succ (Succ Zero)

let rec plus n m = |^ match Zero^ m^ with-> n | Succ m’ -> Succ (plus n m’)

Base Expression Inductive Expression

Base pattern Inductive pattern

Next: Lets get cosy with Recursion

Recursive Code Mirrors Recursive Data

Monday, April 16, 2012 13

Lists are recursive types!

Think about this! What are values of int list?

type ‘a list = |^ NilCons of ‘a * ‘a list

Monday, April 16, 2012 14 Lists are recursive types!

Think about this! What are values of int list?

type ‘a list = |^ NilCons of ‘a * ‘a list

Nil

Nil

Lists are recursive types!

Think about this! What are values of int list?

type ‘a list = |^ NilCons of ‘a * ‘a list

Nil

Cons 3 ,

Cons(3,Nil) Nil

Lists are recursive types!

Think about this! What are values of int list?

type ‘a list = |^ NilCons of ‘a * ‘a list

Cons(2,Cons(3,Nil))

Nil

Cons 3 ,

Cons 2 ,

Cons(3,Nil) Nil

Monday, April 16, 2012 17

Lists are recursive types!

Think about this! What are values of int list?

type ‘a list = |^ NilCons of ‘a * ‘a list

Nil

Nil

Cons(3,Nil)

Cons 3 ,

Cons(2,Cons(3,Nil)) Cons 2 ,

Cons(1,Cons(2,Cons(3,Nil))) Cons 1 , Monday, April 16, 2012 18 Lists aren’t built-in!

Lists are a derived type: built using elegant core!
1. Each-of
2. One-of
3. Recursive
:: is just a pretty way to say “Cons”
[] is just a pretty way to say “Nil”

type ‘a list = |^ NilCons of ‘a * ‘a list Next: Lets get cosy with Recursion

Recursive Code Mirrors Recursive Data

Some functions on Lists : member

(* let val rec mem mem : x‘a ys -> = ‘a list -> bool*) match | [] ys with-> false | y::ys’ -> if x=y then true else mem x ys’

Base Expression Inductive Expression

Base pattern Ind pattern mem 2 (2::[]) ====> true mem 2 (1::2::[]) ====> mem 2 (2::[]) ====> true

Monday, April 16, 2012 25

Some functions on Lists : member

(* let val rec mem mem : x‘a ys -> = ‘a list -> bool*) match | [] ys with-> false | y::ys’ -> if x=y then true else mem x ys’

Base Expression Inductive Expression

Base pattern Ind pattern mem 4 [] ====> false mem 4 (2::[]) ====> mem 4 [] ====> false mem 4 (1::2::[]) ====> mem 4 (2::[]) ====> false Monday, April 16, 2012 26

Some functions on Lists : insert

(* let val rec insert member : x‘a xs -> = ‘a list -> ‘a list *) Base Expression Inductive Expression

Base pattern Ind pattern

Online

Some functions on Lists : append

let rec append xs ys = Base Expression Inductive Expression

Base pattern Ind pattern

  • Find the right induction strategy
    • Base case: pattern + expression
    • Induction case: pattern + expression

Well designed datatype gives strategy

Some functions on Lists : append

let rec append xs ys = Base Expression Inductive Expression

Base pattern Ind pattern

append [] (3::4::[]) ====> 3::4::[]

append (1::2::[]) (3::4::[]) ====> 1::2::(3::4::[]) ====> 1::2::3::4::[]

append [] [] ====> [] append (2::[]) (3::4::[]) ====> 2::(3::4::[]) ====> 2::3::4::[]

Monday, April 16, 2012 29

Some functions on Lists : clone

(* let val rec clone clone : nint x =-> ‘a -> ‘a list *)

Online

Monday, April 16, 2012 30

null, hd, tl are all functions …

Bad ML style: More than aesthetics!

Pattern-matching better than test-extract:

  • ML checks all cases covered
  • ML checks no redundant cases
  • …at compile-time:
    • fewer errors (crashes) during execution
    • get the bugs out ASAP!

Next: Lets get cosy with Recursion

Recursive Code Mirrors Recursive Data

Representing Trees

type | Leaf tree of = | Node of^ inttree*tree

Leaf

Leaf

Node

Node

Leaf

Node(Node(Leaf 1, Leaf 2), Leaf 3)

Monday, April 16, 2012 37

Representing Trees

type | Leaf tree of = | Node of^ inttree*tree

Leaf

Leaf

Node

Node

Leaf

Node(Node(Leaf 1, Leaf 2), Leaf 3)

Monday, April 16, 2012 38

Next: Lets get cosy with Recursion

Recursive Code Mirrors Recursive Data

sum_leaf: tree -> int

“Sum up the leaf values”. E.g.

let t0 = Node(Node(Leaf 1, Leaf 2), Leaf 3);;

sum_leaf t0 ;;

- : int = 6

type | Leaf tree of = | Node of^ inttree*tree

sum_leaf: tree -> int

let rec sum_leaf t =

Monday, April 16, 2012 41

type | Leaf tree of = | Node of^ inttree*tree

sum_leaf: tree -> int

let rec sum_leaf t =

Base pattern Inductive pattern

Monday, April 16, 2012 42

type | Leaf tree of = | Node of^ inttree*tree

sum_leaf: tree -> int

let rec sum_leaf t = |Leaf^ match nt with-> |Node(t1,t2)->

Base pattern nductive pattern

Base pattern nductive pattern

type | Leaf tree of = | Node of^ inttree*tree

sum_leaf: tree -> int

let rec sum_leaf t = |Leaf^ match nt with-> n |Node(t1,t2)-> sum_leaf^ Base Expression t1 + sum_leaf t Inductive Expression

Base pattern Inductive pattern

Base pattern Inductive pattern

Another Example: Calculator

type | Num expr of float= | Add of exprexpr | | SubMul ofof exprexprexpr*expr

Want an arithmetic calculator to evaluate expressions like:

  • 4.0 + 2.9 ====> 6.
  • 3.78 – 5.92 ====> -2.
  • (4.0 + 2.9) * (3.78 -5.92) ====> -14. Whats a ML FUNCTION for EVALUATING expressions?

Online

Monday, April 16, 2012 49

Random Art from Expressions

PA

Build more funky expressions, evaluate them, to produce:

Monday, April 16, 2012 50

Random Art from Expressions

PA

Build more funky expressions, evaluate them, to produce:

Monday, April 16, 2012 51 Docsity.com