haskell interactive中的函数定义

Jac*_*cob 1 haskell types

factorial :: Integer -> Integer
factorial n = product [1..n]
Run Code Online (Sandbox Code Playgroud)

以下是好的:

let factorial n = product [1..n]
Run Code Online (Sandbox Code Playgroud)

我没有看到如何在交互式shell中添加类型声明.

Sam*_*den 5

如果您想自己指定类型签名,可以使用分号在ghci中执行此操作,即:

let factorial :: Integer -> Integer; factorial n = product [1..n]
Run Code Online (Sandbox Code Playgroud)

  • 为了超级挑剔,其工作原因是它是一个语句,一个包含两个声明的let语句.它解析为`let {factorial :: Integer - > Integer; 阶乘n =产品[1..n]}`. (2认同)

Cir*_*uit 5

在此处说明的多行设置旁边,如果您不想编写分号,则可以使用此设置.

?> :{
?> | let factorial :: Integer -> Integer
?> |     factorial n = product [1..n]
?> :}
?> :t factorial
factorial :: Integer -> Integer
Run Code Online (Sandbox Code Playgroud)