What is the essential difference between the two functions to find the first negative entry?

Bombyx mori

From Real World Ocaml, page 24 (see https://realworldocaml.org/v1/en/html/a-guided-tour.html#for-and-while-loops). The code is:

# let find_first_negative_entry array =
     let pos = ref 0 in
     while !pos < Array.length array && array.(!pos) >= 0 do
       pos := !pos + 1
     done;
     if !pos = Array.length array then None else Some !pos
  ;;
val find_first_negative_entry : int array -> int option = <fun>
# find_first_negative_entry [|1;2;0;3|];;
- : int option = None
# find_first_negative_entry [|1;-2;0;3|];;
- : int option = Some 1 

and

# let find_first_negative_entry array =
     let pos = ref 0 in
     while
       let pos_is_good = !pos < Array.length array in
       let element_is_non_negative = array.(!pos) >= 0 in
       pos_is_good && element_is_non_negative
     do
       pos := !pos + 1
     done;
     if !pos = Array.length array then None else Some !pos
  ;;
val find_first_negative_entry : int array -> int option = <fun>
# find_first_negative_entry [|1;2;0;3|];;
Exception: (Invalid_argument "index out of bounds").

The authors claimed that:

As a side note, the preceding code takes advantage of the fact that &&, OCaml's And operator, short-circuits. In particular, in an expression of the form expr1 && expr2, expr2 will only be evaluated if expr1 evaluated to true. Were it not for that, then the preceding function would result in an out-of-bounds error. Indeed, we can trigger that out-of-bounds error by rewriting the function to avoid the short-circuiting:"

But I still do not really understand why the first code works fine, and the second get the exception. I complied and both function behave as expected. But I am very puzzled why the second function does not work, since it used the && command as well, and it handled no negative entry with None exception as well.

stonemetal

It is the order of evaluation. In the first function Term1 && Term2 allows && short circuit behavior to protect us from Term2. In the second example the let bindings are always evaluated before the test occurs. Therefore there is no protection from Term2's bad behavior.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the essential difference between the two functions to find the first negative entry?

From Dev

what is the difference between the two regex(negative lookaround)?

From Dev

what is the difference between the two regex(negative lookaround)?

From Dev

What is the difference between these two functions in Javascript?

From Dev

What is the difference and issues between these two clojure functions?

From Java

What's the difference between these two functions

From Dev

What's the difference between these two functions?

From Dev

What is the difference between these two recursive functions?

From Dev

What is the difference between these two recursive ocaml functions?

From Dev

What's the difference between these two functions in JavaScript?

From Dev

What is the difference between these two ruby functions?

From Dev

What exactly is the difference between these two javascript functions?

From Dev

What is the difference between these two recursive functions?

From Dev

What is the difference and issues between these two clojure functions?

From Dev

What is the difference between these two ruby functions?

From Dev

What is the difference between scope of these two functions

From Dev

What are all the difference between these two Javascript functions?

From Dev

What is the difference between these two functions using async/await/TPL?

From Dev

What's the difference between these two ways to declare functions?

From Dev

What is the difference between these two ways of changing a functions prototype in JavaScript?

From Dev

What is the difference between these two C functions in terms of handling memory?

From Dev

Javascript : What is the difference between these two fat arrow functions?

From Java

What is the difference between find(), findOrFail(), first(), firstOrFail(), get(), list(), toArray()

From Dev

What is the difference between $("") and $.find("")?

From Dev

What are the essential functions to find duplicate elements within a list?

From Dev

Difference between two Times in Minutes are coming as negative

From Dev

What is the essential difference between compound command and normal command in bash?

From Dev

what's the essential difference between int a=0; and int a; a=0;?

From Java

What is the difference between these arrow functions

Related Related

  1. 1

    What is the essential difference between the two functions to find the first negative entry?

  2. 2

    what is the difference between the two regex(negative lookaround)?

  3. 3

    what is the difference between the two regex(negative lookaround)?

  4. 4

    What is the difference between these two functions in Javascript?

  5. 5

    What is the difference and issues between these two clojure functions?

  6. 6

    What's the difference between these two functions

  7. 7

    What's the difference between these two functions?

  8. 8

    What is the difference between these two recursive functions?

  9. 9

    What is the difference between these two recursive ocaml functions?

  10. 10

    What's the difference between these two functions in JavaScript?

  11. 11

    What is the difference between these two ruby functions?

  12. 12

    What exactly is the difference between these two javascript functions?

  13. 13

    What is the difference between these two recursive functions?

  14. 14

    What is the difference and issues between these two clojure functions?

  15. 15

    What is the difference between these two ruby functions?

  16. 16

    What is the difference between scope of these two functions

  17. 17

    What are all the difference between these two Javascript functions?

  18. 18

    What is the difference between these two functions using async/await/TPL?

  19. 19

    What's the difference between these two ways to declare functions?

  20. 20

    What is the difference between these two ways of changing a functions prototype in JavaScript?

  21. 21

    What is the difference between these two C functions in terms of handling memory?

  22. 22

    Javascript : What is the difference between these two fat arrow functions?

  23. 23

    What is the difference between find(), findOrFail(), first(), firstOrFail(), get(), list(), toArray()

  24. 24

    What is the difference between $("") and $.find("")?

  25. 25

    What are the essential functions to find duplicate elements within a list?

  26. 26

    Difference between two Times in Minutes are coming as negative

  27. 27

    What is the essential difference between compound command and normal command in bash?

  28. 28

    what's the essential difference between int a=0; and int a; a=0;?

  29. 29

    What is the difference between these arrow functions

HotTag

Archive