Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

B . Lambda Expression Command Syntax: ( lambda ( arg - id . . . ) body . . . + ) A lambda expression

B. Lambda Expression Command
Syntax: (lambda (arg-id ...) body ...+)
A lambda expression creates a function. In the simplest case, a lambda expression has
the form
Example 1:
((lambda (x)(+ x x))4) Output: 8
Example 2:
(define foo
(let ((x 4))
(lambda (y)(+ x y))))
(foo 6)
Output: 10
C. Lexical Binding or scoping
This is necessary for preserving lexical scope--the meanings of variable names must
be obvious at the point where the procedure is defined.
(let ((x 2)(y 3))
(* x y))
Output: 6
Write expression after converting to prefix
Example 1:
X =10 y=5 z=3
(x-y)*z
Output: 15
Example 2:
w =15 x =10 y=5 z=3
w/y-z*x
Output: -27
Example 3:
v=20 w =15 x =10 y=5 z=3
z*5+ y - w/y-v*x
Output: -183
Lexical binding and lambda
Example 1:
(let ((x 2)(y 3))
(let ((foo (lambda (z)(+ x y z)))
(x 7))
(foo 4)))
Output: 9
Example 2:
(let ((x 2)(y 3))
(let*((x 7)
(z (+ x y)))
(* z x)))
Output: 70
D. Internal Definitions
Example 1:
(let ((x 5))
(define foo (lambda (y)(bar x y)))
(define bar (lambda (a b)(+(* a b)
a)))
(foo (+ x 3)))
Output: 45
Code Explanation: first X will be 5 and Y will be based on input of foo, thus,
y=5+3=8, foo will call bar function. In bar function output will be 5*8+5=45
E. Exercises:
1- Define three variables x, y z and give them the value 10,5 and 3
respectively. Calculate the expression (x-y)*z using lambda.
2- Explain and predict the output of this code:
(define x 2)
(+ x 1)
3- Explain and predict the output of this code:
(set! x 4)
(- x 1)
F. Quoting
Syntax: quote or '
This notation is used to include literal constants in Scheme code. (quote
) may be abbreviated as '. The two notations are equivalent in
all respects.
Code Output
(quote a) a
(quote #(a b c)) #(a b c)
(quote (+12))(+12))
Example:
'"abc" Output: a
"abc" Output: a
G. Exercises: Write the corresponding expression using quoting
to display:
1-145932
2- #t
3- #\a
H. Sequencing
Syntax: begin ...
The s are evaluated sequentially from left to right, and the value(s) of the
last is(are) returned. This expression type is used to sequence side effects
such as input and output.
Example 1:
(define x 0)
(begin (set! x 5)
(+ x 1))
Output: 6
Example 2:
(begin (display "4 plus 1 equals ")
(display (+41)))
Output: 4 plus 1 equals 5=>
unspecified

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions