Hello I am trying to create a program that has a function main_function() that holds two int variables and then passes the variables to two other functions difference() and sum(). I want the two functions perform the computation and display the results. In turn calling each of the two functions from the main_function(). However I am currently having an issue with my program only outputting the bottom most function that is being called in the main_function()
Here is what I have
-module(numbers).
-export([main_function/2]).
main_function(X,Y)->
sum(X,Y),
difference(X,Y).
sum(X,Y)->
X + Y.
difference(X,Y)->
X - Y.
My output for this would be 2 if I was to pass 5 and 3 would for X and Y respectively and my program seems to be only using the difference() function and not sum(). I am looking for an output of 8 and 2.
Any help is greatly appreciated
Thanks
You can change main_function/2 like below
main_function(X,Y)->
A = sum(X,Y),
B = difference(X,Y),
{A, B}.
The result in shell when X = 5, Y = 3 is:
{8, 2}
Or like this
main_function(X,Y)->
A = sum(X,Y),
B = difference(X,Y),
io:format("A = ~p~nB = ~p~n", [A, B]).
The result in shell when X = 5, Y = 3 is:
A = 8
B = 2
Related
i'm new to lua and programming in general and i'd like to find out if its possible to make sure that all my 6 variables are different every time. why isnt this working, and is there a more efficient way to make sure my 6 variables are different every time?
local x = math.random(1,6)
local y = math.random(1,6)
local z = math.random(1,6)
local a = math.random(1,6)
local b = math.random(1,6)
local c = math.random(1,6)
if x or y or z or a or b or c == x or y or z or a or b or c then
repeat
y = math.random(1,6)
z = math.random(1,6)
a = math.random(1,6)
b = math.random(1,6)
c = math.random(1,6)
until x or y or z or a or b or c ~= x or y or z or a or b or c
print(x.." , "..y.." , "..z.." , "..a.." , "..b.." , "..c)
else
print(x.." , "..y.." , "..z.." , "..a.." , "..b.." , "..c)
end
Since the number of possible results is the same as the number of variables, you could just make a randomly sorted table of those results and store the contents into the variables. The following code takes the numbers 1 through 6 and stores each one at a random index in an array:
local rolls = {}
for i = 1, 6 do
table.insert(rolls, math.random(#rolls + 1), i)
-- See the sorting in action
print(table.unpack(rolls))
end
local x, y, z, a, b, c = table.unpack(rolls)
print(x, y, z, a, b, c)
As for why your code doesn't work, it's hard for me to understand how you intended it to work. You seem to be treating or as something other than a simple binary operator. You can read about it here.
calling
math.randomseed(os.time())
before every math.random call will do te trick.
os.time() is by default different everytime you call it, and by setting the seed differently, it will give different results.
x = {1, 2, 3}
y = {4, 5, 6}
z = x + y
I have two tables x and y and just want to create a third one which is just the sum of elements in them. I use the above LUA code in an effort but this gives error input:3: attempt to perform arithmetic on a table value (global 'x')...
Like, I want the result z = {5, 7, 9}
Please suggest functions that will be helpful, or please help me form such a function in LUA.
Thanks
Yes, iterate and check with table.concat()
do...
x = {1, 2, 3}
y = {4, 5, 6}
z = {}
-- First check same table length and if so then add sums to z table
if #x==#y then
for i=1,#x do
z[i]=x[i]+y[i]
end
end
print(table.concat(z,' '))
-- puts out: 5 7 9
...end
You cannot add tables in Lua unless you implement the __add metamethod.
For an element wise sum of two sequences simply do this:
function sumElements(t1,t2)
local result = {}
for i = 1, math.min(#t1, #t2) do
result[i] = t1[i] + t2[i]
end
return result
end
of course you should verify your inputs and think about how you want to deal with mismatching table sizes. Let's say t1 has 3 elements and t2 has 5, will you just have 3 result values or will you add 0 to the remaining 2?
I have following code snippet that use currying:
let multiply x y = x * y
let double = multiply 2
let ten = double 5
I understand the above code, because I remember this code:
Currying is converting a single function of n arguments into n
functions with a single argument each
And then I encounter the following code:
let double2 z = multiply 2 z
double2 5
I do not understand this code at all. Why double2 can be a function?
What's happening in your latter example is really nothing special.
You can basically read it as:
define a function double2 with one argument z,
which is defined as multiply 2 z
Some people might refer to this as "currying" or "partial function evaluation" but really all that's happening here is that you're defining a function that uses another function in its function body.
let double2 z = multiply 2 z
// ^ here you define a parameter
This turns it into a function.
I am very green when it comes to F#, and I have run across a small issue dealing with recursive functions that I was hoping could help me understand.
I have a function that is supposed to spit out the next even number:
let rec nextEven(x) =
let y = x + 1
if y % 2 = 0 then y
else nextEven y
// This never returns..
nextEven 3;;
I use the 'rec' keyword so that it will be recursive, although when I use it, it will just run in an endless loop for some reason. If I rewrite the function like this:
let nextEven(x) =
let y = x + 1
if y % 2 = 0 then y
else nextEven y
Then everything works fine (no rec keyword). For some reason I though I needed 'rec' since the function is recursive (so why don't I?) and why does the first version of the function run forever ?
EDIT
Turns out this was a total noob mistake. I had created multiple definitions of the function along the way, as is explained in the comments + answers.
I suspect you have multiple definitions of nextEven. That's the only explanation for your second example compiling. Repro:
module A =
let rec nextEven(x) =
let y = x + 1
if y % 2 = 0 then y
else nextEven y
open A //the function below will not compile without this
let nextEven(x) =
let y = x + 1
if y % 2 = 0 then y
else nextEven y //calling A.nextEven
Try resetting your FSI session.
I am playing a little bit with Lua.
I came across the following code snippet that have an unexpected behavior:
a = 3;
b = 5;
c = a-- * b++; // some computation
print(a, b, c);
Lua runs the program without any error but does not print 2 6 15 as expected. Why ?
-- starts a single line comment, like # or // in other languages.
So it's equivalent to:
a = 3;
b = 5;
c = a
LUA doesn't increment and decrement with ++ and --. -- will instead start a comment.
There isn't and -- and ++ in lua.
so you have to use a = a + 1 or a = a -1 or something like that
If you want 2 6 15 as the output, try this code:
a = 3
b = 5
c = a * b
a = a - 1
b = b + 1
print(a, b, c)
This will give
3 5 3
because the 3rd line will be evaluated as c = a.
Why? Because in Lua, comments starts with --. Therefore, c = a-- * b++; // some computation is evaluated as two parts:
expression: c = a
comment: * b++; //// some computation
There are 2 problems in your Lua code:
a = 3;
b = 5;
c = a-- * b++; // some computation
print(a, b, c);
One, Lua does not currently support incrementation. A way to do this is:
c = a - 1 * b + 1
print(a, b, c)
Two, -- in Lua is a comment, so using a-- just translates to a, and the comment is * b++; // some computation.
Three, // does not work in Lua, use -- for comments.
Also it's optional to use ; at the end of every line.
You can do the following:
local default = 0
local max = 100
while default < max do
default = default + 1
print(default)
end
EDIT: Using SharpLua in C# incrementing/decrementing in lua can be done in shorthand like so:
a+=1 --increment by some value
a-=1 --decrement by some value
In addition, multiplication/division can be done like so:
a*=2 --multiply by some value
a/=2 --divide by some value
The same method can be used if adding, subtracting, multiplying or dividing one variable by another, like so:
a+=b
a-=b
a/=b
a*=b
This is much simpler and tidier and I think a lot less complicated, but not everybody will share my view.
Hope this helps!