[英]How do you write the mathematical |x| in haskell?
你怎么得到(x - y) < 20
总是积极的?
我想提出一个条件:
getJOL :: [Int] -> String
getJOL [w,x,y,z] = if x - w < 20 && y - x < 20 && z - y < 20
then "Good calibration"
else "Bad calibration"
两个值之间的差异必须是正的。
是的, abs
是你想要的功能。 这是|的常规名称 x | 在大多数语言中。
顺便说一句,你可能不应该为四个列表元素硬编码。 它既不安全(如果有人给你一个包含五个元素的列表怎么办?)又重复。 只需递归列表,并在找到距离太大的一对时中止:
getJOL (w:x:ys)
| abs (x - w) >= 20 = "Bad calibration"
getJOL (_:xs) = getJOL xs
getJOL [] = "Good calibration"
只需使用绝对值abs
。 这将检查绝对差异是否低于20。
getJOL :: [Int] -> String
getJOL [w,x,y,z] = if abs(x - w) < 20 && abs(y - x) < 20 && abs(z - y) < 20
then "Good calibration"
else "Bad calibration"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.