Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我只是在学习 Haskell。我认为这会产生一个阶乘函数......
(在 ghci 内)
Prelude> let ft 0 = 1 Prelude> let ft n = n * ft (n - 1) Prelude> ft 5
(无限期挂起,直到 ^C)。
有人可以指出我正确的方向吗?
谢谢!
这两个单独的let语句相互独立地解释。首先ft 0 = 1定义一个函数,然后定义一个新函数ft n = n * ft (n - 1),覆盖第一个定义。
let
ft 0 = 1
ft n = n * ft (n - 1)
要使用两种情况定义一个函数,您必须将两种情况放在一个let语句中。要在 GHCI 提示符下的一行中执行此操作,您可以通过以下方式分隔两种情况;:
;
Prelude> let ft 0 = 1; ft n = n * ft (n - 1) Prelude> ft 5 120