Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SOME PROLOG EXERCISES I NEED HELP WITH

Status
Not open for further replies.

emerald123

Technical User
Oct 22, 2015
1
0
0
US
I am stumped on this for past few weeks.
I am still learning Prolog and a beginner.

I do not wish to use cut or any non-logical Prolog built-ins as of now :)

So maybe someone could explain me the solution.


?- regularexpr_parse("([a-z][a-z0-9_]*)", S).
S = concat(class(97, 122),
Kclosure(alt(class(48, 57),
alternate(class(95, 95),
classRegularExpr(97, 122)))))
.

Kclosure -> kleene closure of regular expressions
alernate -> alternation of regular expressions
concat -> concatenation of regular expressions
classRegularExpr(low, high)
low & high if are non-negative integers < 256, this term represents a character class representing the characters between low & high inclusive.

In this problem, the term regularexpr_match is used to denote a
regular expression matching a (regex)string with RestExpr unifying with the remaining suffix of the string.
regularexpr_find represents a regular expression which matches a substring of (regex)string with RestExpr unifying with the remaining suffix of the string.


?- regularexpr_parse("([a-z])*", S), regularexpr_match(S, "abc2bc3", RestExpr).
S = Kclosure(classRegularExpr(97, 122)),
RestExpr = [50, 98, 99, 51] ;
S = Kclosure(classRegularExpr(97, 122)),
RestExpr = [99, 50, 98, 99, 51] ;
S = Kclosure(classRegularExpr(97, 122)),
RestExpr = [98, 99, 50, 98, 99, 51] ;
S = Kclosure(classRegularExpr(97, 122)),
RestExpr = [97, 98, 99, 50, 98, 99, 51] ;
false.

?- regularexpr_parse("[0-9][0-9]*", S), regularexpr_find(S, "abc123c", RestExpr).
S = concat(classRegularExpr(48, 57), Kclosure(classRegularExpr(48, 57))),
RestExprconc(classRegularExpr(48, 57), Kclosure(classRegularExpr(48, 57))),
RestExpr = [51, 99] ;
S = concat(classRegularExpr(48, 57), Kclosure(class(48, 57))),
RestExpr = [50, 51, 99] ;
S = concat(classRegularExpr(48, 57), Kclosure(class(48, 57))),
RestExpr = [99] ;
S = concat(classRegularExpr(48, 57), Kclosure(classRegularExpr(48, 57))),
RestExpr = [51, 99] ;
S = concat(classRegularExpr(48, 57), Kclosure(class(48, 57))),
RestExpr = [99] ;
false.

I will post other problems later :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top