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)
(另外,你也不需要一个do
块main
,因为你只有一个语句。)
推荐阅读
- python - python if else 语句只返回第一个条件
- javascript - 从按下的 DOM 创建的按钮中收集数据
- ios - 为 SceneKit ParticleEngine 粒子设置重力
- html - 单击按钮时使用输入元素更改标题的字体颜色
- paypal - 如何集成 PayPal 支付 AAC?
- python - Airflow ExternalTaskSensor 手动触发
- user-interface - 赛普拉斯 - 等到元素更改状态
- vba - MS Access 无法连接到 SAP“无法加载 LibRfc32.dll”错误 - VBA - SAP
- docker - 为什么我们需要在 Dockerfile 中有一个 Alpine 或 Ubuntu 基础镜像?
- angular - 如何在 Angular 中使用外部类?