Sign in to follow this  
speciesUnknown

Does this exist in any language?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
seems like a big

if(
condition &&
condition1 &&
... &&
condition1337)
code();
else
othercode();

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

#define CHECKLIST { if(true
#define CONDITION &&
#define ON
#define SUCCESS ) {
#define END }
#define FAIL else {

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 = c
let 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 *))
END
END



[Edited by - Julian90 on August 3, 2007 6:53:00 AM]

Share this post


Link to post
Share on other sites
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))

Share this post


Link to post
Share on other sites
with the following:

if(
(condition)
&& (condition)
&& (condition)
&& (condition)
)
{



}
else
{


};

does the compiler know to fail the test when one of the conditions is false? That would be part of the language specification.

Share this post


Link to post
Share on other sites
Quote:
Original post by speciesUnknown
does the compiler know to fail the test when one of the conditions is false? That would be part of the language specification.


In C and C++, yes, it's indeed part of the language specification for &&.

Share this post


Link to post
Share on other sites
Quote:
Original post by speciesUnknown
with the following:

if(
(condition)
&& (condition)
&& (condition)
&& (condition)
)
{



}
else
{


};

does the compiler know to fail the test when one of the conditions is false?

Yes, it does. It's called short-circuit evaluation. && is a short-circuit operator.

Quote:
That would be part of the language specification.

You want to make a whole language just for this, which can already be easily done with existing languages?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this