

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Lab; Class: Programming Languages Recitati; Subject: Computer Science; University: University of Texas - San Antonio; Term: Unknown 1989;
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!


CS 3721: Programming Languages Lab Lab #02: Recursion over Lists in Scheme
Due date: February 5, 3:30pm. At the beginning of the next recitation.
Goals of this lab:
if syntax: (if < test > < consequent > < alternate >) , or (if < test > < consequent >) Semantics: An if expression is evaluated as follows: first, < test > is evaluated. If it yields a true value, then < consequent > is evaluated and its value is returned. Otherwise < alternate > is evaluated and its value is returned. Write an example expression using the if operator, and then write down what that expression evaluates to.
A scheme function has the form: (lambda (< parameters >) < f unction body >), where < parameters > are list of identifiers, and < f unction body > is an expression. example: (lambda (x) (∗ x x)), evaluates to a procedure. One can call this function with the argument (+ 2 2) by the following expression: ( (lambda (x) (∗ x x)) ︸ ︷︷ ︸ procedure
︸ ︷︷ ︸ parameter
), this evaluates to 16.
? “lambda” makes it possible to write anonymous functions which are the functions that do not have a declared name. Now, create a function that takes a list as a parameter and builds a new list appending the input list to the input list, again. (Hint: Use the operator cons while building your list.). Test your function with the following expression.
( (lambda ...) ︸ ︷︷ ︸ your anonymous function
′(a b) ︸ ︷︷ ︸ input list
should evaluate to ︷︸︸︷ ⇒ (list (list ′a ′b) ′a ′b)
(define mySquare (lambda (x) (* x x))) You can call this function simply by typing the following.
(mySquare 4) => 16 In Scheme every value is either null, a pair, or an atom. Scheme provides predicates null? and pair? for determining whether a value is ’() or a cons cell, respectively. Moreover, for any x, the expression (or (null? x) (pair? x) (atom? x)) should evaluate to true. Now, define the function atom? which takes a single parameter x and returns whether x is an atomic value. (Hint: use null?, and pair? operators.)