简体   繁体   中英

Creating an append function in Racket

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

It is much simpler than that.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM