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.
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
Can a "all or nothing" boolean expression be simplified? Suppose I have three values, A, B, C, and want to determine if all three are true, or all three are false. Like a XOR gate, but with N values.
Can this statement be simplified?
(A && B && C) || !(A || B || C)
All true or all false basically means that all should be the same. So if the equality comparison is acceptable you can do this:
A == B && B == C
For the function
function function_maker(N)
if N == 1
x = 1.0
else
x = 1
end
f(y) = x+y
end
I want the output of this to not be type-stable, but I want it to generate an f that is type-stable, i.e. uses the type of x determined by the value of N to generate a function dependent on N. Basically, I want the functions that come out of this to be performant, but the function_maker itself doesn't need to be performant because it's only used in the global scope or above a function barrier.
f = function_maker(1)
#code_warntype f(1)
Variables:
#self#::#f#9
y::Int64
Body:
begin
return ((Core.getfield)((Core.getfield)(#self#::#f#9, :x)::ANY, :contents)::ANY + y::Int64)::ANY
end::ANY
This doesn't happen by default. I tried f(y) = x::typeof(x)+y but that didn't work either. Is there a simple way to do this?
There's:
julia> function function_maker2(N)
if N == 1
let x = 1.0
return f(y) = x + y
end
else
let x = 1
return f(y) = x + y
end
end
end
function_maker2 (generic function with 1 method)
julia> f2 = function_maker2(1)
(::f) (generic function with 1 method)
julia> #code_warntype f2(1)
Variables:
#self#::#f#5{Float64}
y::Int64
Body:
begin
return (Base.add_float)((Core.getfield)(#self#::#f#5{Float64}, :x)::Float64, (Base.sitofp)(Float64, y::Int64)::Float64)::Float64
end::Float64
This version separates the x in each branch inside let blocks. Otherwise the compiler seems to get confused.
I have this code:
esprimo :: Int->Bool
esPrimo x = if length (div x x) == 2 then True else False
But I pulled the error is above
In addition to what sibi said, I think what you are trying to do is this:
isPrime :: Int -> Bool
isPrime x = if length [d | d <- [1..x], x `mod` d == 0] == 2 then True else False
this is basically the direct translation of the mathematical concept of beeing prime into Haskell.
As you don't need the if as it checks the same == already returns a bit more readable might be:
isPrime :: Int -> Bool
isPrime x = length divisors == 2
where divisors = [d | d <- [1..x], x `isMultipleOf` d]
isMultipleOf m n = m `mod` n == 0
Please note that this is of course not the most performant prime-test.
The exact reason for your error is because of the different cases you have used in the type signature and the type definition:
esprimo :: Int -> Bool -- p should be capital here to work.
esPrimo x = if length (div x x) == 2 then True else False
Haskell is case sensitive, so esprimo and esPrimo are different. That being said there is other type error in your code: the type of div is div :: Integral a => a -> a -> a, so it returns a and you are applying length function on it. But length function only accepts list i.e [a] and not a which will produce you type error.
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.