且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么Curry的std lib中的非确定性选择函数没有直接定义,而是使用了辅助函数2参数函数?

更新时间:2022-10-24 12:00:58

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.