Why is the non-deterministic choice function in Curry's std lib not defined straightforwardly but rather with a helper 2-argument function?

后端 未结 1 1880
暗喜
暗喜 2021-02-19 05:32

Consider a function choose in Curry programming language with the specification that \"(choose xs) non-deterministically chooses one element from the l

1条回答
  •  生来不讨喜
    2021-02-19 05:52

    I believe there are no semantic differences, but the one with the helper function is more efficient, particularly in the common case (in certain styles of programming) of a list with one element. In this case a choice point is avoided which with your version would need to be set up to call recursively with [] which is then bound to fail.

    A smarter optimizer might figure this out for itself, but handling all kinds of similar situations is likely to be challenging - the compiler would need to try to specialize applications for each of the possible constructors in a datatype, remove those where failure always occurs, and remove choice points when only one possibility remains.

    0 讨论(0)
提交回复
热议问题