Link to home
Start Free TrialLog in
Avatar of kuntilanak
kuntilanakFlag for United States of America

asked on

parsing in prolog

I am having a difficult time to create a parser in prolog that will allow me to do this:

say I have a database of vowels:

vowel(a).
vowel(i).
vowel(u).
vowel(e).
vowel(o).

I want to create a function called match([vowel], [.....]). The dotted lines means that it's a  list of characters that will be tested if they are vowels or not. The function will return true if the entire character list contains one or more vowels found in the database. An example of an complex expression:

match([vowel], [e,e,e])

will result in:

true?;
true?;
true?;
no

notice that it returns true three times as vowel can match one or more vowels.. it can match [e]. [e,e] and [e,e,e]. I am having a difficult time coding it... can someone please help me out?

another example to make it much clear:

match([vowel], [e,e,k])

results in:

true?;
true?;
no

one other thing to consider is that I will have another patttern as well besides vowel, one of them is called cons1, which matches exactly one consonant.. we can combine both patterns, vowel and cons1. As an example:

match([vowel, cons1], [a,e,e,k]), will return:

true?;
no


that's the part where I am confused at... so far my code is

which works if pattern is only vowel, but if you put vowel and cons1 (i.e. like the example above) it doesn't work.. that's where I need help I think..
prefix_match([vowel|T], [H|_] ) :- vowel(H).
prefix_match([vowel|T], [H|T1] ) :- vowel(H), prefix_match([vowel|T], T1).

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial