我有給我陣列的功能
S = [
publication(12, stout, call),
publication(19, jones, wendalissa),
publication(26, olson, dessert),
publication(33, hansen, away)
]
我想將此陣列更改為另一個
S = [
19-olson-wendalissa,
7-author-away,
22-jackson-book,
33-anotherauthor-anotherbook
].
這對我沒有幫助如何在 Prolog 中將串列轉換為字串?
custom_print(L2) :-
sol(S),
S = [
publication(12,A1,B1),
publication(19,A2,B2),
publication(26,A3,B3),
publication(33,A4,B4)
],
L = [
[12,A1,B1],
[12,A2,B2],
[26,A3,B3],
[33,A4,B4]
],
maplist(term_to_atom, L, L1),
atomic_list_concat(L1,'-',L2).
我是序言中的菜鳥,但我試圖理解這一點
uj5u.com熱心網友回復:
一個可能的解決方案
convert(publication(Id,A,B),Id-A-B).
convert_list(L,L1):-
maplist(convert,L,L1).
?- convert_list([publication(12, stout, call), publication(19, jones, wendalissa), publication(26, olson, dessert), publication(33, hansen, away)], L).
L = [
12-stout-call,
19-jones-wendalissa,
26-olson-dessert,
33-hansen-away
]
uj5u.com熱心網友回復:
給定sol/1,是這樣的:
sol(S) :- S = [
publication(12, stout, call),
publication(19, jones, wendalissa),
publication(26, olson, dessert),
publication(33, hansen, away)
].
除非我遺漏了什么,否則它似乎不應該比這更復雜:
fetch_pubs(Qs) :- sol(Ps), convert_pubs(Ps,Qs).
convert_pubs( [] , [] ).
convert_pubs( [P|Ps], [Q|Qs]) :- convert_pub(P,Q), convert_pubs(Ps,Qs).
convert_pub( publication(A,B,C), A-B-C ).
那么這是一個簡單的問題
?- fetch_pubs(L).
產生
L = [
12-stout-call,
19-jones-wendalissa,
26-olson-dessert,
33-hansen-away
]
如果您希望最終串列中的術語是原子或字串,只需添加(如果您使用 SWI Prolog,aterm_to_atom/2或 aterm_string/2到convert_pub/2:
% convert the term A - B - C to an atom
convert_pub( publication(A,B,C), P ) :- term_to_atom(A-B-C,P).
或者
% convert the term A - B - C to a string
convert_pub( publication(A,B,C), P ) :- term_string(A-B-C,P0.
如果您使用另一個 Prolog,您如何從一個術語到一個原子或字串可能取決于實作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510281.html
標籤:数组细绳序言
