如何分隔两个语句(或分号的等价)(How to seperate two statements (or the semicolon's equivalent))
oddSquareSum :: Integer
oddSquareSum =
let oddSquares = filter odd $ map (^2) [1..]
belowLimit = takeWhile (<10000) oddSquares
in sum belowLimit
--extra code to print the list of integers
show belowLimit
这是打印奇数平方和的代码。 平方奇数列表在列表中, belowLimit ,我该如何打印?
将它作为额外的行添加到最后,会产生编译错误,因为show函数调用被视为sum函数的参数。
oddSquareSum :: Integer oddSquareSum = let oddSquares = filter odd $ map (^2) [1..] belowLimit = takeWhile (<10000) oddSquares in sum belowLimit --extra code to print the list of integers show belowLimitThis is the code to print the sum of squares of odd numbers. The list of squared odd numbers is in the list, belowLimit and how can i print it?
Adding it as an extra line at the end, gives compilation errors as the show function call is treated as sum function's argument.
最满意答案
只是为了完整性,如果你只想输入一些临时代码进行调试,既不想更改签名也不想拆分函数,你也可以选择使用traceShow的Debug.Trace :
import Debug.Trace (traceShow) oddSquareSum :: Integer oddSquareSum = let oddSquares = filter odd $ map (^2) [1..] belowLimit = takeWhile (<10000) oddSquares in traceShow belowLimit $ sum belowLimitJust for completeness, if you just want to put in some temporary code for debugging and neither want to change the signature nor split up the function, you also have the option to use traceShow from Debug.Trace:
import Debug.Trace (traceShow) oddSquareSum :: Integer oddSquareSum = let oddSquares = filter odd $ map (^2) [1..] belowLimit = takeWhile (<10000) oddSquares in traceShow belowLimit $ sum belowLimit更多推荐
发布评论