我需要帮助将两个列表合并在一起。我的目标是将 "a"
替换为 "abc"
。我已经做了一个计数器等,但不知何故我无法 append 这两个列表(列表是一个空列表和 '("abc")
作为我要添加的新列表)。
我尝试添加更多的括号,但问题越加越严重。
(define (umwandl tr)
(cond ((null? tr) (display "Kein Text eingegeben"))
(else (uff (string->list tr) '())))) ; string wird nict inliste umgweandel
(define (uff tr liste)
(cond ((null? tr)(list->string liste))
(else
(cond ((equal? (first (rest (first (start1 (first tr))))) 1)
**((append list (list '("abc"))))))**
(uff (rest tr) liste))))
我的错误消息要么是 ('("abc"))
不是参数,要么只是给我 ""
作为答案。如果有人能提供帮助,那就太好了!
最佳答案
学习 Scheme 或任何 Lisp 意味着了解列表结构。列表 (1 2 3)
可以像 (cons 1 (cons 2 (cons 3 '())))
这样我喜欢认为列表中的元素没有 .
的意思是有一个,其余的有一组额外的括号。完成递归!因此:
'(1 2 3) ; ==
'(1 . (2 3)) ; ==
'(1 . (2 . (3))) ; ==
'(1 . (2 . (3 . ()))) ; ==> (1 2 3)
当我看到列表时,我看到了不存在的点,所以我知道 3
是 caddr
,因为我通过了 。 (
2 次 (dd
) 然后跳进 car
,因此 caddr
。
将两个列表 append 在一起意味着复制第一个列表并将 ()
替换为 第二个列表。因此 append
可以定义为:
(define (my-append l1 l2)
(if (null? l1)
l2
(cons (car l1)
(my-append (cdr l1) l2))))
用一个元素替换另一个元素。您需要执行相同的操作,但您比较每次迭代中的第一个元素,并基于该元素使用您找到的元素或替换元素。如果你想比较字符串,那么你需要使用 equal?
因为 (eqv? "hello""hello") ; ==> #f
是一个有效的结果。
关于list - 如何将两个列表 append 在一起?方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204166/