1

How to change string to atoms using maplist.

This does not work :

 ?- maplist(atom_string,["a","b","c"]).

first because atom_string/2 has arity of two (How do you do partial-function//currying in prolog).

But even if partial-fun worked the complication is that the first argument of atom_string is the unknown i.e. the call is :

  atom_string(A,"atom")

not :

  atom_string("atom",A)

this worked :

?- use_module(library(lambda)).

?- F = \Y^X^(atom_string(X,Y)), maplist(F,["a","b","c"],L).
F = \Y^X^atom_string(X, Y),
L = [a, b, c].
duplode
  • 33,731
  • 7
  • 79
  • 150
sten
  • 7,028
  • 9
  • 41
  • 63
  • correct ... i asked for changing "strings" to "atoms" using maplist, not "string" to "atom" ... changed the header to be more clear – sten Jan 01 '21 at 01:38
  • 1
    Why do you think `atom_string(A, "atom")` is a problem? [SWI-Prolog's documentation for atom_string/2](https://www.swi-prolog.org/pldoc/doc_for?object=atom_string/2) says that it's bidirectional, and running the query `?- atom_string(A, "atom").` I get the expected answer `A = atom.`. – Isabelle Newbie Jan 01 '21 at 18:51

2 Answers2

3

This works as intended:

?- maplist(atom_string, Atoms, ["a","b","c"]).
Atoms = [a, b, c].

If this is not what you are after, please explain.

TA_intern
  • 2,222
  • 4
  • 12
1

Use a helper predicate.

string_atom(String,Atom) :-
    atom_string(Atom,String).

Then run using

?- maplist(string_atom,["a","b","c"],Atoms).
Atoms = [a, b, c].
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • Why would you do that? What is wrong with `maplist(atom_string, Atoms, Strings)`? – TA_intern Jan 01 '21 at 19:21
  • 1
    @TA_intern: *Why would you do that?* To avoid leftover choice points in systems with only first argument indexing, e.g. SICStus. – false Jan 04 '21 at 18:25
  • @false It sounds like a workaround to me. I try to teach my students to fix things _in the right place_, **if possible**. I know there is never agreement what is the right place, and "possible" is on some scale of "very easy" to "theoretically not impossible". – TA_intern Jan 05 '21 at 06:23
  • 1
    @TA_intern: It is a pure workaround for systems that need it. Contrast this to other "optimizations" that cut away choice points giving up relational properties. – false Jan 05 '21 at 11:04