Skip to main content
Commonmark migration
Source Link
improved phrasing
Source Link
0 '
  • 3.8k
  • 1
  • 22
  • 32

With the unary operators + and -, Prolog will not interpret +20 or -20 as numbers instead of a call to a +/1 or -/1 predicate but instead as a number. Predicates given unary + or - as names can still be called on number by using parentheses (+(20), -(20)) or if. If avoiding the extra bytes from parentheses is desirable other unary operators such as \, $, etc can be used as names instead.

With the unary operators + and -, Prolog will not interpret +20 or -20 as a call to a +/1 or -/1 predicate but instead as a number. Predicates given unary + or - as names can still be called on number by using parentheses (+(20), -(20)) or if avoiding the extra bytes from parentheses is desirable other unary operators such as \, $, etc.

With the unary operators + and -, Prolog will interpret +20 or -20 as numbers instead of a call to a +/1 or -/1 predicate. Predicates given unary + or - as names can still be called on number by using parentheses (+(20), -(20)). If avoiding the extra bytes from parentheses is desirable other unary operators such as \, $, etc can be used as names instead.

Added info on how this golf can be used with DCGs
Source Link
0 '
  • 3.8k
  • 1
  • 22
  • 32

Corollary

Since DCGs are syntactic sugar for predicates they too can be given operators for names. This works as expected when calling them as DCGs either from a DCG or using the phrase predicates or others that are designed to work with DCGs. When calling them as predicates parentheses are required (eg A+B-->... must be called like +(A,B,...)) since DCG predicates take an additional two arguments for their difference lists. For operator named DCGs with more than two arguments using operator pattern matching then it is important to make sure when calling it as a predicate that the pattern matched operators are distributed correctly.

Giving operator names to DCGs that take no additional arguments can be useful if you need to call them within your program since then you can do so without using parentheses. Caution is required because it can be the case that what you save in parentheses you can lose to added spacing required to parse adjacent operators.

CaveatsExamples

/ -->a+b+X,X+d+e.
A+B+C-->[A],[B],[C].


X/[],member(c,X),phrase(f+o+o,Y),+(b+a,r,Z,[]).

Output will be

X = [a, b, c, c, d, e],
Y = [f, o, o],
Z = [b, a, r].

Try it online!

Caveats

Caveats

Corollary

Since DCGs are syntactic sugar for predicates they too can be given operators for names. This works as expected when calling them as DCGs either from a DCG or using the phrase predicates or others that are designed to work with DCGs. When calling them as predicates parentheses are required (eg A+B-->... must be called like +(A,B,...)) since DCG predicates take an additional two arguments for their difference lists. For operator named DCGs with more than two arguments using operator pattern matching then it is important to make sure when calling it as a predicate that the pattern matched operators are distributed correctly.

Giving operator names to DCGs that take no additional arguments can be useful if you need to call them within your program since then you can do so without using parentheses. Caution is required because it can be the case that what you save in parentheses you can lose to added spacing required to parse adjacent operators.

Examples

/ -->a+b+X,X+d+e.
A+B+C-->[A],[B],[C].


X/[],member(c,X),phrase(f+o+o,Y),+(b+a,r,Z,[]).

Output will be

X = [a, b, c, c, d, e],
Y = [f, o, o],
Z = [b, a, r].

Try it online!

Caveats

Restructured tip to improve clarity
Source Link
0 '
  • 3.8k
  • 1
  • 22
  • 32
Loading
Source Link
0 '
  • 3.8k
  • 1
  • 22
  • 32
Loading