这里的语法错误到底是什么? [英] What exactly is the syntax error here?

查看:112
本文介绍了这里的语法错误到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,该函数将返回列表中的第二个最小数字.我不断收到语法错误,但我无法真正查明问题所在.我可以在这方面寻求帮助吗?

I'm trying to write a function that will return the second smallest number in a list. I keep getting a syntax error but I can't really pinpoint what the issue is. Can I please get help on this?

已删除的代码

推荐答案

您忘记使用in关闭 local let绑定.正确的(和缩进的)代码应为:

You forget to close local let bindings using in. The correct (and indented) code should be:

let second_smallest_helper1 lst=
  let second_smallest_helper2 currentMinimum currentNumber =
    if currentMinimum < currentNumber then currentMinimum else currentNumber
  in List.fold_left second_smallest_helper2 (List.hd lst) lst
;;

let delete (x, mylist) = List.filter (fun y -> y != x) mylist;;

let second_smallest myList = 
  let x = second_smallest_helper1 myList in
  let newList = delete (x,myList) in
  second_smallest_helper1 newList
;;

顶级let绑定的格式为

let <pattern> = <expression>;; (* ;; is optional, but beginners should have it *)

但本地let绑定的格式为

let <pattern> = <expression> in <expression>

您绝对需要为编辑器使用合适的OCaml缩进工具,以免发生此类错误.

You absolutely need to use a proper OCaml indentation tool for your editor to avoid this kind of errors.

还有一件事.我不确定您是否可以使用!=.这是物理指针比较.可能您想使用<>(结构比较).

One more thing. I am not sure your use of != is ok. This is physical pointer comparison. Probably you want to use <>, the structural comparison.

由于个人原因",OP试图编辑和删除所有答案.我本人跳过了编辑批准,将其留给了社区,该社区显然拒绝了它.在

The OP tried to edit and delete all of the answer due to "personal reasons". I myself skipped the edit approval and left it to the community, which apparently rejected it. Meta SO discussion about this kind of thing is found at What to do when an OP asks to delete my code from my answer? , including what the OP should do.

这篇关于这里的语法错误到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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