Scheme Programming: Expressions, Procedures, and Examples, Slides of Computer Programming

An introduction to scheme programming, covering the basics of expressions, procedures, and special forms. It includes examples of evaluating expressions and writing procedures for various tasks such as computing the usable page area, the most beautiful rectangle, and the positive root of a quadratic polynomial. It also covers recursive procedures for computing exponents, remainders, and fibonacci numbers.

Typology: Slides

2012/2013

Uploaded on 04/25/2013

lathika
lathika 🇮🇳

4

(12)

167 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Scheme
1. Basic Elements
(a) self-evaluating -expressions whose value is the same as the expression.
(b) names -Name is looked up in the symbol table to find the value associated with it.
Names may be made of any collection of characters that doesn’t start with a number.
2. Combination
( procedure arguments-separated-by-spaces )
Value is determined by evaluating the expression for the procedure and applying the resulting
value to the value of the arguments.
3. Special Forms
(a) define -(define name value)
The name is bound to the result of evaluating the value. Return value is unspecified.
(b) if -(if test consequent alternative)
If the value of the test is not false (#f), evaluate the consequent, otherwise evaluate the
alternative.
(c) lambda -(lambda parameters body)
Creates a procedure with the given parameters and body. Parameters is a list of names
of variables. Body is one or more scheme expressions. When the proc edure is applied,
the body expressions are evaluated in order and the value of the last one is returned.
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Scheme Programming: Expressions, Procedures, and Examples and more Slides Computer Programming in PDF only on Docsity!

Scheme

  1. Basic Elements

(a) selfevaluating expressions whose value is the same as the expression.

(b) names Name is looked up in the symbol table to find the value associated with it. Names may be made of any collection of characters that doesn’t start with a number.

  1. Combination ( procedure argumentsseparatedbyspaces ) Value is determined by evaluating the expression for the procedure and applying the resulting value to the value of the arguments.
  2. Special Forms

(a) define (define name value)

The name is bound to the result of evaluating the value. Return value is unspecified.

(b) if (if test consequent alternative)

If the value of the test is not false (#f), evaluate the consequent, otherwise evaluate the alternative.

(c) lambda (lambda parameters body)

Creates a procedure with the given parameters and body. Parameters is a list of names of variables. Body is one or more scheme expressions. When the procedure is applied, the body expressions are evaluated in order and the value of the last one is returned.

Problems

1. Evaluation For each expression:

(a) Write the type of the expression (b) Write your guess as to the expression’s return value. If the expression is erroneous simply indicate “error” for the value. If the expression returns an unspecified value, write whatever you want! If the expression returns a procedure, indicate “procedure” for the value. (c) Evaluate the expression, and copy the response from the scheme buffer.

(lambda (x) x)

((lambda (x) x) 17)

((lambda (x y) x) 42 17)

((lambda (x y) y) (/ 1 0) 3)

((lambda (x y) (x y 3)) (lambda (a b) (+ a b)) 14)

2. Writing Procedures Write the procedure indicated. Then test it in scheme to make sure it

works.

(a) Write a procedure cube that returns the cube of it’s input. (define cube

(b) Write a procedure theanswer? , which returns true (#t) if the input is the number 42. (define theanswer?

(c) Write a procedure sign that returns 1 if it’s input is positive, 1 if it’s input is negative, and 0 if it’s input is 0. (define sign

3. BiggieSizing!

Suppose we’re designing an pointofsale and ordertracking system for Wendy’s 1. Luckily the UberQwuick drive through supports only 4 options: Classic Single Combo (hamburger¨ with one patty), Classic Double With Cheese Combo (2 patties), and Classic Triple with Cheese Combo (3 patties), AvantGarde Quadruple with Guacamole Combo (4 patties). We shall encode these combos as 1, 2, 3, and 4 respectively. Each meal can be biggiesized to acquire a larger box of fries and drink. A biggiesized combo is represented by 5, 6, 7, and 8 respectively.

(a) Write a procedure named biggiesize which when given a regular combo returns a biggiesized version.

(b) Write a procedure named unbiggiesize which when given a biggiesized combo returns a non biggiesized version.

(c) Write a procedure named biggiesize? which when given a combo, returns true if the combo has been biggiesized and false otherwise.

(d) Write a procedure named comboprice which takes a combo and returns the price of the combo. Each patty costs $1.17, and a biggiesized version costs $.50 extra overall.

(^1) 6.090 and MIT do not endorse and are not affiliated with Wendy’s in any way. They merely capitalize on the pleasant way “biggiesize” rolls off the tongue.

Recursion

More Problems

  1. Write expt, a procedure that raises x to the nth power. You may assume that n is nonnegative and integer.

(expt 2 2) ;Value: 4 (expt 2 3) ;Value: 8

Plan:

(define expt (lambda (x n)