[MLton] fold on lists that need to be non-empty

Henry Cejtin henry.cejtin@sbcglobal.net
Mon, 05 Jun 2006 14:47:51 -0500


Have  you  ever  thought  about  the need for a function like fold, but which
fails on empty lists?  The canonical example is something like max or min.

The simpler case would be

    fun ('e)
       reduce ((h::t): 'e list, f: 'e * 'e -> 'e): 'e =
          fold (t, h, f)

The more general version would need a separate function  to  take  the  first
element of the list and convert it to the result type:

    fun ('e, 's)
       reduce ((h::t): 'e list, f: 'e -> 's, g: 'e * 's -> s'): 's =
          fold (t, f h, g)