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...
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.
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...
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of...
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of consecutive integers, from start to end. If start > end then an empty list is generated. For example: (gen-list 1 5) ---> (1 2 3 4 5) b) write a recursive function pair-sum? that takes an integer sequence as generated by the gen-list function in exercise 4 above. This function tests whether any two adjacent values in the given list sum to the given...
(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++
1.Write a function div7(lst) which takes in a list of integers, and returns a list of...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long. Here is some examples: >>> div7([14, 5, 7, 3, 29, 28, 10]) [True, False, True, False,...
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.
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a list containing only the items in list-a that are not also in list-b. E.g., (remove-all '(a b b c c d) '(a c a)) -> '(b b d)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT