What exactly is the syntax error here?

2019-09-19 23:53发布

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

标签: ocaml
1条回答
再贱就再见
2楼-- · 2019-09-20 00:29

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.

查看更多
登录 后发表回答