Ocaml 语法很奇怪 [英] Ocaml syntax quite weird

查看:87
本文介绍了Ocaml 语法很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ocaml 中有这个程序,它从键盘读取一行并返回一个 int :

I have this program in Ocaml that reads a line from keyboard and returns an int :

let get_int ()  =
print_string "Insert a number\n" ;
let input =  read_line() in
let return__ = int_of_string( input )
;;

print_string "I'll print what you write : ";
print_int ( get_int() );
print_string "\n"

问题是第 5 行的语法错误,;;";编译器说.

The problem is a syntax error on line 5, ";;" said the compiler.

我知道已经存在执行此操作的函数,但我这样做是为了学习.

I know that already exist functions that do this but I'm doing this to learn.

我阅读了官方的 Ocaml 文档,但我仍然不明白语法.有人可以向我解释一下吗?

I read the official Ocaml documentation but I still don't get the syntax. Someone that could explain me something about it?

推荐答案

您的 get_int 绑定必须以表达式结尾.你可以去掉最后一个 let 绑定并直接返回 int :

Your get_int-binding has to end in an expression. You can get rid of the last let-binding there and return the int directly:

let get_int () =
  print_string "Insert a number\n";
  let input = read_line () in
  int_of_string input;;

这篇关于Ocaml 语法很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