Scheme Programming Problems and Solutions, Slides of Computer Programming

A list of programming problems in scheme, a functional programming language, along with their solutions. The problems cover various topics such as absolute value, summation, computing e, recursion, string manipulation, and euclid’s algorithm. The solutions are provided with explanations and scheme code.

Typology: Slides

2012/2013

Uploaded on 04/25/2013

lathika
lathika 🇮🇳

4

(12)

167 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Scheme
1. Special Forms
(a) begin -(begn exps)
Evaluate each expression in order and return the value of the last expression.
Problems
1. Write abs, a function that returns the absolute value of its input.
(abs 5)
;Value: 5
(abs -5)
;Value: 5
(define abs
(lambda (val)
2. Write sum-to-n which sums the numbers from 1 to n inclusive.
(define sum-to-n
(lambda (n)
Alter the procedure to sum the squares of the numbers.
Docsity.com
pf3
pf4

Partial preview of the text

Download Scheme Programming Problems and Solutions and more Slides Computer Programming in PDF only on Docsity!

Scheme

  1. Special Forms

(a) begin (begn exps)

Evaluate each expression in order and return the value of the last expression.

Problems

  1. Write abs, a function that returns the absolute value of its input.

(abs 5) ;Value: 5 (abs 5) ;Value: 5

(define abs (lambda (val)

  1. Write sumton which sums the numbers from 1 to n inclusive.

(define sumton (lambda (n)

Alter the procedure to sum the squares of the numbers.

  1. Write a procedure that computes e.
  2. Write a procedure that runs forever. (Remember that Cc, Cc stops evaluation)
  3. Using stringappend , write a procedure pad, which takes a string and a number, that returns the string with that number of spaces added to the end.

(pad "yay" 0) ;Value: "yay" (pad "yay" 1) ;Value: "yay " (pad "yay" 3) ;Value: "yay "

(define pad

  1. Write a procedure that uses Euclid’s algorithm to compute the GCD of two numbers. Euclid’s algorithm (according to Knuth it’s the oldest known algorithm) goes as follows: if r is the remainder of a divided by b, then the common divisors of a and b are the same as those of b and r. Additionally, the gcd of a number and 0 is the number.

(gcd 206 40) ;Value: 2

(define gcd (lambda (a b)

(define (baz) 5)

bar

baz

(bar)

(baz)

(let ((a 3) (b 5)) (+ a b))

(let ((+ ) ( +)) (+ 3 (* 4 5)))

(define m 3) (let ((m (+ m 1))) (+ m 1))

(define n 4) (let ((n 12) (o (+ n 2))) (* n o))

Guessing Game