[英]Creating an append function in Racket
在ISL中,您將如何創建一個遞歸 append
函數,它接受兩個列表並返回第一個列表中所有最高位置元素的列表,其中第二個列表的位置元素最高(不使用lambda
或append
)?
基本上,為這些檢查保留的函數需要:
(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))
我覺得肯定會使用map
,因為這是我們最近一直關注的內容。 這就是我所擁有的,它確實有效,但我想知道是否有一種方法可以使用map,foldr,foldl,filter或類似的東西來簡化它。
這是我到目前為止所擁有的:
(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))]))
它比這簡單得多。
(define (append-test lhs rhs)
(if (empty? lhs)
rhs
(cons (first lhs) (append-test (rest lhs) rhs))))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.