The concept of first class objects is fundamental for Scheme programming. In par
ID: 3824112 • Letter: T
Question
The concept of first class objects is fundamental for Scheme programming. In particular, in Scheme functions are first class objects. The main properties of functions as first class objects are exemplified by answering the following questions: (a) The first class object may be expressed as an anonymous literal value (constant). Show an example of the anonymous function and its use. (b) The first class object may be stored in variables (i.e. it may have a symbolic name). Show examples of defining and using named functions. (c) The first class object may be stored in data structures. Show an example of a data structure(e.g. a list) that contains functions (d) The first class object may be comparable to other objects for equality. Show an example of comparing functions and lists for equality. (e) The first class object may be passed as parameter to procedures/functions. Show an example of passing function as an argument to another function. (f) The first class object may be returned as result from procedures/functions. Show an example of returning a function as a result of another function. (g) The first class object may be readable and printable. Show examples of reading function(s) from the keyboard, reading function(s) from a file, and displaying a function.Explanation / Answer
a). example of anonymous funct 123
//> 123
123//
b)defining named function
//> (define x 5)
> x
5 //
c). Data structure with list
//>(length (list 2 4 6 8 10))
5//
d) comparing
//> (eq? ‘x ‘x)
#t
>(eqv? (* 2 2) (sqrt 16))
#t
>(equal? 3 (/ 6 2))
#t //
e).passing function as argument
//> ((lambda (x) (* 2 x)) 5)
10 //
f).Returning a function as resullt of another func.
//> (define fact
(lambda (n)
(if (= n 0) 1
(* n (fact2 (- n 1))))))
> (fact2 3)
6 //
g))
//> (define p (open-input-file "testdata.txt"))
> (read p)
123
>(display p) //
// thank_you//
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.