首页 > 解决方案 > 方案 - 字符串附加两个字符串列表

问题描述

我正在尝试附加两个字符串列表

但我不知道如何在两个单词之间添加空格。

(define (string-concat lst1 lst2)
        (map string-append lst1 lst2)
)
(string-concat '("elementary") "(school))

然后结果为“小学”

它们合并时如何添加空间?我尝试使用 lambda 但它不起作用,例如

(map string-append (cdr (append* (map (lambda (x) list " " x)) lst1)) lst2)

标签: schemeracket

解决方案


您可以简单地在两个字符串之间添加一个空格,string-append接受多个参数:

(string-append "hello" " " "world")
=> "hello world"

或者你可以使用string-join

(string-join '("hello" "world"))
=> "hello world"

现在,将其扩展到字符串列表:

(map (lambda (s1 s2) (string-append s1 " " s2))
     '("a" "b" "c")
     '("1" "2" "3"))
=> '("a 1" "b 2" "c 3")

(map (lambda (s1 s2) (string-join (list s1 s2)))
     '("a" "b" "c")
     '("1" "2" "3"))
=> '("a 1" "b 2" "c 3")

推荐阅读