Programming Languages Recitation - High Order Function in Scheme | CS 3721, Lab Reports of Programming Languages

Material Type: Lab; Class: Programming Languages Recitati; Subject: Computer Science; University: University of Texas - San Antonio; Term: Unknown 1989;

Typology: Lab Reports

Pre 2010

Uploaded on 07/30/2009

koofers-user-b1o
koofers-user-b1o ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 3721: Programming Languages Lab
Lab #03: Higher order functions in Scheme
Due date: February 12, 3:30pm. At the beginning of the next recitation.
Goals of this lab:
โ€ขLearn to use let* ;Be able to define and use higher-order functions ;More recursion
1. The operator, let*. This operator is used for variable bindings, and has the following
syntax:
(letโˆ—< bindings > < body >)
< bindings > should have the form
((< variable1> < init1>). . .),
where each < init > is an expression, and < body > should be a sequence of one or more
expressions. The bindings are performed sequentially from left to right. Therefore, the
second binding is done in an environment in which the first binding is visible, and so on.
An example:
(let* ((x 7) ; first binding
(y 3) ; second binding
(z (+ x y))) ; third binding: first and second bindings are visible
(* z x y)) ; <body>: you can use your <bindings> in the <body>
=> 210
Given the lengths of the legs of several right triangles in a list, define a recursive function
calcHypotenuse which calculates the hypotenuses of the triangles using the let* operator.
Hint: You may want to embed the following template into the else part of the check for
whether the input list is null.
(let*
((triangle <expression for evaluating triangle, you may use your input list>)
(leg1 <expression for evaluating leg1, you may use triangle>) ; second binding
(leg2 <expression for evaluating leg2, you may use triangle>) ; third binding
(hypotenuse <expression for evaluating hypotenuse> )) ; forth binding
(... hypotenuse ...) ; <body>
)
Test your function with the expression below.
> (calcHypotenuse โ€™((5 12) (3 4) (7 24) (8 15)))
(list 13 5 25 17)
1
pf3

Partial preview of the text

Download Programming Languages Recitation - High Order Function in Scheme | CS 3721 and more Lab Reports Programming Languages in PDF only on Docsity!

CS 3721: Programming Languages Lab Lab #03: Higher order functions in Scheme

Due date: February 12, 3:30pm. At the beginning of the next recitation.

Goals of this lab:

  • Learn to use let* ; Be able to define and use higher-order functions ; More recursion
  1. The operator, let. This operator is used for variable bindings, and has the following syntax: (letโˆ— < bindings > < body >) < bindings > should have the form ((< variable 1 > < init 1 >).. .), where each < init > is an expression, and < body > should be a sequence of one or more expressions. The bindings are performed sequentially from left to right. Therefore, the second binding is done in an environment in which the first binding is visible, and so on. An example: (let ((x 7) ; first binding (y 3) ; second binding (z (+ x y))) ; third binding: first and second bindings are visible (* z x y)) ; : you can use your in the

=> 210 Given the lengths of the legs of several right triangles in a list, define a recursive function calcHypotenuse which calculates the hypotenuses of the triangles using the let* operator. Hint: You may want to embed the following template into the else part of the check for whether the input list is null. (let* ((triangle ) (leg1 ) ; second binding (leg2 ) ; third binding (hypotenuse )) ; forth binding (... hypotenuse ...) ; ) Test your function with the expression below. > (calcHypotenuse โ€™((5 12) (3 4) (7 24) (8 15))) (list 13 5 25 17)

  1. The function insert takes a number x and a sorted list of numbers in ascending order as arguments. It returns a sorted list in ascending order including the number x. Following is the scheme code for this function. (define insert (lambda (x myList) (cond ((null? myList) (cons x โ€™())) ;base case (else (if (<= x (car myList)) (cons x myList) (cons (car myList) (insert x (cdr myList))))))))

You can call it by the following: > (insert 6 โ€™(1 5 7)) (list 1 5 6 7) Now, using insert, write a function sort that takes a list of numbers and evaluates to a list containing those same integers sorted into ascending order (Hint: Use let to give a name to the result of recursively sorting the rest of the list (cdr field of the list)). Test your function with the expression below. > (sort โ€™(3 9 6 1)) (list 1 3 6 9)

  1. Higher-order functions. Higher-order function is a function that either takes a function as an argument or returns a function as a result (or both). The following maplist function is a higher-order function that takes a function and list and applies the function to every element in the list. The result is a list that contains all the results of function application. (define maplist (lambda (f myList) (cond ((null? myList) โ€™()) (else (cons (f (car myList)) (maplist f (cdr myList))))))) You can use this function by: > (maplist (lambda (x) (* x x)) โ€™(1 2 3 4 5)) (list 1 4 9 16 25) Now, using maplist, write a function seconds that takes a list containing lists having more than one elements and returns the second elements of the lists in a flat list. Test your function with the expression below. >(seconds โ€™((e l i) (m a n) (n i n g))) (list โ€™l โ€™a โ€™i)