Procedures and Recursion - Building Programming Experience - Lecture Slides, Slides of Computer Programming

Some concept of Building Programming Experience are Trees, Square Limit Language, Special Forms, Quizanssheet, Professor Abstraction, Compound Procedure, Procedures And Recursion. Main points of this lecture are: Procedures And Recursion, Evaluate, Argument Values, Arguments Unspecified, Value, Operator Value, Evaluation, Order, Compound, Procedure

Typology: Slides

2012/2013

Uploaded on 04/25/2013

lathika
lathika 🇮🇳

4

(12)

167 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 2: Procedures and Recursion
Review:
Self-evaluating expression
#t, 5, “yay”
Name expression
+, foo, bar
N | V
foo | 4
combinations (+ 3 4) to evaluate a combination, sequence of steps:
Evaluate operator
Evaluate arguments
Apply operator value to argument values
Order of evaluation of arguments unspecified, but scheme does it right-to-left
Special forms:
define
(define name value)
(define bar (+ 3 3))
if
(if test consequent alternative)
Make mental pictures for better gasp abstract more difficult to hold
Lambda:
((lambda (x) (* x x)) 5) compound procedure in a combination
(* 5 5) => 25
Lambda procedures are lengthy; in order to reuse over and over again with
simplicity, give name.
(define square
-Æ(lambda (x)
---Æ(* x x)))
Notice indentations
All code now must be tabbed accordingly: newly opened paren is where tab should
begin. In scheme, simply hit the return key and then hit the tab key, and the correct
tab will automatically appear in the code.
Writing Procedures
Docsity.com
pf3

Partial preview of the text

Download Procedures and Recursion - Building Programming Experience - Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

Lecture 2: Procedures and Recursion

Review: Self-evaluating expression #t, 5, “yay” Name expression +, foo, bar

N | V

foo | 4

combinations (+ 3 4) to evaluate a combination, sequence of steps: Evaluate operator Evaluate arguments Apply operator value to argument values Order of evaluation of arguments unspecified, but scheme does it right-to-left

Special forms: define (define name value)

(define bar (+ 3 3))

if (if test consequent alternative)

Make mental pictures for better gasp abstract more difficult to hold

Lambda:

((lambda (x) (* x x)) 5) compound procedure in a combination

Lambda procedures are lengthy; in order to reuse over and over again with simplicity, give name.

(define square

  • Æ(lambda (x) ---Æ(* x x))) Notice indentations

All code now must be tabbed accordingly: newly opened paren is where tab should begin. In scheme, simply hit the return key and then hit the tab key, and the correct tab will automatically appear in the code.

Writing Procedures

Not takes one argument, it negates what you see For example: Not true-ish is false Not false is true

(define not (lambda (s) (if s #f #t)))

Evaluation of (not #f):

  • (not #f)
  • ((lambda (s) (if s #f #t)) #f)
  • (if #f #f #t)
  • #t

When you come across a problem Æ break into smaller, more manageable problems

Factorial n! = (n – 1)! * n 0! = 1

(define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1))))))

Use substitution model to investigate this If scheme gets stuck in an infinite loop, exit with C-c, C-c.

  • (fact 2)
  • (if (= 2 0) 1 (* 2 (fact (- 2 1)))
  • (* 2 (fact 1))
  • (* 2 (if (= 1 0) 1 (* 1 (fact (- 1 1)))))
  • (* 2 (* 1 (fact 0)))
  • (* 2 (* 1 (if (= 0 0) 1 (* 0 (fact (- 0 1))))))
  • (* 2 (* 1 1))
  • (* 2 1)
  • 2

Defined factorial in terms of itself: this is known as recursion.

Exponentiation x^n = x^(n-1)x X^0 = 1 (define expt (lambda (x n) (if (= n o) 1 ( x (expt x (- n 1))))))