fun intf base next 0 = base | intf base next n = next (n, (intf base next (n-1))); val iota = intf [0] (op ::); local val append = foldl (op @) [] fun repeat x = intf [] (fn (_,ls) => x::ls) fun rep2each x n ls = map (fn e => (repeat x n) @ e) ls in fun change nil 0 = [[]] | change nil _ = [] | change (c::cs) x = append (map (fn n => rep2each c n (change cs (x - n*c))) (iota (x div c))) end;