Fiedberg Nichenko
Fiedberg Nichenko

Reputation: 153

Prolog - Ran into an issue

minu('-').
minu('+').
minu('/').

f(A):-
   atom_chars(A,X),
   write(X),
   fun(X).

fun([]).

fun([A]):-
   not(minu(A)).

fun([Hd|Tail]):-
   not(minu(Hd)),
   fun(Tail).

I am trying to make an function "f" that takes a string and returns True if "-", "+" or "/" are not in it.

But as soon as I use recursive call. It just returns false.

EDIT ::

SOLUTION FOUND ::

Thank you, lurker... And thank you, Daniel Lyons... I apologize it has been a long day learning Prolog... I ought to go get some sleep.

f(A):-
 atom_chars(A,X),
 write(X),
 fun(X).

fun([]).

fun([A]):-
 not(minu(A)).

fun([Hd|Tail]):-
 not(minu(Hd)),
 fun(Tail).

Upvotes: 1

Views: 35

Answers (1)

Daniel Lyons
Daniel Lyons

Reputation: 22803

I'd try this:

minu('-').
minu('+').
minu('/').

f(A) :- 
  atom_chars(A, Chars),
  \+ (minu(Op), memberchk(Op, Chars)).

By the way, it's important to note that Prolog will not "return" anything; it will either tell you true or false if the goal succeeds or not, so for instance:

?- f("this is a string").
true.

?- f("this i-a string").
false.

?- f("this i+a string").
false.

Upvotes: 2

Related Questions