Need some help making functions using Scheme Dr Racket 1 Cre

Need some help making functions using Scheme (Dr. Racket)

1) Create a recursive procedure called (alternate lst1 lst2 lst3) that returns a list with alternative values from the 3 given list. Use comments to indicate in the code the four steps of the fantastic approach. Assume that all 3 lists have the same length. Hint: (append lst1 lst2 lst3) returns the appended list.                                                                                                                               [10 points]

Test case: (alternate ‘(1 2 3) ‘(a b c) ‘(m n o)) should give \'(1 a m 2 b n 3 c o).

           

2) Create a recursive procedure called (pairs lst1 lst2) that returns a list of pairs from 2 given lists. Use comments to indicate in the code the four steps of the fantastic approach. You can assume that the both lists have the same length.                                                                                    [10 points]

Test case: (pairs ‘(1 2 3) ‘(a b c)) should give \'((1 . a) (2 . b) (3 . c)).

3) Create a recursive procedure called (sumOdds lst), where lst is a list of numbers. The procedure should return the sum of all odd numbers on the list. When the list is empty, the result should be 0.      

Test case: (sumOdds \'(1 3 4 5 6 -7)) à 2

((lambda (lst) (add code here) \'(1 3 4 5 6 -7)) where the list \'(1 3 4 5 6 -7) is the argument.          [5 points]

4) Create a recursive procedure called (findMax lst), where lst is a list of numbers. The procedure should return the max number in the list. If the given list is empty, you must print “empty list”. You may need to create a helper procedure.

Solution

a.

(define (alternate lst1 lst2 lst3)
(if (or (null? lst1) (null? lst2) (null? lst3))
      \'()
      (cons (car lst1)
        (cons (car lst2)
          (cons (car lst3)
            (alternate (cdr lst1)(cdr lst2)(cdr lst3)))))))


b.

(define some-letters \'(a b c))
(define some-nums \'(1 2 3))
(define x (first (foldr cons empty some-letters)))
(define y (first (foldr cons empty some-nums)))
(define (pairs list1 list2)
(cond
    [(empty? list1) empty]
    [(empty? list2) empty]
    [else (cons (list (first list1) (first list2))
                (pairs (rest list1) (rest list2)))]))


c.


(define (sumOdds lst)
(length (filter odd? lst)))

(define l \'(1 2 3 4 5 6 7 8 9 10))
(sumOdds l)


d.

(define (findMax lst)
(if (empty? lst)
      #f
      (let ((next (findMax (cdr lst))))
        (define cur (car lst))
        (if (not next)
            (cons cur cur)
            (cons (findMax (cdr next) cur))))))

Need some help making functions using Scheme (Dr. Racket) 1) Create a recursive procedure called (alternate lst1 lst2 lst3) that returns a list with alternative
Need some help making functions using Scheme (Dr. Racket) 1) Create a recursive procedure called (alternate lst1 lst2 lst3) that returns a list with alternative

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site