Nim is to assign variable in or beside other expression once - nim-lang

How Nim's way assign variable in or beside other expression at once, the one that'd always led to:
Error: expression 's = "foo"' has no type (or is ambiguous)
when trying like c/c++ code if (s = "foo").len > 5 { cout<< "Yes" ;} or some else?
the point was that, how to have variable assignment in some expressions at once,

if (let s = "foo"; s).len > 5:
echo "Yes"
or
var s: string
if (s = "foo"; s).len > 5:
echo "Yes"

Related

The runtime constant in forward declaration

How do we do runtime constant in forward definition/declaration by any way around (tried hard not work on using let)
let n :int
proc m : int =
let i=1
var u=n+i
n=m()
error for this or for other varied ones:
Error: 'let' symbol requires an initialization
Error: redefinition of 'n'; previous
... etc
Please solve it out, thanks before
It's hard to say what you are trying to do with this code, a let variable is treated as a constant, so it can't be modified, but presumably your intent is to use the variable as a sort of counter to increase itself and presumably reassign n multiple times by calling m()? If you remove the n from the proc you can write the code like:
proc m : int =
let i=1
var u=0+i
let n :int = m()
So if you actually want n to be mutable, there's no problem using a var:
var n :int
proc m : int =
let i=1
var u=n+i
u
n=m()
echo n
Note that I had to add u as a last line to the m proc, because otherwise you were returning nothing and the assignment to n would always be zero, which is the default value for the implicit result variable inside a proc that returns something. You can verify this by repeating the n=m() assignment before the last echo.

Is there an easy way to transform a string to a mathematical expression with vbscript?

For example suppose I have
var1 = "15+28*3+(15.2+2)*2"
I want to return something like
var2 = CDbl(var1) or var2 = 133.4
Which I know it is wrong. But I am wondering if there is an easy solution?
Use Eval to evaluate an expression:
>> s = "15+28*3+(15.2+2)*2"
>> WScript.Echo Eval(s)
>>
133,4

OCaml Test if a string is almost empty or contains keywords

