Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
257 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...