简体   繁体   中英

Prolog: Downcase all atoms in a list

I'm trying to write a Prolog program that needs to take a user's natural language input and match it against a set of atoms. I'm using SWI Prolog's readln/1 to get input and put it in a list of atoms, but I don't have any guarantee of the case of the user input, so I want to just downcase all of the input I get before I try matching. What I have right now is:

downcase_list(AnyCase, LowerCase) :- dcl(AnyCase, X), flatten(X,LowerCase).

dcl([], List) :- List.
dcl([Head|Rest], []) :- downcase_atom(Head,X), dcl(Rest,X).
dcl([Head|Rest], List) :- downcase_atom(Head,X), dcl(Rest, [List|X]).

Appending using [List|X] seems to be my problem, but I don't know how to fix it, since I've already tried using append/3 and just got an infinite loop:

downcase_list([], List) :- List.
downcase_list([Head|Rest], []) :- downcase_atom(Head,X), downcase_list(Rest, X).
downcase_list([Head|Rest], NewList) :- downcase_atom(Head,X), append(NewList,X,Z), writeln(Z), downcase_list(Rest,Z).

I am very new to Prolog (I would classify myself as a Lisp programmer at this point), so it's very possible I'm missing something elementary. Help?

If you already have a list of atoms, to downcase them you have to apply a mapping with maplist/3 and downcase_atom/2 :

downcase_list(AnyCaseList, DownCaseList):-
  maplist(downcase_atom, AnyCaseList, DownCaseList).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM