[英]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
)? 在ISL中,您将如何创建一个递归 append
函数,它接受两个列表并返回第一个列表中所有最高位置元素的列表,其中第二个列表的位置元素最高(不使用lambda
或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. 我觉得肯定会使用map
,因为这是我们最近一直关注的内容。 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. 这就是我所拥有的,它确实有效,但我想知道是否有一种方法可以使用map,foldr,foldl,filter或类似的东西来简化它。
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))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.