I've got a problem in OCaml, I'm currently learning it but I'm quite a newbie still. I would like to make a function which is returning true if the string is empty or contains only whitespace and in the same time remove any occurence of begin and end.
I tried already this:
let isEmptyString s =
let rec empty i =
if i < 0 then true
else
let c = String.get s i in
if c = ' ' || c = '\009' then empty (pred i)
else false
in
s = Str.global_replace( Str.regexp "begin") "" s;
s = Str.global_replace( Str.regexp "end") "" s;
empty (pred (String.length s))
But obviously, this function is not working as I would like it because I obtain still begin in Formula.importNrAgentsFormula after calling it... Here is my way to call it :
while true do
let input = read_line () in
if not (isEmptyString input) then
let (nr, f) = Formula.importNrAgentsFormula input in
incr counter;
flush stdout;
match choice with
| "graph" -> printRes (Graph.isSat ~verbose:verb nr f)
| _ -> printUsage ()
else ()
done
If someone with more experiences in OCaml could spot and explain to me the error, I would be glad :)
Thanks in advance,
Best Regards.
I suggest you let your function isEmptyString (isBlankString rather?) do what it is supposed to do (just check if it contains only whitespaces or nothing), it should not modify the original string. You can do this in your loop:
while true do
let input = read_line () in
let input = Str.global_replace( Str.regexp "begin") "" input in
let input = Str.global_replace( Str.regexp "end") "" input in
if not (isEmptyString input) then
...
Edit: Sorry for the late edit, here is some additional information on your error:
If you run your function in OCaml, you will see this warning:
Warning 10: this expression should have type unit.
on the line of s = Str.global_replace( Str.regexp "begin") "" s;. That is because the = operator in OCaml is not the assignment operator in this case but the equality operator, so on this line you simply compare your two values and return a boolean. Since OCaml expects e1 in e1;e2 to return unit, you get this warning.
In OCaml, values of variables are immutable, so you can:
Use another variable as #Jason suggests: let t = Str.global_replace( Str.regexp "begin") "" s
"shadow" the old value as I suggest above: let s = Str.global_replace( Str.regexp "begin") "" s
Use a reference (a pointer to a location in memory): let s = ref "before" in s := "after", you can then access the value pointed by the reference with the !operator: !s. However, if you are learning functional programming, I suggest you try not to use any imperative features of OCaml at the beginning to discover this new paradigm and its possibilities.
As I am at work I don't have utop with me, but just from first glance, in your first one, the documentation says:
val global_replace : regexp -> string -> string -> string
That means you don't need a ";" as that is for when functions return unit and is syntactic sugar for something like
let () = print_endline("foobar")
Additionally, you need to use a let statement as you cannot just reassign the value of s. I don't recommend shadowing the variable as that's generally bad practice in functional programming. Use something like:
let t = (Str.global_replace( Str.regexp "begin") "" s)
Also, your function does two different things. The helper recursive function you wrote returns true and false which is good (I'm assuming it works). What you ultimately use it for however is what you're returning. Therefore, for the first function are you aren't really returning the string if "begin" and "end"s have been replaced. Therefore you should have the end output of your function actually a tuple of type (bool,string). Then you can match on it when you call it (e.g.
let b,s = isEmptyString "foobar" in
if not b then:
rest of your code
I believe you have the right idea for your function though.
Also in your second function is there a way for you to not use any while loops and counters? (Also hopefully your counter is implemented with references otherwise you won't have anything global). I would suggest retrying the place where you call your first function as loops and counters are core to imperative programming and not functional (which is what makes OCaml so
fun
:). If not, it's fine sometimes there are just things you can't really do in OCaml without using its imperative features. Let me know if those suggestions don't work.

Is it possible to fix this gsub pattern?

I'm messing around with Lua trying to create my own "scripting language".
It's actually just a string that is translated to Lua code, then executed through the use of loadstring. I'm having a problem with my string patterns. When you branch (for example, defining a variable inside of a variable declaration) it errors. For example, the following code would error:
local code = [[
define x as private: function()
define y as private: 5;
end;
]]
--defining y inside of another variable declaration, causes error
This is happening because the pattern to declare a variable first looks for the keyword 'define', and captures everything until a semicolon is found. Therefore, x would be defined as:
function()
define y as private: 5 --found a semicolon, set x to capture
I guess my question is, is it possible to ignore semicolons until the correct one is reached? Here is my code so far:
local lang = {
["define(.-)as(.-):(.-);"] = function(m1, m2, m3)
return (
m2 == "private" and " local " .. m1 .. " = " .. m3 .. " " or
m2 == "global" and " " .. m1 .. " = " .. m3 .. " " or
"ERROR IN DEFINING " .. m1
)
end,
}
function translate(code)
for pattern, replace in pairs(lang) do
code = code:gsub(pattern, replace)
end
return code
end
local code = [[
define y as private: function()
define x as private: 10;
end;
]]
loadstring(translate(code:gsub("%s*", "")))()
--remove the spaces from code, translate it to Lua code through the 'translate' function, then execute it with loadstring
The easiest solution is to to change your last capture group from
(.-) -- 0 or more lazy repetitions
to
(.*) -- 0 or more repetitions
i.e.
pattern = 'define(.-)as(.-):(.*);'
The - modifier according to PiL matches the shortest sequence.
However, as noted in my comment, I wouldn't advise writing a parser for your language using pattern matching. It will either require really complicated patterns (to prevent edge-cases) and probably be unclear to others.

Programming language idiom for "if object.value == some_value then object.value = some_other_value"

I've been wondering myself multiple times if, and if not, why not, there is an idiom/shortcut for the following pseudocode:
if object.value == some_value then object.value = some_other_value
For example, in JavaScript I sometimes write:
if (document.getElementById("toggledDiv").style.display == "block") {
document.getElementById("toggledDiv").style.display = "none";
}
This seems to be rather tedious. Is there a name for this idiom, and is there a more concise syntax for this in common programming languages?
Thank you!
Edit: To be more precise, I don't care about the braces, but about that you have to reference the attribute at least to times. I'd like to have something like that (pseudocode):
test ( object.value ):
if (it > 0) it = 0;
else it -= 1;
e. g.:
test (document.getElementById("toggledDiv").style.display):
if (it == "block") it = "none";
where it is a keyword that references the tested property. I'm just wondering no programming language seems to have implemented that.
Upate:
Okay, in the meantime I have found something which is a little bit short, but only works in JavaScript:
(function(s){
if(s.display=='block')
s.display="none";
else
s.display='block';
})(document.getElementById("toggledDiv").style)
Well, in Haskell, and other FP languages, conditionals, like ternary operators, are first-class expressions, so you can float the assignment out,
a = if x == y then x else z
Making the code a lot cleaner.
I don't know of any languages that support it out of the box, but there are a number that support defining new operators. In theory, you could write something like the following (in psuedo-code)
operator <T> T toggle(T value, T[] values) {
for(int i=0; i<values.size(); i++) {
if(value == values[i]) {
if(values.size() > (i+1)) {
return values[i+1]
} else {
return values[0]
}
}
}
error "value $value not found in value list $values"
}
Assuming my psuedo-code is correct, this would allow you to do the following:
v = true;
v = v toggle [true, false] ; // v == false
v = v toggle [true, false] ; // v == true (loops to beginning of list
v = v toggle [false, true, true] ; // v == true, since true is both the 2nd and 3rd elements of the list
You could also two versions:
One that, if the values isn't in the list, returns the original value
One that, if the values isn't in the list, throws an error (what my version did)
The former would be less of a toggle and more of... what you asked for, I guess. I was basing the code off the toggle use case from the previous note about css/block/none, where toggle is the more common behavior.
Assuming the language supports it, you could write a toggle= operator too:
v toggle= ['none', 'block']
In JavaScript you can do:
var d = document.getElementById("toggledDiv");
if (d.style.display == "block") d.style.display = "none";

Resources