OCaml syntax error in function - function

I have to create a function which will display each element from a set of strings. I did the following:
module S = Set.Make(String);;
module P = Pervasives;;
let write x = (
P.print_string("{"); let first = true;
S.iter (fun str -> (if first then () else P.print_string(","); P.print_string(str))) x;
P.print_string("}");
P.print_newline
);;
^
At the end of the program (where I placed that sign) it appears I have an error: Syntax error: operator expected.
Please help me solve this.

I believe your syntactic problem is with let. Except in top-level code (outermost level of a module), let must be followed by in.
There are many other problems with this code, but maybe this will let you find the next problem :-)
A few notes:
Variables in OCaml are immutable. So your variable named first will always be true. You can't change it. This (seemingly minor) point is one of the keys to functional programming.
You don't need to reference the Pervasives module by name. That's why it's called "pervasive". You can just say print_string by itself.
Your last call to print_newline isn't a call. This expression just evaluates to the function itself. (You need to give it an argument if you want to call the function.)

Try replacing the semicolon after the let first = true with the keyword in.

Related

Lua - Should I use ":" when defining functions in tables if not using the "self" keyword?

This is more of a design philosophy question as I already know you shouldn't call a function with : (object-oriented syntactic sugar) if the function has been defined without the self keyword by using .. But the problem is that programmers using a library I have created tend to not read the documentation and run into the question of "how should I call your function?", so I end up always defining functions using the method below:
local tbl = {};
function tbl:Add(a, b)
return a + b;
end
I have installed Luacheck (in VS Code) and it often complains when I use this syntax and not use the self referential keyword. It says: [luacheck] unused argument "self". Is there any problem with this in terms of performance (or is there a way of disabling Luacheck in VS Code)?
I prefer writing functions in this style as opposed to the style below:
function tbl.Add(_, a, b)
return a + b;
end
It seems a pain to have to add a dummy variable at the start of the parameter list.
EDIT: Another problem is what if you had many tables that implement a function with the same name and want to iterate over them but some implementations do not use the self argument and others do? It would be very tedious and bad design to check what type of table it is to call the function correctly.
What is the preferred style? A bit confused in general about this warning. What are your thoughts? Thanks.
if you're not using the self argument you can just do
function tbl.Add(a, b)
return a + b;
end
no need to use a dummy variable.
You just need to be sure then that you also call it with a . and not a :
so
local someValue = tbl.Add(1, 3)
for example and not
local someValue = tbl:Add(1, 3)

Creating an ODE function Fortran [duplicate]

