Does this exist in any language?

Started by
12 comments, last by speciesUnknown 16 years, 8 months ago
I just came up with this, similar to a case statement. Kind of an alternative to nested IF statements. Has anybody seen this before? Or did I just invent something...

CHECKLIST

  CONDITION(boolean logic)

  CONDITION(boolean logic)

  CONDITION(boolean logic)

  CONDITION(boolean logic)

  CONDITION(boolean logic)

  CONDITION(boolean logic)

  ON SUCCESS

  END

  ON FAIL
  
  END

END

Basically, the statement checks through each piece of boolean logic in the order they are presented. If all logics are true, the code within ON SUCCESS block exexcutes. On the first failure, code within ON FAIL block executes.
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!
Advertisement
seems like a big
if(     condition &&     condition1 &&     ... &&     condition1337)         code();else         othercode();
I've seen similar enough things done in C++ and C# via functor aggregation. I'd imagine functional languages do this sort of thing more often in their own style.
There are several languages that have this. For example:

Common Lisp:
(cond (condition1 ...)      (condition2 ...)      (condition3 ...)      ...      (t ...))

Scheme:
(cond (condition1 ...)      (condition2 ...)      (condition3 ...)      ...      (else ...))

Haskell:
fn arg1 arg2 ... argn  | condition1 = expression1  | condition2 = expression2  | condition3 = expression3  ...  | otherwise = expressionn


It's called a guard in some languages (such as Haskell).

EDIT: I misread the OP. What you are talking about is a simple collection of and-statements.

[Edited by - Roboguy on August 2, 2007 11:58:13 PM]
It's all semantics.

Replace "CHECK LIST" with "if", and replace most occurrences of "CONDITION" with "&&", etc.
#define CHECKLIST      { if(true#define CONDITION      &&#define ON#define SUCCESS        ) {#define END            }#define FAIL           else {
Or if you prefer...
#define CHECKLIST    bool b=false; do {#define CONDITION(c) if (!(c)) break;#define ON#define SUCCESS      b = true;#define END          } while(false);#define FAIL         if (!b) do { do {
As a matter of fact I just used such a construct today
Of course I didn't use these defines to accomplish the task.

Edit: Missed the ON FAIL bit. Okay okay, so mine now doesn't work if you use it twice in the same function. That's what you get for using END to mean two different things.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Edit: Missed the ON FAIL bit. Okay okay, so mine now doesn't work if you use it twice in the same function. That's what you get for using END to mean two different things.


Thats simple to fix :P
#define CHECKLIST    if (bool b=false); else do {#define CONDITION(c) if (!(c)) break;#define ON#define SUCCESS      b = true;#define END          } while(false);#define FAIL         if (!b) do { do {


Edit: A quick F# version
let CONDITION c = clet ON = ()let SUCCESS = ()let END = ()let FAIL = ()let CHECKLIST conditions () () on_success () () () on_fail () () =  if List.forall (fun c -> c) conditions  then on_success ()  else on_fail ()CHECKLIST  [ CONDITION(boolean logic)  ; CONDITION(boolean logic)  ; CONDITION(boolean logic)  ; CONDITION(boolean logic)  ; CONDITION(boolean logic)  ; CONDITION(boolean logic)  ]  ON SUCCESS    (fun () -> (* logic *))  END  ON FAIL    (fun () -> (* logic *))  ENDEND


[Edited by - Julian90 on August 3, 2007 6:53:00 AM]
As you might have gathered by now, taking an if-statement and renaming it doesn't actually turn it into anything new. [wink]
I would make a Common Lisp implementation of your syntax, but it would be significantly more complicated than just doing this:
(if (and condition1         condition2         condition3         condition4         condition5         condition6)  (progn    "On success" code goes here)  (progn    "On fail" code goes here))

This topic is closed to new replies.

Advertisement