pattern matching - SML - Base case, wrong return value for declared datatype -


i'm having trouble base case of function i'm writing. have datatype i've declared. in base case need replace nil something. how tell function won't return anything?

datatype 'a newthing = cons of 'a option * (unit -> 'a newthing);  fun permutation [] = cons(none, fn () => nil) |   permutation l = .....;   error: operator , operand don't agree [tycon mismatch] operator domain: 'z option * (unit -> 'z newthing) operand:         'z option * (unit -> 'y list) in expression: cons (none,(fn () => nil)) 

edit: i'm not allowed change datatype.

your datatype has cons value constructor (indicating element), want nil constructor (indicating no further elements).

the typechecker gives type error, because attempt return nil, of 'a list type, rather 'a newthing.

what want add nil constructor 'a newthing datatype, , return value in base case.

datatype 'a newthing = cons of 'a option * (unit -> 'a newthing)                      | nil  fun permutation [] = nil   | permutation l = ... 

if not change datatype, there no way terminate list. keep going, thing can have cons, represents element.

assuming there no none elements in lists want represent, let value of none signify end of list, , keep returning none infinitely once run out of elements.

fun permutation [] = cons(none, permutation []) 

you throw exception, if wish:

exception endofnewthing  fun permutation [] = raise endofnewthing 

honestly, though, these pretty poor solutions compared adding new value constructor.


Comments