1
votes

In ISL, how would you create a recursive append function that takes two lists and returns a list of all highest position elements of the first list with the highest position elements of the second list (without using lambda or append)?

Basically a function that would hold for these check expects:

(check-expect (append-test '(a b c) '(d e f g h))  (list 'a 'b 'c 'd 'e 'f 'g 'h))
(check-expect (append-test '() '(7 2 0 1 8 3 4)) (list 7 2 0 1 8 3 4))

I feel like it would definitely use map, since that's what we've been focusing on lately. Here's what I have, which does work, but I was wondering if there was a way to simplify this with map, foldr, foldl, filter, or something like that.

Here's what I have so far:

(define (append-test lst1 lst2)
  (cond
    [(and (empty? lst1)(empty? lst2)) '()]
    [(empty? lst1) lst2]
    [(empty? lst2) lst1]
    [else (cons (first (first (list lst1 lst2)))
                (append-test (rest lst1) lst2))]))
1

1 Answers

5
votes

It is much simpler than that.

(define (append-test lhs rhs)
  (if (empty? lhs)
      rhs
      (cons (first lhs) (append-test (rest lhs) rhs))))