I am trying to create a new operator in the Io language, but I've noticed different behavior when I check my work with an interpreter versus file execution:
## Executing nand.io
##########################
OperatorTable addOperator("nand", 10)
true nand := method(bool, if(bool, false, true))
false nand := method(bool, true)
true nand false println # => false
true nand true println # => true
false nand false println # => false
false nand true println # => true
# Why does this print 'true' (it should be 'false')?
(true nand true) println # => true
# I noticed that the argument to nand is always printed
true nand "not" println # => not
true nand "what" println # => what
false nand "I" println # => I
false nand "expected" println # => expected
# Another sanity check. I thought this might have to do with println behaving
# oddly, but this conditional really does return true
if(true nand true, "This should not print" println, "This should print" println)
# => This should not print
# More unexpected behavior
false nand := method(arg, "foo")
false nand "bar" println # => "bar"
true nand := method(arg, "foo")
true nand "bar" println # => "bar"
false nand := "Why is this not returned?"
false nand "aaaaahhhh" println # => "aaaaahhhh"
## Ran in the io interpreter
###############################
OperatorTable addOperator("nand", 10)
true nand := method(bool, if(bool, false, true))
false nand := method(bool, true)
# Same as file execution when sent to println
true nand false println # => false
true nand true println # => true
false nand false println # => false
false nand true println # => true
# It acually does what's expected here
true nand false # => true
true nand true # => false -- It works here!!!
false nand false # => true
false nand true # => true
if(true nand true, "This should not print" println, "This should print" println)
# => This should print
Maybe this has to do compilation vs interpretation? Is this actually expected behavior and maybe I'm missing something fundamental about the language (I only started learning Io today)?
Ok so this has to do with the way the VM parses operators. The first pass of the file will go through and rewrite any operators it knows about already, rewriting the messages in relation to the number supplied as precedence. The second pass will then execute that modified message tree. Because of this, when you add a new operator to the operator table in a file that you later pass through the VM, your new operator will only be available to any files or modules you load after that. For the life of that message tree that was produced from that file, your operator doesn't exist.
Basically the rule of thumb here is to maintain a separate operators.io file, and your main io file separate; then ensure operators.io gets loaded before main.io, and you'll be all set.
The reason the CLI doesn't have this problem is because it evaluates messages one by one. That is to say, the next message you type in will have the new operator st
Related
I'd like to change the function boolfunction by adding an extra case for the true,true match case. Now the function returns true for the case true,true and I want to code that this case can be false as well. I wonder if there is a way to code this in Coq.
Definition boolfunction (c d : bool) : bool :=
match c,d with
| _, false=> true
|false, true => false
| true,true => true
end.
A function can produce a different type instead of bool. If you want to produce more than one value, one way is to produce a list of values. That is one way of representing a nondeterministic function.
Require Import List.
Import ListNotations.
Definition boolfunction (c d : bool) : list bool :=
match c, d with
| _, false => [true]
| false, true => [false]
| true, true => [true, false]
end.
Without more details about what you are looking for it's hard to be more accurate.
I am trying to solve a beginner problem but can't reach the solution:
If any duplicates in list, return true, else false. Empty lists considered.
def duplicates(a: List[Int]): Boolean = {
case Nil => false
case x :: xs =>
if(xs.contains(x)) true else false
}
But this doesn't work. And it's not recursive. It's just something where I wanted to start but I'm stuck. Please kindly help and try to avoid non-beginner solutions if reasonable.
You need to call your function recursively. So if xs doesn't contain x, then call the function but with the remaining list.
def duplicates(a: List[Int]): Boolean = a match {
case Nil => false
case x :: xs =>
if(xs.contains(x)) true else duplicates(xs)
}
I just noticed that in Scala Boolean supports both & and &&. Is there a difference between these two operators? The Scala docs use the exact same description for both of them, so I wasn't sure.
& and | are strict while && and || are short-circuiting:
false && (throw new Exception()) => false
false & (throw new Exception()) => ex
true || (throw new Exception()) => true
true | (throw new Exception()) => ex
The full documentation for & and | have a note explaining this behaviour:
This method evaluates both a and b, even if the result is already
determined after evaluating a.
This is a my function
st3 = (x, y) ->
console.log "#{x?}, #{y?}"
if [x?, y?] is [true, true] # <- this is the line
'good'
'bad'
This is the output
true, true
bad
I want to be able to do a tuple comparison like it is in python.
In python, the if can be written roughly as
if (x, y) == (True, False):
return 'good'
The coffescript if loop is translated into javascript as such
if ([x != null, y != null] === [true, true]) {
'good';
}
That's why this will not evaluated to true.
Is there any alternative way to express it in coffeescript?
If you want to check to see if all of your arguments are not None, then I would do this:
def check_all(*args):
return all(arg is not None for arg in args)
If you wanted to check if they're all True (like literally True) then you could use
def check_all(*args):
return all(arg is True for arg in args)
If you wanted to take in a list (instead of variable number of parameters, remove the asterisk.
Both of the following print false. The desired behaviour is for the first to print true and the second to print false. I'm not sure why false always gets printed.
def myTest(filter: => Boolean) = () => {
if (filter) {
// do something here
true
}
false
}
println(myTest(5 > 3)())
println(myTest(5 > 7)())
This expression:
if (filter) {
true
}
is an expression that returns true when filter evaluates to true or Unit () otherwise.
This block:
{
expr;
false
}
is a block that evaluates expr, discards it, then returns false.
So:
{
if (filter) {
true
}
false
}
always returns false. Think of if (filter) { true } like expr;
huynhjl gives the syntactic reason. The phenomenological reason is that you forgot the else before the false.
scala> def myTest(filter: => Boolean) = () => {
|
| if (filter) {
| // do something here
| true
| }
| else false
| }
myTest: (filter: => Boolean)() => Boolean
scala> println(myTest(5 > 3)())
true
scala> println(myTest(5 > 7)())
false
The last statement is a false, so it returns false, you'd need an else in the if expression to conditionally execute that statement.
Just like to point out that your filter parameter already resolves a boolean so the if expression is entirely redundant, your construction can be rewritten simply as:
def myTest(filter: => Boolean) = () => filter
which may lead to further existential questions.