Write a scheme function named longer-list that takes two list arguments and retu
ID: 3940964 • Letter: W
Question
Write a scheme function named longer-list that takes two list arguments and returns the longer list of the two inputs. If the two lists are equal in length, the function returns #t, and if one of the arguments is not a list, the function should return #f.
Note: You are not allowed to use the predefined length function; however, you can write your version of length or other helper functions that you may want to call from longer-list.
Sample runs:
(longer-list '(1 2 3 4) '(a b c d e)) returns (a b c d e)
(longer-list '(d e f) '(4 5 6)) returns #t (or true)
(longer-list '(g h i) 3) returns #f (or false)
Explanation / Answer
(define (longer-list x y)
(and (list? x)
(list? y)
(let ll ((xx x) (yy y))
(cond
((and (null? xx) (null? yy)) #t)
((null? xx) y)
((null? yy) x)
(else (ll (cdr xx) (cdr yy)))))))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.