Hacker News new | past | comments | ask | show | jobs | submit login

In Prolog :- is the if operator.



Its use is kind of a code smell, and I believe it was a relatively (prolog is old) late addition.

In any case, I wrote "doesn't need", though perhaps you consider that hair splitting.


I think you misinterpreted that. Prolog does has an if operator -> as in ( P -> If ; Else ) yes, but oldsecondhand said :-

"predicateA is true" if "clauseB is true" and "clauseC is true".

or in prolog

  predicateA :-
    clauseB,
    clauseC.
so :- is if, just in it's own, Prolog-y way.


Is that an "if", though? In C/C++, you could write that as:

  if (clauseB && clauseC)
    predicateA = true;
  else
    predicateA = false;
which is clearly an "if". Or you could write it as:

  predicateA = claseB && clauseC;
which is not an "if" at all, but just a boolean calculation. (Unless you regard all boolean calculations as "if"s in disguise...)

The prolog version seems to me to be more in the spirit of the second C version.


Well if nothing has any arguments then yeah, it's basically the second version. but if you had arguments you end up with things like

  %foo(+A,-B)

  foo(A,42) :-
     A < 10.
  foo(A,24) :-
     A >= 10.
  foo(_,100).
which would be

  bool foo(int *A, int *B){
     if (*A < 10){
         *B = 42;
     }elseif (*A >= 10){
         *B = 24;
     }else{
         *B = 100;
     }

     return true;
  }  /* my pointer knowledge is rusty though, so grain of salt */




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: