Search code examples
arraysstringprolog

SWI Prolog, array of arrays to array of strings


I have function that gives me array

S = [
      publication(12, stout, call),
      publication(19, jones, wendalissa),
      publication(26, olson, dessert),
      publication(33, hansen, away)
    ]

and I want to change this array to another

S = [
      19-olson-wendalissa,
      7-author-away,
      22-jackson-book,
      33-anotherauthor-anotherbook
    ].

This doesn't help me How to convert a list to a string in 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).

I'm noob in prolog, but I'm trying to understand this


Solution

  • A possible solution

    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
    ]