Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sml 采用类型单位的函数_Sml_Smlnj - Fatal编程技术网

Sml 采用类型单位的函数

Sml 采用类型单位的函数,sml,smlnj,Sml,Smlnj,我正在尝试创建一个具有以下类型的函数: unit -> (int list * int list * int list) 但我想知道,unit是一个空类型(没有值),那么如何处理它并返回3个int列表呢 谢谢类型单元不是空的。 它有一个值,拼写为(),通常称为“unit”,就像它的类型一样。 (单词“unit”的一个意思是“一件事”。) 例如: - (); val it = () : unit - val you_knit = (); val you_knit = () : unit

我正在尝试创建一个具有以下类型的函数:

unit -> (int list * int list * int list)
但我想知道,unit是一个空类型(没有值),那么如何处理它并返回3个int列表呢


谢谢

类型
单元
不是空的。
它有一个值,拼写为
()
,通常称为“unit”,就像它的类型一样。
(单词“unit”的一个意思是“一件事”。)

例如:

- ();
val it = () : unit
- val you_knit = ();
val you_knit = () : unit

- fun foo () = ([1], [2], [3]);
val foo = fn : unit -> int list * int list * int list
- foo ();
val it = ([1],[2],[3]) : int list * int list * int list
- foo you_knit;
val it = ([1],[2],[3]) : int list * int list * int list
(请注意,
()
不是空的参数列表;ML没有参数列表。)

严格来说,上述定义模式与值
()

如果没有模式匹配,它可能会如下所示:

- fun bar (x : unit) = ([1], [2], [3]);
val bar = fn : unit -> int list * int list * int list
- bar ();
val it = ([1],[2],[3]) : int list * int list * int list

在SML中,类型
单元
通常表示输入/输出动作,或者更一般地表示涉及副作用的事物。您正在寻找的某种函数的一个比较现实的例子是返回3个随机生成的列表。另一个例子是从标准输入中提取数字,例如:

open TextIO

fun split s = String.tokens (fn c => c = #",") s

fun toInt s = valOf (Int.fromString s)

fun toIntList line = map toInt (split line)

fun getInts prompt = 
    ( 
       print prompt;
       case inputLine(stdIn) of
           SOME line => toIntList line |
           NONE => []
     )

fun getLists() = 
     let
         val prompt = "Enter integers, separated by a comma: "
     in
         (getInts prompt, getInts prompt, getInts prompt)
     end
getlist
的类型为

val getLists = fn : unit -> int list * int list * int list
getlist的典型“运行”:

- getLists();
Enter integers, separated by a comma: 1,2,3
Enter integers, separated by a comma: 4,5,6
Enter integers, separated by a comma: 7,8,9
val it = ([1,2,3],[4,5,6],[7,8,9]) : int list * int list * int list
是的,语法
()
有点误导,因为这是一个实际值。