SML:模式匹配

SML: Pattern Matching

我对SML有一点经验,我正在尝试制作一个扫描仪。我被困在以下代码中。感谢任何帮助。

- fun nextChar nil = NONE
    | nextChar (head::tail) = SOME (head, tail);
- val a = [#"a",#"b",#"d",#"c"];
- val (head, tail) = nextChar a;

我收到有关模式和表达式 return 类型不匹配的错误。有人可以指出我可以使用哪种模式来匹配表达式 return type.

错误:

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,...)

正如 Simon 所写,在尝试将其用作列表之前,您需要检查 nextChar 的 return 值。您 return 正在 (char * char list) option 而不是 char * char list。您可能希望对结果进行模式匹配以检查它是否是 SOME 元组然后使用它:

val it = case nextChar (a) of
    NONE => defaultChar
  | SOME (head, tail) => head;