如何分隔两个语句(或分号的等价)(How to seperate two statements (or the semicolon's equivalent))

编程入门 行业动态 更新时间:2024-10-11 09:23:24
如何分隔两个语句(或分号的等价)(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 belowLimit

This 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 belowLimit

Just 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

更多推荐

本文发布于:2023-07-19 04:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1173717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分号   语句   两个   seperate   semicolon

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!