How would I get the index of a char in a string? - lua

So, essentially i'm trying to create a function that solves for the value of x.
for example, x + 4 = 8
So I'm trying to make it so, it replaces x with "" and then it gets the symbol in front of it in this case, "+" and replaces it with ""
but In Order to do so, and not remove an imaginary symbol behind it, I need to make it check if the index is 1.
My Brain Hurts.
Here's what I have for the function, I deleted some of the code for getting the index, cause It didn't work.
mathMod.solveExpression = function(variable, expression)
if (string.find(expression, "=") == nil) then
-- void
else
-- continue with search but with variable
if (string.find(expression, variable) == nil) then
-- void
else
-- ooh time for some sneaky equations
local copy = expression
for i = 1, #expression do
local c = expression:sub(i,i)
if (expression == c) then
end
end
end
end
end
/ Link To My Code`https://pastebin.com/DnKPdw2q /

If your equations are all of the form var op a = b, try this code, which uses Lua pattern matching:
s=" x + 4 = 8 "
var,op,a,b = s:match("(%w+)%s*(%p)%s*(%d+)%s*=%s*(%d+)")
print(var,op,a,b)
The pattern captures the first word as the var, skips spaces, captures a punctuation char as the operation, skips spaces, captures operand a, skips the equal sign possibly surrounded by spaces, and finally captures operand b.

Related

Type inference F# - how to generate fresh variables?

i'm trying to develop the algorithm W in f# for type inference, but i would like to understand how to write the function for generating fresh variables properly.
Actually my function is
let counter = ref -1
let generate_fresh_variable () : string =
let list_variables = ['a' .. 'z'] |> List.map string
counter.Value <- !counter + 1
list_variables.Item(!counter)
but i'm not satisfy with this solution, someone can give me other better ideas?
If you really want to do this with an impure function, I would write it like this:
let mutable counter = -1
let generate_fresh_variable () =
counter <- counter + 1
counter + int 'a'
|> char
|> string
Notes:
Reference cells are obsolete. If you need impurity, use mutable variables instead. (Alternatively, if you really want to stick with a reference cell, the canonical way to update it is with :=, rather than assigning directly to the underlying Value.)
There's no need to maintain a list of potential variable names (and there's especially no need to rebuild the entire list each time you generate a fresh variable).
What happens if you need more than 26 variables?
If you wanted to use some more sophisticated F# tricks, you could create an inifinte sequence of names using a sequence expression (which makes it very easy to handle the looping and dealing with >26 names):
let names = seq {
for i in Seq.initInfinite id do
for c in 'a' .. 'z' do
if i = 0 then yield string c
else yield string c + string i }
A function to get the fresh name would then pick the next name from the sequence. You need to do this using the underlying enumerator. Another nice trick is to hide the state in a local variable and return a function using lambda:
let freshName =
let en = names.GetEnumerator()
fun () ->
ignore(en.MoveNext())
en.Current
Then just call freshName() as many times as you need.

Using a variable as arithmetic operator in Lua

I want to use a variable that references an arithmetic operator within an if statement expression as shown below:
str = { '>60', '>60', '>-60', '=0' }
del = 75
function decode_prog(var1, var2)
op = string.sub(var1, 1, 1)
vb = tonumber(string.sub(var1, 2, 3))
if var2 op vb then
print("condition met")
else
print('condition not meet')
end
end
for i = 1, #str do
decode_prog(str[i], del)
end
When the above code executes, it should either print "condition met" or "condition not met" based on the result of the operation, however I am instead receiving an error.
You cannot substitute a native Lua operator with a variable that references a function, the only way to go about what you are attempted to do is to create a set of functions within an associative array and set the index as a reference to the respective operation you want to conduct.
Looking at your list, you have a greater than (>) and equal to (=). We create a table for these operations that takes two parameters as follows.
local operators = {
[">"] = function(x, y) return x > y end,
["="] = function(x, y) return x == y end,
-- Add more operations as required.
}
You can then invoke the respective function from the decode_prog function by obtaining the operation character from the string, along with the numeric value itself - this is possible because you can obtain the function from the associative array where the index is the string of the operation we want to conduct.
local result = operators[op](var2, number)
This calls upon the operators array, uses the op to determine which index we need to go to for our appropriate operation, and returns the value.
Final Code:
str = { '>60', '>60', '>-60', '=0' }
del = 75
local operators = {
[">"] = function(x, y) return x > y end,
["="] = function(x, y) return x == y end,
}
function decode_prog(var1, var2)
local op = string.sub(var1, 1, 1) -- Fetch the arithmetic operator we intend to use.
local number = tonumber(string.sub(var1, 2)) -- Strip the operator from the number string and convert the result to a numeric value.
local result = operators[op](var2, number) -- Invoke the respective function from the operators table based on what character we see at position one.
if result then
print("condition met")
else
print('condition not meet')
end
end
for i = 1, #str do
decode_prog(str[i], del)
end
I can't make much sense of your code or what you want to achieve doing that but if could simply use load.
You build your expression as a string and run it. Of course you should take care of two character operators like >= which I did not and you should validate your input.
local str={'>60','>60','>-60','=0'}
local del=75
function decode_prog(var1, var2)
local operator = var1:sub(1,1):gsub("=", "==")
local expr = string.format("return %d %s %s", var2,operator, var1:sub(2))
print(string.format("condition %smet", load(expr)() and "" or "not "))
end
for i,v in ipairs(str) do
decode_prog(v, del)
end
A very simple way would be to add a condition for each supported operator:
function decode_prog(var1, var2)
op = string.sub(var1, 1, 1)
vb = tonumber(string.sub(var1, 2)) --remove the last argument and use tonumber()
if vb == nil then return end --if the string does not contain number
if (op == ">" and var2 > vb) or (op == "=" and var2 == vb) --[[add more conditions here]] then
print("condition met")
else
print("condition not met")
end
end
I changed the vb=string.sub(var1,2,3) line too.
This form vb = tonumber(string.sub(var1, 2)) will allow use of numbers that have any number of digits and added tonumber() which will allow us to catch not-a-number errors when comparison would probably fail.
Then I added a logic to determine what the operator is and if the condition is met.
Operator limitations:
This will only work with operators that are one character and operator such as >= will not be possible unless you use a different character for it. ≥ will not play nicely, since it is multiple characters.

Define a palindrome operator that duplicates the values on the stack in reverse order in postscript

Define a palindrome operator that duplicates the values on the stack in reverse order.
this is what i have so far and it not doing what i want it to do
/palindrome {
1 dict begin
count 1 gt
{
/first exch def
/second exch def
temp1 = first
temp2 = last
first = last
last = temp1
}
} def
Most of what you have written there doesn't make any sense in PostScript:
/palindrome
{
1 dict begin
count 1 gt
{
/first exch def
/second exch def
%% The following four lines are not valid PostScript
temp1 = first
temp2 = last
first = last
last = temp1
%% There is no '=' assignment operator in PostScript, in PS the = operator
%% pops one object from the stack and writes a text representation to stdout.
%% You have not defined any of the keys temp1, temp2 or last
%% in any dictionary. If executed I would expect this program to throw an
%% 'undefined' error in 'temp1'
}
%% Given the 'count 1 gt' at the opening brace, I would expect this executable
%% array to be followed by a conditional, such as 'if'. Since it isn't this just
%% leaves the executable array '{....}' on the stack
} def
So overall I would expect this PostScript function to push a boolean on to the operand stack, true or false depending on whether the stack has at least 2 objects on it at the time of execution, and then an executable array on to the operand stack and exit.
If I were doing this I would store the stack into an array, then unload the array back to the stack and then iterate through the array from end to beginning. Something like:
%!
/palindrome
{
count array astore
aload
dup length 1 sub -1 0 {
1 index exch get
exch
} for
pop
} def
(line 1)
2
3
(before palindrome\n) print
pstack
palindrome
(after palindrome\n) print
pstack
It's also possible (I have a working example here) to do this in a single pass without defining any extra storage objects (dictionaries or arrays) by using a for loop and manipulating the stack. That seems like a more elegant solution to me, and is left as an exercise for the reader :-)

Need advice on the Swift while statement

The following code got compilation error:
var a : Int = 0
var b : Int = 3
var sum : Int = 0
while (sum = a+b) < 2 {
}
The error message is:
Cannot invoke '<' with an argument list of type '((()),
IntegerLiteralConvertible)'
How to solve this problem? (Of course I can put sum assignment statement out side the while statement. But this is not convenient. Any other advice? Thanks
In many other languages, including C and Objective-C, sum = a+b would return the value of sum, so it could be compared.
In Swift, this doesn't work. This was done intentionally to avoid a common programmer error. From The Swift Programming Language:
Swift supports most standard C operators and improves several capabilities to eliminate common coding errors. The assignment operator (=) does not return a value, to prevent it from being mistakenly used when the equal to operator (==) is intended.
Since the assignment operator does not return a value, it can't be compared with another value.
It is not possible to overload the default assignment operator (=), but you could create a new operator or overload one of the compound operators to add this functionality. However, this would be unintuitive to future readers of your code, so you may want to simply move the assignment to a separate line.
In most languages, assignments propagate their value -- that is, when you call
sum = a + b
the new value of sum is available for another part of the expression:
doubleSum = (sum = a + b) * 2
Swift doesn't work that way -- the value of sum isn't available after the assignment, so it can't be compared in your while statement. From Apple's documentation:
This feature prevents the assignment operator (=) from being used by
accident when the equal to operator (==) is actually intended. By
making if x = y invalid, Swift helps you to avoid these kinds of
errors in your code.
The other answers explain why your code won't compile. Here is how you can clean it up without calculating sum in the while loop (I'm assuming you want to be able to reassign what sum's getter is, elsewhere.):
var a = 0, b = 3
var getSum = { a + b }
var sum: Int { return getSum() }
while sum < 2 {
...and if you're okay with invoking sum with parentheses:
var a = 0, b = 3
var sum = { a + b }
while sum() < 2 {
You can rewrite it as a for loop, although you'll have to repeat the assignment and addition:
for sum = a+b; sum < 2; sum = a+b {
}

F#: Why do two ref or boxed values holding equivalent value types equate to the same cell or obj?

let i = ref 123
let j = ref 123
i = j // true
Similarly:
let i = box 123
let j = box 123
i = j // true
Presumably, i and j are not actually pointing to the same exact place in memory...??
I get around this (odd?) behavior in the second case by doing:
obj.ReferenceEquals (i, j) // false
What is the proper equality test for the first case?
EDIT:
I see that calling obj.ReferenceEquals works in the first case, as well.
Can someone out there explain to me why I have to call this function, though? Why can't I just use the = operator?
The (=) operator calls GenericEqualityObj. It first checks the types of the args (for arrays, assignability to IStructuralEquatable, and a few other special cases) and the final case calls obj.Equals. Equals is overridden by ValueType (which int derives) to do bit comparison. That explains why (box 123) = (box 123) is true.
Ref cells are represented using records, by default records are structurally comparable\equatable

Resources