Question

In: Computer Science

scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to...

scheme:

Write a recursive Scheme function (subst x y L), which returns a list identical to L except that every occurrence of x has been replaced with y. The following example illustrates the use of this function: > (subst 'c 'k '(c o c o n u t)) (k o k o n u t)

Write a recursive Scheme function (all-different? L), which determines whether all elements of list L are distinct (that is, not equal?). The following example illustrates the use of this function: > (all-different? '(3 7 2 7)) #f

Solutions

Expert Solution

car gets the first pointer

cdr gets the second pointer

cpr and ctr are not required for this task

One method of doing it:

(define (subst k c list)     #; define the substitute procedure

       (cond   #; conditional computation

#; Syntax: cond < phrase 1 > < phrase 2 > etc ……..

#; where phrase = (<test> <expression> …….. )

#; that is, cond (<test> <expression> <test> <expression> …….)

                ((null? list) ‘())   #; make sure that the list is not null

#; cons will construct objects of memory to hold a couple of values

#; it may also hold couple of pointers to those values

#; ( cons c o c o n u t ) will construct (c . o . c . o . n . u . t)

#; can also be written as (c.(o. (c. (o. (n. (u. (t. nil)))))))

                ((list? (car list)) (cons (subst k c (car list)) (subst k c (cdr list))))

#; car list will get the first element in our case car (cococnut) returns c

#; cdr list returns the second or last pointer

                ((eq? (car list) k) (cons c (subst k c (cdr list))))

#; check for equality, if equal construct by replacing c with k

#; hence coconut will become kokonut, cat become kat, circuit becomes kirkuit etc

                (else

#; in the else case construct by getting the first element of the list c and calling the subst method again

                (cons (car list) (subst k c (cdr list))))))

#’ end of the scheme method

#; this is a comment line

#; (caar ‘((c o) (n u t))) is equivalent to

#; (car (car ‘((c o) (n u t))))

#; the result is c

#; because ( car ‘((c o) (n u t))) will return (c n )

#; then the outer car written as

#; ( car ( c n) ) will return c

#;

#|     this is a multi line comments block

spanning over few lines of commenting

Scheme is similar to LISP the language of Artificial Intelligence, Humanoids, Matrix Agents and Robotics

up to this line is a block of comments in scheme |#


Related Solutions

Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest value in the array. If the array is empty, return the “end” pointer ·and takes as parameters: (1)   a pointer to double. The pointer is the address of the start of an array, (2)   the “end” pointer to the address after the array (3)   and the address of the smallest value seen so far ·Write main() to test this function – try a case where the array...
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int)...
Exercise: Recursive List Processing Part 1: Write a recursive method that returns the length (an int) of the longest String in a List of Strings. For each recursive call, remove the first String from the list and return the greater of the length of the String you removed or the result of calling the method on the remainder of the list. The base case should be that the size of the list is 0. Write a driver to verify that...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
Your utility function over x and y is U ( x , y ) = l...
Your utility function over x and y is U ( x , y ) = l n ( x ) + 0.25 y. Your income is $20. You don’t know the prices of x or y so leave them as variables (p x and p y). a) (8 points) Find x*, your demand function for x. Find y*, your demand function for y. b) (10 points) Find the cross-price elasticity of demand for x (E x ∗ , p y:...
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers....
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers. If x=2, y=4, the method pow should return 16. Java answers only please.
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT