-
Notifications
You must be signed in to change notification settings - Fork 2
Conditionals and Bits
In DeviousYarn, bits are represented using two keywords: on and off. Bits can be operated on using a few simple functions.
The not
operator takes one argument, and returns the opposite of it. This means not:on
is the same as off
.
The or
operator takes a multitude of arguments and returns on
if any of the arguments evaluate to on
.
The or
operator takes a multitude of arguments and only returns on
if every single argument within it does not evaluate to off
.
In DeviousYarn, conditionals are just another function. We're going to learn all of them, and we're going to do it fast, okay?
Now, this one might be a little bit weird to most programmers, but you'll learn to love it. This function, based on the LISP function of the same name, continues to evaluate every argument until one of them returns an on
. If none of the arguments return on
, an error is thrown. You'll see a function called any
later which works kind of like the cond
function.
cond {
if { divisible(30 4)
x = divide(30 4) }
if { divisible(9 3)
x = divide(9 3) }
if { divisible(10 2)
x = divide(10 2) }
}
print( x ' was the result of the divide.\n' )
The if
or ?
conditional allows you to check the value of a bit, which is the first argument, and if it's set to on
, run through a list of statements and evaluate them. Lets take a look at a simple example.
if { divisible(6 3)
out : 'Everything is working as planned' }
The elf
, alf
, else
, and also
conditionals in DeviousYarn are not statements. They are individual expressions. The conditionals inter-communicate in a long distance
way. This means you can call an if
from one function, then an elf
from another, or even asynchronously call an if
and an elf
. If you want a less abstract, more procedural-friendly way of using conditionals, you should really REALLY be using the condition
or cond
function.
The elf
or -?
conditional, if the last conditional was evaluated and evaluated to false, will then function like an if
statement. 🎄 🎅 ⛄
if { divisible(6 5)
out : 'Six is not divisible by five.' }
elf { divisible(6 3)
out : 'Six is divisible by three.' }
The alf
or &?
conditional, if the last conditional was used, will also be used. This is the opposite of the elf
.
if { divisible(6 2)
out : 'Six is divisible by two.' }
alf { divisible(6 3)
out : 'Six is also divisible by three.' }
The else
or --
conditional does not check a bit value. It simply runs if no proceeding conditionals did.
if { divisible(6 5)
out : 'Is six divisible by five?' }
else {
out : 'It seems not.' }
The also
or &&
conditional does not check a bit value either. It simply runs if the proceeding conditionals also did. What you might ask yourself is "Why would I need that when I can simply put my statements in the last conditional?" There are many uses for the also conditional, but here is a simple one.
if { divisible(6 2)
out : 'Is six divisible by two?' }
out : 'This statement will always run.'
also {
out : 'It seems so.' }