Below is a sample code that addresses the problem I am having. The error message I am getting is
Function result 'sample' at (1) has no IMPLICIT type.
I label where line (1) is below.
I tried to follow this other question, however I wasn't able to figure it out. This function is within a module in my program and I made sure that the module has contains and I end the module after this function.
I also use implicit none in this function so I'm not sure why I get this message. How can I fix this error message?
Adding Real or Complex in front of function works, but I don't really get why. Shouldn't I only be able to use complex since the arrays are complex inside the function? Which is more suitable for my actual function? Both yield no compilation errors.
real function Sample(func) !this is line (1)
!complex function Sample(func)
implicit none
integer :: n,m
real :: x,y
complex, dimension(-9:9,-9:9), intent(in) :: func
complex, dimension(-9:9,-9:9) :: LocalF
LocalF = func
do n=-9,9
do m=-9,9
x = real(n)*0.2
y = real(m)*0.2
LocalF(n,m)= cmplx(z1(x,y),z2(x,y)) !assume z1,z2 are well defined
end do
end do
end function Sample
In Fortran every function has a result. If you like you can think of the result as a value returned by the function. Like every other value in a Fortran program a function result has a type, and a kind and a rank too.
By default the function result has the same name as the function itself, and its declaration is prepended to the function declaration. For example, here
integer function add(m,n)
integer, intent(in) :: a,b
add = a+b
end function
the function is called add and you can see (a) that the result is of type integer (and of default kind and scalar) and (b) that the result is formed by adding the two arguments together.
For functions returning arrays this syntax is not available, so you couldn't write something like
integer(1:4) add_vec(m1,m2)
In such cases you have to explicitly define the name (and later type and kind) of the result variable. Sticking with the simple example, something like
function add(m,n) result(addvec)
integer, intent(in) :: a(4),b(4)
integer, dimension(4) :: addvec
....
end function
Notice that you don't define the intent of the result.
In OP's case sample is, I think, intended to return a rank-2 array of complex values. I think OP needs to replace
function Sample(func) !this is line (1)
with
function Sample(func) result(LocalF)
and see how that goes. Here, if it is not evident already, you learn that the result name doesn't have to be the same as the name of the function.
Furthermore ... Adding Real or Complex in front of function works, but I don't really get why.
It might work in the sense of compiling, but executing it will lead to tears. By telling the compiler that the function result is either a real or complex value you satisfy the syntactical requirements for a function definition. But without assigning a (real or complex as declared) value to the result variable (called Sample in OP's code) the function will, at best, return junk.
To be as clear as I can ... in OP's original code there were two serious mistakes:
The function (result) was not given an explicit type, which lead to the compiler message shown.
The function did not include setting the value of the result variable, i.e. the variable with the same name as the function (in the absence of the result clause).
Procedures in Fortran come in two types: functions and subroutines. This question is about functions, so I'll consider just those.
What was missing in the first revision, giving the error about the implicit type of the function result1, was the result type.
Adding real function ... or complex function ..., etc., resolves that problem by explicitly giving the type of the function result. The linked documentation gives other ways of doing that.
The function's result is used when the function is referenced. When we have a reference like
func0 = Sample(func)
in the main program, the function Sample is invoked and the function result is defined in its execution. At the end of the function's execution its result is placed in the expression of the reference.
So, if you declare
real function Sample(func)
or
complex function Sample(func)
what you are saying is that the function result is either a real or complex entity. And when the function is evaluated, whatever value Sample had at the end is used in the expression (here assignment).
As a consequence of the function result being returned through Sample (in this case) we need to define its value. The important thing to note for the question, then, is that LocalF is a variable local to the function. If you mean it to be the result of the function you need to use the function result.
You have a number of options:
function Sample(func)
<type>, <attributes> :: sample ! Instead of LocalF
... :: func
end function
or
function Sample(func) result(LocalF)
<type>, <attributes> :: LocalF
... :: func
end function
You can even have
<type> function Sample(func)
<attribute statements for Sample>
... func
end function
but I really suggest you avoid that last one.
1 Note the error here is about type for the function result; in the linked question simply about the function when referenced.

In Lua, what is the difference between functions that use ":" and functions that do not? [duplicate]

This question already has answers here:
Difference between . and : in Lua
(3 answers)
Closed 7 years ago.
Let's say we have two function declarations:
function MyData:New
end
and
function New(MyData)
end
What is the difference between them? Does using : have any special purpose when it comes to inheritance and OOP? Can I only call functions declared with : by using :?
I'm coming from using only C# -- so if there's any comparison to be made, what would it be?
Adapted from the manual, end of ยง3.4.10:
The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter self. Thus, the statement
function t:f (params) body end
is syntactic sugar for
t.f = function (self, params) body end
You should search SO as there are many questions about this but you have a set of questions so I can't say this is a duplicate.
Q. What is the difference between them?
A. The one with colon causes a method to be added to the MyData table, and the Lua interpreter to automatically insert a "self" before the first parameter when called, with this "self" pointing to the MyData instance that the "method" is being called upon. It is the same as writing MyData.New = function(self) end. The second signature has a parameter called MyData and is a global function. It is unrelated to the MyData table (or class).
Q. Does using ":" have any special purpose when it comes to inheritance and OOP?
A. No; it is merely syntactic sugar so that when you call MyData.New you can just write MyData:New() instead of the clunky looking MyData.New(MyData). Note that a "new" function is typically to create instances of a class, so you wouldn't have such a function be a method, rather just a function in the MyData table. For inheritence and OOP, you use metatables, and this does not interact with : in any special way.
Q. Can I only call functions declared with ":" by using ":"?
A. No, as mentioned, it just syntactic sugar, you can define one way and call a different way.
Q. I'm coming from using only C# -- so if there's any comparison to be made, what would it be?
A. For functions, the : is like the . in C#, whether used in a call or definition. The "." in Lua is more like "attribute", there is no equivalent in C# for functions.
MyData = {} -- a table
function MyData.func(self)
print('hello')
end
MyData:func()
MyData.func(MyData) -- same as previous
function MyData:func2() -- self is implicit
print('hello')
end
MyData:func2()
MyData.func2(MyData) -- same as previous
Note that MyData as defined above is not a class, because you cannot create "instances" of it (without doing extra work not shown there). Definitely read the Programming in Lua online book on the Lua.org website, lots of useful discussions of these notions.
Lua doesn't have functions declarations per se; It has function definition expressions. The syntax you have used is shorthand for a function definition expression and assignment.
The only difference in your examples is when the first statement is executed a new function is created and assigned to the field New in the table referenced by the variable MyData, whereas the second is an assignment to a non-field variable (local, if previously declared, otherwise global).
Keep in mind that these are only the first references to the created function values. Like any other value, you can assign references to functions to any variable and pass them as parameters.
If you add formal parameter usage to the bodies then there is another difference: The first has an implicit first parameter named self.
If you add function calling to the scenarios, the ":" syntax is used with an expression on the left. It should reference a table. The identifier to the right should be a field in that table and it should reference a function. The value of the left expression is passed as the first actual argument to the function with any additional arguments following it.
A function definition with a ":" is called a method. A function call with a ":" is called a method call. You can construct a function call to a function value that is a field in a table with the first argument being a reference to the table using any function call syntax you wish. The Lua method definition and method call syntax makes it easier, as if the function was an instance method. In this way, a Lua method is like a C# Extension Method.

Variable Availability in Erlang

I am wondering if it is possible to have a function get a variable if it is not passed explicitly.
The issue is mainly about cleaning up my code, as I have many functions that need to pass every variable that will ever be used to the next function.
In SML for example, one could easily accomplish this with something like:
fun myFun varx vary varz
let
fun otherFun () = varx
fun otherFun2 () = vary
in
otherFun() + otherFun()
end
Is there a way to allow other functions to see variables that are not explicitly passed to it? Or is this just not the way one would program in erlang?
Erlang variable scope works much in the same way:
E.g:
add_two(X) ->
F = fun(Y) ->
X + Y
end,
F(2).
Hope this helps.

Function definitions are not permitted in this context

Ok, I am new in Matlab and I am currently working on some econometric script. Before I move to real econometrics I have to create a function that selects the data that I'm interested in. Although I managed to get that script to work by writing at a very structural level, I would like this script to be as universal as possible and therefore would like to divide it into specific functions. However, when I converted all this to one function, I keep getting the error "Function definitions are not permitted in this context".
Thanks in advance for your help.
function [probingArray] = extractData (data, startValue, numberOfPeriods)
arrayHeight=size(data,1);
for i=1:arrayHeight
if Date(i)==startValue
datePosition=i;
end
end
n=1;
for i=(datePosition-numberOfPeriods):datePosition
probingArray(n,1)=n;
probingArray(n,2)=UK(i);
n=n+1;
end
clear n i;
make sure you respect matlab conventions
function [out1, out2, ...] = myfun(in1, in2, ...) declares the function myfun, and its inputs and outputs. The function declaration must be the first executable line of any MATLAB function.
from http://www.mathworks.com/help/techdoc/ref/function.html