Haskell在递归时打印? [英] Haskell Print while in recursion?
本文介绍了Haskell在递归时打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
foo:: Int -> Int -> Int
foo z x = if (z < 100)
then z * foo (z+(x*z)) z
else z
每次从它本身调用它时,你会如何输出(整数z)输出?你可以有返回IO和Int的函数吗?你需要一个辅助函数吗?
How would you print out(the integer z) an output every time it gets called from itself?Can you have function that returns an IO and Int? Do you need a secondary function?
推荐答案
为了简单起见,你可以使用 trace 。然而,它并不欢迎真正的生产代码,因为它会破坏参考透明度。
trace
需要打印一个字符串
并返回一个值。
For simplicity, you can use trace. However it's not welcomed for real production code as it breaks referential transparency.
trace
takes a String
to print and a value to return.
import Debug.Trace
foo:: Int -> Int -> Int
foo z x = trace ("z = " ++ show z) $ if (z < 100)
then z * foo (z+(x*z)) z
else z
*Main> foo 1 2
z = 1
z = 3
z = 6
z = 24
z = 168
72576
这篇关于Haskell在递归时打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文