首页 > 解决方案 > 无法理解 Haskell 跟踪

问题描述

标签: haskell

解决方案


trace不是一元函数,所以你不需要把它放在一个do块中。相反,trace它接受两个参数:第一个是要打印的字符串,第二个是要返回的结果。所以你需要这样做:

import Debug.Trace

factorial :: Int -> Int
factorial x =
    if x <= 1 then
        1
    else
        let helloMsg = "Hola: x = " ++ show(x)
        in trace helloMsg (x * factorial (x - 1))

main = do
    print (factorial 6)

(另外,你也不需要一个domain,因为你只有一个语句。)


推荐阅读