Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In Prolog, there is a neat technique that lets you "comment out" any goal of a clause, including the last one, in a completely uniform way.

The trick is to define * as a prefix operator, and then define the predicate ( * )/1 to generalize away its argument. You only need two lines to do it:

    :- op(920,fy, *).
    *_.
Now, you can put * in front of any goal to "remove" it. For example:

    pred :-
         true,
         * false.
With this definition, ?- pred. succeeds.

In Erlang, you cannot easily replicate this. You can try parse transformations to rewrite code at compilation time:

http://erlang.org/doc/man/compile.html

In Elixir, defining otherwise unused operators is generally discouraged:

https://hexdocs.pm/elixir/master/operators.html

For exchanging lines, an editor macro may be useful for Erlang and also for Prolog.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: