6

たとえば、すべての呼び出しで haskell のリストまたは何かを出力するにはどうすればよいですか。

funct a list = funct (a + 1) (a : list) 
               print list here ??????? but how ? 
4

2 に答える 2

11

デバッグのために、

import Debug.Trace

funct :: Integer -> [Integer] -> Bool 
funct a list = trace (show list) $ funct (a + 1) (a : list)

どこでtrace :: String -> a -> a。内部で使用unsafePerformIOされるため、悪であり、デバッグ専用です。

遅延評価が原因で、デバッグ出力が驚くべき順序で表示され、プログラムが通常生成する出力とインターリーブされる場合があることに注意してください。

module TraceIt where

import Debug.Trace

funct :: Integer -> [Integer] -> Bool
funct 10 list = null list
funct a list = trace (show list) $ funct (a + 1) (a : list)

私は得る

*TraceIt> funct 1 []
[]
[1]
[2,1]
[3,2,1]
[4,3,2,1]
[5,4,3,2,1]
[6,5,4,3,2,1]
[7,6,5,4,3,2,1]
[8,7,6,5,4,3,2,1]
False

予想通り。

于 2012-04-21T22:23:03.593 に答える
1

ダニエル・フィッシャーが提案したのと同じですが、unsafePerformIOのみです。

> import System.IO.Unsafe
> let funct a list = unsafePerformIO $ do { print list; return $ funct (a + 1) (a : list) }

を使用するときに実際に何が起こっているかを説明する同様の質問を見てくださいunsafePerformIO

于 2012-04-21T22:27:11.863 に答える