Can OCaml check tail recursion - ocaml

Is there a way to get ocaml to tell me if a function implements a recursion using tail recursion? I don't mean reading the code. I mean getting ocaml to tell me, say like this:
let x = tail_recursion f;;

You can compile your source code with '-annot'. It will produce an annotation file, that some editors can use.
In caml-mode (emacs) the command ist:
M-x caml-types-show-call

Related

For statement in OCaml without integers

For learning reasons, I would like to use for loops in OCaml instead of recursion.
I've researched a bit and applied one example of (https://ocaml.org/learn/tutorials/camlp4_3.10/foreach_tutorial.html):
let a_list = ["hello"; "world"] in
for s in a_list do
print_endline s
done
This makes sense. However, when I look at it (on Visual Studio, for instance), it tells me that 's' is an int variable. Therefore, I cannot do stuff like (where 'a', 'b',... are variables of any type):
let a_list = [a;b;c...] in
for s in a_list do
match s with
...
done
Is there not a way to use the for statement like in Python? I mean, to use 's in list' and that s's type will be its type and not 'int'?
There is something I do not see there.
Thanks in advance!
There is no foreach in OCaml. Recursion is the idiomatic style, or if you really want to avoid recursion, you can use while loop. For loop only iterate on integers.
You might be confused on the nature of the tutorial that you are linking: this is a tutorial for implementing a (old style) syntax extension for OCaml, and not a tutorial on using for loop in OCaml.

Ocaml interpreter first approach

I have to create an interpreter in ocaml, I have to implement ths String and some operation on it. Something like strcpy in c . I never use ocaml, I googled a lot but I can't find something that helps me. So I have to write the synthax?
Something like :
type ide = string
type exp = ........ EXPRESSIONS
Then some module, struct in c ? And some SEMANTIC EVALUATION FUNCTIONS ? Sorry is my first time in ocaml, but also my first interpreter, I want to learn it so I need just some input to know where to study it . Please don't rate me wrong I wanna learn!

How to make input part of my code?

So I have this maths project where I have to write a program which calculates definite integral of a given function within the given boundaries. I've done this using C++ and CodeBlocks, but now I would like to try and make it possible to input function using cmd when I run my code in CodeBlocks, just like I input boundaries, so I don't have to edit this line of code every time I want to run it for different function. I realised that this would require actually using then this input ( e.g. "sqrt(pow(x,2)-1)" ) as part of the code when entered, and I really don't know how to do this or if it is possible at all, so any help is welcome.
This the part of the code which handles function:
double Formula(double x)
{
double a;
a = sqrt(x);
return a;
}
If you want to evaluate expression like "sqrt(pow(x,2)-1)", you have to:
parse the string and generate an AST (Abstract syntax tree) which describes the operations to execute
use an evaluation function on the AST
For example, if you have "sqrt(pow(x,2)-1)" in input, the AST could be represented like this:
function - sqrt
function - substract
function - pow
variable - x
integer - 2
integer - -1
You have to define the structures which will be used to represent your AST.
Then, to parse the query string you have 2 choices:
parse it yourself, count the parentheses etc...
use a tool to generate the parser: yacc + lex or under linux bison + flex. These tools require time to be used to them.
If you have just a little project to do, you may have to try to parse the input yourself to generate the AST.
If the project is a compilation project, you should use bison + flex, they are exactly made for that (but require time to be used to ! ).
Alternatively, integrate with a scripting language, make it do the function parsing and evaluation. It will be considerably slower though.
JavaScript interpreters are all over the place. Python is fairly popular, too. Some people like Lua.

"Eval" a string in OCaml

I'm trying to "eval" a string representing an OCaml expression in OCaml. I'm looking to do something equivalent to Python's eval.
So far I've not been able to find much. The Parsing module looks like it could be helpful, but I was not able to find a way to just eval a string.
Here is how to do it, but I didn't tell you. (Also the Parsing module is about Parsing, not executing code)
#require "compiler-libs" (* Assuming you're using utop, if compiling then this is the package you need *)
let eval code =
let as_buf = Lexing.from_string code in
let parsed = !Toploop.parse_toplevel_phrase as_buf in
ignore (Toploop.execute_phrase true Format.std_formatter parsed)
example:
eval "let () = print_endline \"hello\";;"
Notice the trailing ;; in the code sample.
To use ocamlbuild, you will need to use both compiler-libs and compiler-libs.toplevel.
OCaml is a compiled (not interpreted) language. So there's no simple way to do this. Certainly there are no language features that support it (as there are in almost every interpreted language). About the best you could do would be to link your program against the OCaml toplevel (which is an OCaml interpreter).

How to parse mathematical formulae from strings in c++

I want to write a program that takes an string like x^2+1 and understand it.
I want to ask the user to enter her/his function and I want to be able to process and understand it. Any Ideas?
char s[100];
s <- "x*I+2"
x=5;
I=2;
res=calc(s);
I think it could be done by something like string analyses but I think Its so hard for me.
I have another Idea and that is using tcc in main program and doing a realtime compile and run and delete a seprated program (or maybe function) that has the string s in it.
and I will create a temp file every time and ask tcc to compile it and run it by exec or similar syntax.
/*tmp.cpp:*/
#include <math.h>
void main(/*input args*/){
return x*I+2;
}
the tmp.cpp will created dynamically.
thanks in advance.
I am not sure what do you expect. It's too complex to give the code as answer, but the general idea is not very complex. It's not out of reach to code, even for a normal hobbyist programmer.
You need to define grammar, tokenize string, recognize operators, constants and variables.
Probably put expression into a tree. Make up a method for substituting the variables... and you can evaluate!
You need to have some kind of a parser. The easiest way to have math operations parsable is to have them written in RPN. You can, however, write your own parser using parser libraries, like Spirit from boost or Yacc
I use with success , function parser
from www it looks like it supports also std::complex, but I never used it
As luck would have it, I recently wrote one!
Look for {,include/}lib/MathExpression/Term. It handles complex numbers but you can easily adapt it for plain old floats.
The licence is GPL 2.
The theory in brief, when you have an expression like
X*(X+2)
Your highest level parser can parse expressions of the form A + B + C... In this case A is the whole expression.
You recurse to parse an operator of higher precedence, A * B * C... In this case A is X and B is (X+2)
Keep recursing until you're parsing either basic tokens such as X or hit an opening parenthesis, in which case push some kind of stack to track where your are and recurse into the parentheses with the top-level low-precedence parser.
I recommend you use RAII and throw exceptions when there are parse errors.
use a Recursive descent parser
Sample: it's in german, but a small and powerfull solution
look here
here is exactly what You are searching for. Change the function read_varname to detect a variable like 'x' or 'I'.