What exactly is the syntax error here?

╄→гoц情女王★ 提交于 2019-12-14 03:22:42

问题


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?

Deleted code


回答1:


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
;;

Top level let binding has the form

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

but local let binding has the form

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

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.

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.



来源:https://stackoverflow.com/questions/28668323/what-exactly-is-the-syntax-error-here

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