我目前拥有的代码是我正在测试 stringOfList 函数的地方
let rec sepConcat sep sl = match sl with
| [] -> ""
| h :: t ->
let f a x = a^sep^x in
let base = h in
let l = t in
List.fold_left f base l
let stringOfList f l = sepConcat "; " (List.map f l)
我应该得到的输出是
# stringOfList string_of_int [1;2;3;4;5;6];;
- : string = "[1; 2; 3; 4; 5; 6]"
但我得到
# stringOfList string_of_int [1;2;3;4;5;6];;
- : string = "1; 2; 3; 4; 5; 6"
我究竟做错了什么?如何在括号内添加额外的 []。Sepconcat 应该这样做
# sepConcat ", " ["foo";"bar";"baz"];;
- : string = "foo, bar, baz"
# sepConcat "---" [];;
- : string = ""
# sepConcat "" ["a";"b";"c";"d";"e"];;
- : string = "abcde"
# sepConcat "X" ["hello"];;
- : string = "hello"