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).
Write a Scheme function that takes a list of integers and returns all odd integers on...
Write a Scheme function that takes a list of integers and returns all odd integers on the list in the original order: (odd-numbers `(2 4 9 16 25 7)) (9 25 7) Hint: 2 (remainder 13 5) 3 Please explain every step
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
Write a Scheme function that takes two integers and returns the list of all integer numbers...
Write a Scheme function that takes two integers and returns the list of all integer numbers between these two integers (inclusively) in increasing order. (numbers 10 20) (10 11 12 13 14 15 16 17 18 19 20) Please explain every step.
(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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT