I have a little experience with SML and I am trying to make a scanner. I am stuck at the folowing CODE. Any help is appreciated.
- fun nextChar nil = NONE
| nextChar (head::tail) = SOME (head, tail);
- val a = [#"a",#"b",#"d",#"c"];
- val (head, tail) = nextChar a;
I get an error regarding mismatch of pattern and expression return type. Can someone please point out on which pattern can I use to match the expression return type.
Error:
stdIn:7.5-7.28 Error: pattern and expression in val dec don't agree [tycon mismatch]
pattern: 'Z * 'Y
expression: (char * char list) option
in declaration:
(head,tail) = nextChar a
stdIn:7.5-7.28 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
stdIn:7.5-7.28 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
nextChar a
returnsSOME (#"a", [#"b", #"d", #"c"])
, and the type of this value (char × char list) option) will never unify with the type of the pattern inval (head,tail) = ...
(being 'a × 'b). If you wroteval SOME (head, tail) = nextChar a
, it would work, but it is ill-adviced to deconstructSOME
/NONE
in a let-binding, since one pattern will exclude the other and cause a run-time exception. Rather, you should use case-of. – Simon Shine