Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Syntactic Sugar - Building Programming Experience - Homework, Exercises of Computer Programming

Some concept of Building Programming Experience are Evaluation, List Procedures, Experience Modifying, Non-Collaborative, Syntactic Sugar, Thesaurus.Main points of this homework are: Syntactic Sugar, Check, Recursive, Iterative Processes, Definitions, Stepper, Computation Unfolds, Evaluation, Process, Evaluate

Typology: Exercises

2012/2013

Uploaded on 04/25/2013

lathika
lathika 🇮🇳

4

(12)

178 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Problem 1: Syntactic Sugar
1. Desugar the following expressions:
(define (foo x)
(+ x 5))
(let ((x 1))
x)
(let ((foo (= x 1))
(bar 7))
(if foo
bar
#f))
(define (weird x y z) ; this one's odd
(lambda (foo)
(+ x y z foo)))
2. Evaluate the following expressions (first guess, then check with MITScheme).
(define x 5)
(define (y) (+ 7 7))
(let ((x 3))
(+ x x))
(let ((x (y))
(y 7))
(if (> x 3)
7
y)
(let ((mit 12))
(let ((is (+ mit 1)))
(let ((hard (- is 7)))
Docsity.com
pf3
pf4

Partial preview of the text

Download Syntactic Sugar - Building Programming Experience - Homework and more Exercises Computer Programming in PDF only on Docsity!

Problem 1: Syntactic Sugar

  1. Desugar the following expressions:

(define (foo x) (+ x 5)) (let ((x 1)) x) (let ((foo (= x 1)) (bar 7)) (if foo bar #f)) (define (weird x y z) ; this one's odd (lambda (foo) (+ x y z foo)))

  1. Evaluate the following expressions (first guess, then check with MITScheme).

(define x 5) (define (y) (+ 7 7)) (let ((x 3)) (+ x x)) (let ((x (y)) (y 7)) (if (> x 3) 7 y) (let ((mit 12)) (let ((is (+ mit 1))) (let ((hard (- is 7)))

(+ mit is hard))))

Problem 2: Recursive and Iterative processes

  1. Exploration : Evaluate the following two definitions in MITScheme.

(define (remainder x y) (if (< x y) x (remainder (- x y) y))) (define (fact n) (if (= n 0) 1 (* n (fact (- n 1)))))

Then evaluate (fact 10) with the Stepper (use M-s instead of C-x,C-e). Hold down the space bar and watch how the computation unfolds. How would you describe the text as a whole as it does the evaluation? How indented is it? What does the end (you can use M-> to go to the end of a buffer) look like? Is there a point at which the process of evaluation changes? Keep in mind that the stepper indents two spaces when it is trying to evaluate a subexpression.

Pop back to scheme and evaluate (remainder 30 3) with the Stepper (again, M-s). Hold down the space bar again. How is this computation different? What does the end look like?

Explain these differences with reference to recursive vs iterative processes. (You need not submit the Stepper buffers you produce)

  1. The following are two different implementations of slow-add, a procedure that adds two numbers which using any arithmetic procedures other than inc and dec:

(define (slow-add1 a b) (if (= a 0) b (inc (slow-add1 (dec a) b)))) (define (slow-add2 a b) (if (= a 0) b (slow-add2 (dec a) (inc b))))

For each procedure, indicate whether it gives rise to a recursive or iterative process, and why. Then test it with the stepper to verify your hypothesis (you need not submit the Stepper buffer that you produce).

  1. Write expression whose values print out like the following:

("this" "is" "yummy") ((())) (("apples" 3) ("oranges" 2))

  1. Here is the length procedure we wrote in class:

Plan: Base case: empty-list -> length is 0 Recursive: length whole lst = 1 + length rest of lst (define (length lst) (if (null? lst) 0 (+ 1 (length (cdr lst)))))

Write a procedure called sum-list which takes in a list of numbers and outputs their sum.

(sum-list (list 1 2 3)) ;Value: 6 (sum-list (list 7)) ;Value: 7 (sum-list nil) ;Value: 0

  1. Extra Bonus Problem: Write a procedure seven-on-the-end which takes in a list and returns a new list with 7 on the end.

(seven-on-the-end nil) ;Value: (7) (seven-on-the-end (list 4)) ;Value: (4 7) (seven-on-the-end (list 4 7 5 3)) ;Value: (4 7 5 3 7)

Tackle this problem by figuring out the base case, then the one-off base case (ie where the first recursive call results in the base case).