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