Write a scheme function named longerlist that takes two list

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)

Solution

(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)))))))

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site