Tests failing on Acronym exercise


I’ve got this code:

abbreviate(Sentence, Acronym) :-
    split_string(Sentence, "- ", " \t\n", PhraseList),
    maplist(remove_non_alphanumeric, PhraseList, CleanList),
    maplist(first_char, CleanList, AcronymList),
    atomic_list_concat(AcronymList, AcronymString),
    upcase_atom(AcronymString, Acronym).

remove_non_alphanumeric(String, CleanString) :-
    atom_chars(String, Chars),
    include(alpha_numeric_char, Chars, AlphaNumericChars),
    atom_chars(CleanString, AlphaNumericChars).

alpha_numeric_char(Char) :-
    char_type(Char, alpha);
    char_type(Char, digit).

first_char(String, Char) :-
    atom_chars(String, [Char|_]).

When I test it interactively it works. However when I run the test, it fails with errors similar to:

    [[ EXCEPTION while printing message url('/mnt/exercism-iteration/acronym_tests.plt':9)
       with arguments []:
       raised: type_error(text,url('/mnt/exercism-iteration/acronym_tests.plt':9))
	test basic: failed

I’m completely new to Prolog but do have lots of experience with other programming likes like Python. Thanks in advance for your help on this.

Hi, @deji.

In case you’re still having trouble with this, the explanation for the failed test is very simple. The solution you posted returns an atom, not a string:

?- ["acronym.pl"].

?- abbreviate("domain name server", A), atom(A).
A = 'DNS'.

?- abbreviate("domain name server", A), string(A).

Notice the “single” quotes ('') around the acronym (or rather, the initialism in this case). A string in Prolog is only ever represented with “double” quotes ("").

Atoms are typically not allowed to begin with uppercase letters, unless you “quote” them as above. Quoted atoms can even contain spaces:

?- [user].
|: 'Winnie the Pooh'(bear).
|: ^D

?- 'Winnie the Pooh'(What).
What = bear.

As you may have figured out already, the abbreviate predicate needs to unify the acronym with a string representation:

  % . . .    
  upcase_atom(AcronymString, AcronymAtom),  % <//  result is an atom
  atom_string(AcronymAtom, Acronym).        % <//  result is a string