I already have the following code:
"atom_length(Var, Len) :- length(Var, Len)."
I wanna construct a predicate atom_lengths/2 that does the same thing with a list of atoms:
?- atom_lengths([one, two, three, four], [3, 3, 5, 4]).
true.
?- atom_lengths([one, two, three, four], LS).
LS = [3, 3, 5, 4].
?- atom_lengths([], LS).
LS = [].
How to write “atom_lengths”??
Thanks in advance!
You cannot use
length/2to compute the length of an atom.You can however first convert each atom to a list of characters with
atom_chars/2and then uselength/2to get its length:Test:
Instead of using the pair
atom_chars/2-length/2you could also use ISO builtin predicateatom_length/2:or using
findall/3:As suggested by commenter, a better idiom would be to use
maplist/3: