What is up with the cut (!)

With the new “Picat style matching”, we can write if_then_else as:

if_then_else(Test, Then, _Else), Test => Then.
if_then_else(_Test, _Then, Else) => Else.

(untested code, no warranty, etc. etc.)

Here’s an example of max_member_/3, rewritten with => style:

max_member_([], Max0, Max) =>
    Max = Max0.
max_member_([H|T], Max0, Max), H @=< Max0 =>
    max_member_(T, Max0, Max).
max_m_([H|T], _Max0, Max) =>
    max_member_(T, H, Max).

The original code (using if-then-else) was:

max_member_([], Max, Max).
max_member_([H|T], Max0, Max) :-
    (   H @=< Max0
    ->  max_member_(T, Max0, Max)
    ;   max_member_(T, H, Max)
    ).

There’s a bit more discussion at How to write max_member/2 with => (but ignore the digression about “lazy lists”).

1 Like