Absolute value function Ocaml

為{幸葍}努か 提交于 2020-08-10 19:23:46

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/62484643/absolute-value-function-ocaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!