且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算两点之间的距离(Haskell)

更新时间:2022-10-25 19:25:51

你正在做一个indendation错误,这应该可以工作 - 看看where子句是如何缩进的:

distance(x1,y1)(x2,y2)= sqrt(x'* x'+ y'* y')
其中
x'= x1 - x2
y'= y1 - y2


Given an input of two tupples, i want to be able to calculate the distance between two points using the formula: distance = sqrt ( (x1 - x2) ^ 2 + (y1 - y2) ^2 )

so i want the function call and output to look like this:

-- > distance (5 , 10) (3 , 5)
-- 5.385...

when i try to run my code below, it tells me parse error on input 'where'. Can anyone help me resolve my issue? Here is my code:

distance (x1 , y1) (x2 , y2) = sqrt (x'*x' + y'*y')
where
    x' = x1 - x2
    y' = y1 - y2

You are making an indendation error, this should work- see how where clause is indented:

distance (x1 , y1) (x2 , y2) = sqrt (x'*x' + y'*y')
    where
      x' = x1 - x2
      y' = y1 - y2