将字符串列表拆分为嵌套的字符列表

问题描述 投票:0回答:1

我试图使用string_chars / 2将字符串列表转换为嵌套的字符列表。我想我需要使用maplist / 2来实现这一点,但我不确定为什么它不起作用。这是我所做的一个例子

maplist(string_chars(X),["Hello","Prolog"]).

但它只是返回错误。从文档看起来它应该将列表中的每个参数传递给string_chars。有谁知道我怎么做到这一点?

prolog swi-prolog
1个回答
0
投票
?- maplist(string_chars,["Hello","Goodbye"],Res).
Res = [['H', e, l, l, o], ['G', o, o, d, b, y, e]].

只需按预期使用string_chars,但删除前两个参数,因为它只有两个参数,string_chars

maplist / 2也只是一个谓词,并说明所有映射是否有效,但maplist / 3可用于返回结果。

© www.soinside.com 2019 - 2024. All rights reserved.