The runtime constant in forward declaration - nim-lang

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.

Related

Can I use where in Haskell to find function parameter given the function output?

This is my program:
modify :: Integer -> Integer
modify a = a + 100
x = x where modify(x) = 101
In ghci, this compiles successfully but when I try to print x the terminal gets stuck. Is it not possible to find input from function output in Haskell?
x = x where modify(x) = 101
is valid syntax but is equivalent to
x = x where f y = 101
where x = x is a recursive definition, which will get stuck in an infinite loop (or generate a <<loop>> exception), and f y = 101 is a definition of a local function, completely unrelated to the modify function defined elsewhere.
If you turn on warnings you should get a message saying "warning: the local definition of modify shadows the outer binding", pointing at the issue.
Further, there is no way to invert a function like you'd like to do. First, the function might not be injective. Second, even if it were such, there is no easy way to invert an arbitrary function. We could try all the possible inputs but that would be extremely inefficient.

initialize an argument of a proc, but not in it's definition line

Suppose that we have an object which has some properties of type proc:
type
x = object
y: proc(a,b:int)
proc myproc(a,b:int) =
echo a
var tmp = new x
tmp.y = myproc # I want to insert initial value in this line for example a = 1
tmp.y(5)
How can I insert initial values in the specified line, and not anywhere else?
Thank you in advance
As far as I know it's not possible to do what you want without other modifications, because you have specified y to be a proc receiving two parameters. So whatever you assign to it, the compiler is always going to expect you to put two parameters at the call site.
One alternate approach would be to use default values in the proc definition:
type
x = object
y: proc(a: int = 1, b: int)
proc myproc(a,b: int) =
echo(a, " something ", b)
var tmp = new x
tmp.y = myproc
tmp.y(b = 5)
The problems with this solution are of course that you can't change the value of a at runtime, and you are forced to manually specify the name of the parameter, otherwise the compiler is going to presume you are meaning the first and forgot to specify b. Such is the life of a non dynamic language.
Another approach is to define the proc as having a single input parameter, and then using an anonymous proc or lambda to curry whatever values you want:
type
x = object
y: proc(a: int)
proc myproc(a,b: int) =
echo(a, " something ", b)
var tmp = new x
tmp.y = proc (x: int) = myproc(1, x)
tmp.y(5)
If you were to use the sugar module, as suggested in the docs, the assignment line could look like:
tmp.y = (x: int) => myproc(1, x)

python's exec() works oddly inside functions

I (think I) need exec() to procedurally define variables for symbolic computation. The code searches for variables in an expression like 'x/y + z' and generates three variables,
x = sp.sympy('x'), y = sp.sympy('y'), z = sp.sympy('z')(for example).
When I run the code
for char in expr:
if char.isalpha():
exec('%s = sp.symbols("%s")' % (char, char))
print(type(char))
It works just as it should. print(type(char)) just checks if what I wanted to happen happens (it prints <class 'sympy.core.symbol.Symbol'> if it worked).
However, as I need this for a general expr, I need this inside a function. When I do this print(type(char)) outputs <class 'str'>, which means it didn't work.
Also, if I type print(type(char)) inside the exec I get a correct output.
Note: I know exec() is dangerous. I'm just going to use this to have an easier time writing lab reports.
I couldn't think of a way to get it into one line, but I believe this will work:
for char in expr:
if char.isalpha():
exec('%s = sp.symbols("%s")' % (char, char))
exec('temp_type = type(%s)' % char)
print(temp_type)
Alternatively using a dict approach may make it easier in the long run. It avoids the exec.
symbol_dict = dict()
for char in expr:
if char.isalpha():
symbol_dict[char] = sp.symbols(char)
print(symbol_dict[char])

Ocaml - empty reference

How to create a empty reference? I mean something lika as
let x = ref Null
or
let x = ref None
or
let x = ref ()
There is no such thing in ML. A value of type t ref always holds a value of type t. This way, the curse of null pointer exceptions is avoided.
If you really need a nullable reference then you have to create a reference of type t option ref:
let r : int option ref = ref None
...
r := Some 5
The type system then ensures that you don't forget to handle the None case, e.g. with a match:
match !r with
| Some n -> ...
| None -> (* What now? *)
You should very rarely need such a thing, though. If you find yourself wanting an option ref then you should first try to restructure the problem.
It's hard to tell what you're asking. There's nothing predefined named Null in OCaml. There is something named None, and indeed you can say let x = ref None. You can also say let x = ref (). However it's hard to imagine a case where you'd want to do this. Since () is the only value of its type, you could never set x to any other value. So there would be no reason to use a reference.
Here is a session showing how to use ref None.
# let x = ref None;;
val x : '_a option ref = {contents = None}
# x := Some 5;;
- : unit = ()
# x;;
- : int option ref = {contents = Some 5}
# x := None;;
- : unit = ()
# x;;
- : int option ref = {contents = None}
Generally speaking an option type like int option allows you to have either an int value (like Some 5) or no value (None). Using a reference like int option ref gives you a mutable value that you can change between these two.
It looks to me like you're trying to reproduce patterns that you use in other languages. If possible you should try to learn OCaml in its own terms.

Basic OCaml function returns type error

I've been trying to run this function :
let insert_char s c =
let z = String.create(String.length(s)*2 -1) in
for i = 0 to String.length(s) - 1 do
z.[2*i] <- s.[i];
z.[2*i+1] <- c;
done;
z;;
print_string(insert_char("hello", 'x'));;
However the interpreter returns a type error at the last line "type is string * char" and it expected it to be string. I thought my function insert_char created a string. I don't really understand, thanks.
You define your function as a curried function, but you're calling it with a pair of values. You should call it like this:
insert_char "hello" 'x'
OCaml doesn't require parentheses for a function call. When two values are placed next to each other with nothing between, this is a function call.
The syntax of function application in OCaml is f arg1 arg2, not f(arg1, arg2). So it would be print_string (insert_char "hello" 'x').
(Once you fix that you'll discover that your code has other problems, but that is unrelated to your question.)

Resources