--What does the following Scheme function do? Explain the answer provided. --Wha
ID: 3546838 • Letter: #
Question
--What does the following Scheme function do? Explain the answer provided.
--What is the result of (guess '(A B C))? Show the derivation process.
(DEFINE(guess lis)
(COND
(NULL? lis) '())
(ELSE(append(guess(CDR lis)) (LIST (CAR lis)) ) )
))
Explanation / Answer
result is (C B A) reversed the list......solution is given at the ending
Let me start with the basics....
1)NULL:
NULL? function tests its parameters to determine whether it is the empty list and returns #T if it is.
eg.,
(NULL? '(A B)) returns ( )
(NULL? '( )) returns #T
(NULL? 'A) returns ( )
(NULL? '( ( ) ) ) returns ( ) (parameter is not empty list, but list containing an empty list)
(NULL? NIL) returns #T
____________________________________________________________
2)CAR:
CAR function returns the first element of a given list.
e.g.,
(CAR '(ABC)) returns A
(CAR '((A B) C D)) returns (A B)
(CAR 'A) is an error (A is not a list)
(CAR '(A)) returns A
________________________________________________________________
3)CDR:
CDR returns the remainder of a given list after its CAR is removed.
(CDR '(A B C)) returns (B C)
(CDR '((A B) C D)) returns (C D)
(CDR 'A) is an error
(CDR '(A)) returns ( ) (empty list, also equivalent to NIL)
___________________________________________________________________
now coming to the question
given lis = (A B C)
code runs ,
now as lis is not NULL, else executes i.e, append(guess(CDR lis)) (LIST (CAR lis))
==> append (guess((B C)) (LIST (A)))
==> append ( append (guess (C) LIST (B)) LIST (A))
==> append (append ( append () LIST (C))) LIST (B) ) LIST (A))
==> append (append LIST(C) LIST (B)) LIST (A)
==> append (LIST (C B) LIST (A))
==> LIST (C B A)
==> ( C B A)
I reversed the list given i.e, reversed (A B C ) to (C B A)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.