私は再帰的に2つの他の関数(step1 & step2)を呼び出すメイン関数(foo)を持っています。 fooはa1をa2に追加し、count
の時間量を返してから(a1、a2)を返します。各ステップで変数count、a1、a2をどのように出力できますか?関数内から変数を出力するにはどうすればよいですか?
-- adds a1 to a2 a `count` number of times
-- returns (a1, a2) once count reaches 0
foo :: Integer -> Integer -> Integer -> (Integer, Integer)
foo count a1 a2 | count == 0 = (a1,a2)
| otherwise = foo count' a1' a2'
where (count', a1', a2') = let (count'', a1'', a2'') = step1 count a1 a2
in step2 count'' a1'' a2''
-- adds a2 to a1. How to print out count, a1 and a2' here?
step1 :: Integer -> Integer -> Integer -> (Integer, Integer, Integer)
step1 count a1 a2 = (count, a1, a2')
where
a2' = a1 + a2
-- decrements count by 1. How to print out count', a1 and a2 here? Or can I do these prints somewhere in the `foo` function?
step2 :: Integer -> Integer -> Integer -> (Integer, Integer, Integer)
step2 count a1 a2 = (count', a1, a2)
where
count' = count - 1
これは、より大きなコードベースのコードの簡略化されたバージョンです。私は別のアプローチを使用することにオープンです。私が探しています出力例は次のとおりです。
$> fooという3 4 5
(4、17)
編集:私はちょうど私はおそらく、リスト内の中間結果を格納し、そのリストから印刷可能性が実現。しかし、関数の引数としてリストを渡さなければならないと思うのは正しいですか?
ありがとうございます。これはfoo関数ではうまくいくかもしれませんが、私は各ステップ関数(あなたの例のように最後ではなく)で印刷する必要があります。ですから、step1とstep2の出力タイプを変更する必要があります。これは、foo関数の内部でいくつかのバインディングの問題を引き起こします。アイデア? 編集:気にしないでください。私は今あなたのソリューションが完璧に動作することを知ります。どうもありがとうございました。 –