你怎么写数学| x | 在哈斯克尔?

Uns*_*der 2 math haskell module

你如何得到(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"
Run Code Online (Sandbox Code Playgroud)

两个值之间的差异必须是正的.

lef*_*out 10

是的,abs是你想要的功能.这是|的常规名称 x | 在大多数语言中.

顺便说一句,你可能不应该为四个列表元素硬编码.它既不安全(如果有人给你一个包含五个元素的列表怎么办?)又重复.只需递归列表,并在找到距离太大的一对时中止:

getJOL (w:x:ys)
  | abs (x - w) >= 20  = "Bad calibration"
getJOL (_:xs) = getJOL xs
getJOL [] = "Good calibration"
Run Code Online (Sandbox Code Playgroud)


Jon*_*220 5

只需使用绝对值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"
Run Code Online (Sandbox Code Playgroud)