The Oz Compiler launch an exception "Missing Else Clause" when I try to compile this code, can anybody tell me why? Here is my code :
declare
fun {Numero x y}
local NumeroAux in
fun {NumeroAux x y Acc}
if {And (x == 0) (y == 0)} then Acc
else
if y == 0 then {NumeroAux 0 x-1 Acc+1}
else
{NumeroAux x+1 y-1 Acc+1}
end
end
end
{NumeroAux x y 0}
end
end
{Browse {Numero 0 0}}
In my opinion, there isn't a missing else clause in this code! My function will always return something.
In Oz language, all the variables need a uppercase. Lowercase is for atom's.
So if you try to run your code with uppercases:
declare
fun {Numero X Y}
local NumeroAux in
fun {NumeroAux X Y Acc}
if {And (X == 0) (Y == 0)} then Acc
else
if Y == 0 then {NumeroAux 0 X-1 Acc+1}
else
{NumeroAux X+1 Y-1 Acc+1}
end
end
end
{NumeroAux Y X 0}
end
end
{Browse {Numero 0 0}}
This will browse 0 as expected.
Related
I am writing a code in julia but I am unable to call a function from another function. Code is:
function add(x, y)
if x == 3 && y ==1
z =0
else x == 0 && y ==0
z =1
end
return z
end
function width(a, b, c)
add(x,y)
.....
end
The variables in add function will be used in width function but as I am new to julia, I am unable to call add in the other function. Kindly guide.
Edit:
I tried declaring with the z but it also didn't worked
struct z
a::Int
b::Int
end
There are two problems in your code that are not related to Julia per se. First problem in the add function: if x == 3 && y == 1 the output should be z = 0, else if x == 0 && y == 0, actually the if was missing, the output should be z = 1. Now what will be the output if, e.g., x = 1 && y == 1? The answer is nothing and z will be undefined.
To fix the add function, you should add a default branch for the if-else.
function add(x, y)
if x == 3 && y == 1
z = 0
elseif x == 0 && y == 0
z = 1
else
z = -1 # some default
end
return z
end
The same function could be written more concisely as:
function add(x, y)
x == 3 && y == 1 && return 0
x == 0 && y == 0 && return 1
return -1 # some default
end
which can even be written in a one-liner like this:
add(x, y) = x == 3 && y == 1 ? 0 : x == 0 && y == 0 ? 1 : -1 # some default
The second problem is with the width function. x and y are not defined inside the body of the width function. So, you can't call add(x, y). It should be z = add(a, b) where z should be used in subsequent calculations. Finally, check what the third argument c is for, otherwise, remove it.
function width(a, b, c)
z = add(a, b)
.....
end
Using the Hoare-Rules I want to show that I can imply
{x >= 0} --> {a + y = x}
PROGRAMM
// PRECONDITION
{x >= 0}
a = x;
y = 0;
// POSTCONDITION
{a + y = x}
Using the assignment rules I get
// PRECONDITION
{x >= 0}
{x + 0 = x} // assignment rule
a = x;
{a + 0 = x} // assignment rule
y = 0;
// POSTCONDITION
{a + y = x}
To show
{x >= 0} --> {a + y = x}
therefore I need to show in a last step
{x >= 0} --> {x + 0 = x}
How can I show this or what is wrong in my proof?
Your reasoning is correct.
To prove an implication formally, argue as follows:
Assume the antecedent, x >= 0
By Additive identity, we have x + 0 = x
By implication introduction (from 1 and 2) we have x >= 0 --> x + 0 = x
Does this code work as a xor function in Lua?
function xor (a,b)
if a ~= b then return true else return false end
end
i = false
j = false
if xor(not i, not j) then
print("one exclusive")
else
print("both or none")
end
Yes, your code works.
If a and b contain boolean values, then a XOR b is the same as not(a == b), which of course is the same as
a ~= b.
I'm trying to sum all numbers from 1 to 1000 that are either divisible by 3 or 5.
The first attempt is straight forward:
ans1 = 0
for x in 3:999
ans1 += x % 3 == 0 || x % 5 == 0 ? x : 0
end
When I try the same approach using function chaining, it fails to return the answer I expect, it instead returns 0.
ans2 = [3:999] |> x -> x % 3 == 0 || x % 5 == 0 ? x : 0 |> sum
I believe the problem is the center function, since the code below prints all values within the range of 3 to 999. So i know there is no problem with iteration.
[3:999] |> x -> println(x)
Could anyone please help me.
I discovered the reason was because I did not understand the type being parsed. Here is an example:
[3:999] |> println(typeof(x)) # Array{Int64,1}
Meaning the value being parsed is an array of integer64. So evaluating the following:
[1:999] % 3 == 0 # false
So my answer was to instead use the filter function, here is an example:
ans3 = sum(filter(x -> x % 3 == 0 || x % 5 == 0,[1:999]))
The final answer using function chaining is:
ans4 = [1:999] |> x -> filter(y -> y % 3 == 0 || y % 5 == 0,x) |> sum
Which evaluates to the expected answer.
I want to create Haskell function with different return value from its parameter, example:
I want function isOdd 3 return value either True or False.
I've tried
isOdd :: Integer -> Bool
isOdd x = (if x mod 2 == 0 False else True)
but it returns an error, can anybody help me? Also, is there a tutorial about functions in Haskell? I can't find a good tutorial about function in haskell.
isOdd :: Integer -> Bool
isOdd x = (if x mod 2 == 0 False else True)
You don't need the parens:
isOdd :: Integer -> Bool
isOdd x = if x mod 2 == 0 False else True
You missed out then:
isOdd :: Integer -> Bool
isOdd x = if x mod 2 == 0 then False else True
As you are using mod as an operator, you must surround it with backticks:
isOdd :: Integer -> Bool
isOdd x = if x `mod` 2 == 0 then False else True
That works.
Furthermore, you can write if blah then False else True more simply as not (blah):
isOdd :: Integer -> Bool
isOdd x = not (x `mod` 2 == 0)
Even more simply:
isOdd :: Integer -> Bool
isOdd x = x `mod` 2 /= 0
Please note: this is practically the same as the standard odd function.
Which tutorials have you tried?
Learn You A Haskell has a chapter introducing functions.
The problem here is that mod isn't infix. Additionally, in Haskell if statements work like this
if cond then expr1 else expr2
Notice the then.
You should use mod like this mod x 2. However you can make it infix like this:
x `mod` 2
On a side not
x `mod` 2 /= 0
Is much easier to read than the whole if statement.
As far as tutorials: Learn You a Haskell for Great Good is a good start. For a more in depth coverage Real World Haskell is excellent.
If you just want to find a function then Hoogle is your friend.