[Solved] CSI2120 Lab 9-Scheme

$25

File Name: CSI2120_Lab_9-Scheme.zip
File Size: 188.4 KB

SKU: [Solved] CSI2120 Lab 9-Scheme Category: Tag:
5/5 - (1 vote)

Exercise 1: Prefix Notation

Use Racket as an interactive Scheme calculator. Scheme uses prefix notation. Example:

(* 6 (- 10 5))

This expression will evaluate to 30 because 6 * (10 5) = 30.

Calculate the sum of the squares for the numbers 1 to 4. Hint: Your result should be 30.

Calculate both solutions to 2x^2 + 5x 3 = 0.

Calculate the result of the sin(pi/4) cos(pi/3) + cos(pi/4) sin(pi/3)

Exercise 2: Local and Global Definitions

Global definition use define and local definition use let bindings.

Define a global constant

(define myFavorite 42)

Verify that you can use the definition.

Define locals with let

(let ((x 1) (y 2)) (+ x y))

Verify that after the evaluation x and y are no longer defined, e.g. by using (* x y)

Use a local let binding for the angles to calculate sin(pi/4) cos(pi/3) + cos(pi/4) sin(pi/3) again.

Exercise 3: Procedures

Lambda expressions create local procedures.

((lambda (x y) (+ x y)) 1 2)

This code defines the lambda and then uses it with the arguments 1 and 2.

Use a lambda with the angles as arguments to calculate sin(pi/4) cos(pi/3) + cos(pi/4) sin(pi/3) again.

We can also define global procedures.

(define foo (lambda (x y) (+ x y)))

Call the function foo with 1 and 2 as arguments.

Note that the syntax of Scheme follows strict form. The first entry in a list is treated as a function. Lists are written with round brackets. In fact all the above calculations used a list to express the calculation.

You can find more information in our textbook The Scheme Programming Language by R. Kent Dybvig, especially, Section 2.5 Lambda Expressions.

Define a global function to calculate sin(alpha) cos(beta) + cos(alpha) sin(beta) with alpha and beta as parameters.

Exercise 4: Quadratic equation

Define a global function that finds both roots of a quadratic equation ax^2+bx+c = 0 with a,b,c as arguments. Note that you can calculate two solutions and return them as a list as follows:

(define foo (lambda (x y) (list (+ x y) (- x y))))

Exercise 5 and Quiz:

Please hand-in the answer to this question on Virtual Campus at the latest by Friday 6:00pm! Your submission will only count if you also hand-in solutions to all of the exercises. However, Exercise 1 to 4 will not be graded.

The following top-level define uses local lambda functions in its calculations (Half-angle trigonometric identity). Change the definition to instead use local let bindings.

(define (halfTrig theta) (/ (* 2 ((lambda (x) (tan (/ x 2))) theta)) (+ 1 ((lambda (y) (* y y)) ((lambda (x) (tan (/ x 2))) 1.57)))))

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSI2120 Lab 9-Scheme
$25