I'm trying to create a list of tuples of key and value pairs from a map using Stringmap fold, but I'm getting a syntax error in response.
let testlist = Stringmap.fold (fun k v -> (k,v)) mymap []
(Assume mymap is already defined)
My terminal says
File "myfile.mll", line 47, characters 0-3:
Error: Syntax error
make: *** [myfile] Error 2
But I can't tell what my issue (and also I wish there was more information to help me).
The mll-files have special rules that doesn't allow you to use the OCaml syntax everywhere. There are special locations, like in a preambles, where you can add your OCaml definitions. Consult the documentation for extra details.
P.S. your definition is syntactically valid OCaml, no problem with that.
Related
I'm taking a look to this programming language "Ocaml" and I have some troubles because I read the official ocaml documentation but I don't uderstand how to use :
";" and ";;" and "in" specially inside the definition of functions.
This is my code :
let abs_val value : int -> int =
let abs_ret = ref 0 ;
if value >= 0
then abs_ret := value
else abs_ret := -value ;
let return : int = abs_ret
;;
print_int abs_val -12
Compiled with "ocamlc" it said :
File "first_program.ml", line 7, characters 2-4:
7 | ;;
^^
Error: Syntax error
And it sounds so weird for me because official ocaml's doc says that when function definition ends I must use ";;".
I noticed that after the definition of abs_val VisualStudio Code ,when I go on a newline, put automatically the cursor to 2 spaces on the right, not at the beginning of the line.
I'm new in ocaml so I don't know if this is common or not but for me sounds like if something is missing, and probably it is :)
P.S. : I know that an abs function already exists but I'm doing this to learn.
Update :
let abs_val value =
let abs_ret = ref 0 in
if value >= 0
then abs_ret := value
else abs_ret := -value in
let return : int = abs_ret;
;;
print_int abs_val -12
Am I closer right?
Sometimes it happens the syntax error is not here but above. Did you closed your previous function with ;; ? Also, what is this return ? Use the functional paradigm of the language. You use variables and references and force the type checking. I'm not sure how do you want to use this code but in a general way, try to let OCaml determine the type of your functions rather than telling the compiler what is the signature. Plus, your code shouldn't be using that much references. For instance the following :
let abs_val value =
if value < 0 then
-value
else
value
Will work perfectly and not mess up things with reference. If you wish to use references, I suggest you learn more about the functional paradigm of OCaml before going deeper into its imperative possibilities.
Your syntax error is a result of having let with no matching in.
This is a very common error when learning OCaml syntax. There are two separate uses of let in OCaml. At the top level of a module, you use let to define a symbol (a function or a value) that is one of the elements of the module. So in the following:
module M = struct
let f x = x * 2
end
The let defines a function named M.f.
Similarly your code uses let this way to define abs_val.
In other cases (not at the top level of a module), let is used only as part of the let ... in expression that looks like this:
let v = exp1 in exp2
This essentially defines a local variable v with the value exp1 that can be used in the body of exp2.
All your other uses of let (except the initial definition of abs_val) are of this second kind. However, none of them have in, so they are all syntactically incorrect.
You need to fix up these problems before you can make progress with this function. You can fix the first one, for example, by changing the first semicolon (;) to in.
As #SDAChess points out, you have a second problem with the return value of your function. There is no special return keyword in OCaml that's used to return the value of a function. A function in OCaml is just a set of nested function calls, and the value of the function is the value returned by the outermost call.
I use vscode, with extensions of "OCaml and Reason IDE"
Here is my result in utop:
utop # 1. = 1. ;;
Line 1, characters 0-2:
Error: This expression has type float but an expression was expected of type
int
And also for String:
utop # "Me" = "Me";;
Line 1, characters 0-4:
Error: This expression has type string but an expression was expected of type
int
Same for anything but int:
utop # 2 = 2 ;;
- : bool = true
">" "<" also have the same symptom. I don't know what actually happens. Can anyone help me out ? Thanks a lot!
You are probably using JaneStreet Base library. Maybe you imported it like that:
open Base;;
Base tries to limit exceptions to functions that have explicit _exn suffix, so it shadows the built-in polymorphic equality (=) which can raise an exception on some inputs (for example, if you compare structures containing functions).
You can get polymorphic equality back as follows:
let (=) = Poly.(=);;
Or you can use it with a local import: Poly.(x = y).
There are pros and cons to polymorphic comparison.
The consensus seems to be that using monomorphic comparison (for example, String.equal, etc) is a more robust choice, even though it is less convenient.
I'm new to Prolog and what I want to achieve is to perform an operation like append/3 over two lists that are saved in my program. I don't need to open swi-prolog and type append([a,b,c],[h,j,k],X). in order to obtain X=[a,b,c,h,j,k]. What I need is something like retrieve two lists from the program and perform the append over them.
I don't even know if what I want to obtain is possible in Prolog.
This is my situation: I got this "esempio.pl" file where I have the following rule and these two facts:
personal_union(F,C,Xs) :-
personal_list(F,Fs),personal_list(C,Cs),append(Fs,Cs,Xs).
personal_list(family,[alessandro,cinzia,fabio]).
personal_list(colors,[blu,giallo,lilla,verde,rosso]).
I'd like to question "esempio.pl" from SWI-prolog and ask it:
personal_union(family,colors,X).
And obtain the unified lists:
X=[alessandro,cinzia,fabio,blu,giallo,lilla,verde,rosso]`
Is my code a possible solution? I couldn't try it myself because it keeps giving me this error: Syntax error: illegal start of term stating that the error is at the start of the body of my rule.
Your code is valid as I tested it on Swish, and it produces the requested result.
The error you get is usually derived of mistaken syntax such as additional commas or brackets issues. Check the rest of you code. Also, you might find http://swish.swi-prolog.org/ comfortable for such debugging.
%% Function even_print(List),takes a list and returns a list of only even numbers. Function even_odd(X), takes an integer and tells if it is even or odd.
even_print(List) ->
[X||X<-List, even<-even_odd(X)].
I don't understand why I get this error:
3> seq_erlang:even_print([2,3,4]).
** exception error: no function clause matching
seq_erlang:'-even_print2/1-lc$^1/1-1-'(even) (seq_erlang.erl, line 154)
Just to comment, I have already implemented another function that prints even numbers just fine (so please don't comment with other implementations). I need help with this one only.
That should be even == even_odd(X) instead of using <-. A list comprehension has two types of "clauses": those that map over a list with <-, and those that filter out undesired combinations using a guard or boolean expression that doesn't contain <-.
(And a third one: extract bytes from a binary using <=; but that one is more rarely used.)
Why does the following code has a syntax error? I did not find out why.
let rec revStr stringa k e =
if k = e then ""
else (string_of_char stringa.[e])^
(revStr stringa (e-1) k);;
string a, k, e are the input and it should give in output the sub.string from integer e to integer k.
There is no syntax error in this code. When I try it I get "Unbound value: string_of_char" which is a type error. Type errors appear after syntax errors, because typing occurs after parsing.
My guess is that you have a syntax error before this function, such as a forgotten semi-colon or a forgotten "in". Those kinds of errors are often detected much later.
In addition to what cygin points out (parameters seem reversed), revStr calls itself with a smaller value for e. But your description sounds like e is supposed to be less than or equal k. So then wouldn't you want to pass a larger value for e? Otherwise I don't see why you would ever reach k.
As a side comment, I wonder why a substring function is named revStr.