自引用定义在Haskell中不起作用

Mic*_*oni 1 haskell

Haskell的新手,但我遇到了令我困惑的事情,我无法解释或找到任何文档.

如果我有以下代码:

-- Simple 2D Coordinate Data Structure
data Point = Point
   { x :: !Float
   , y :: !Float }
   deriving (Show)

-- Constant origin (always at (0,0) unless otherwise specified)
origin = Point 0 0

-- Return a Point that has been shifted by the specified amounts
move :: Point -> Float -> Float -> Point
move p dx dy = Point { x=xs+dx, y=ys+dy }
    where
        xs = x p
        ys = y p
Run Code Online (Sandbox Code Playgroud)

现在,这段代码非常有效

*Main> origin
Point {x = 0.0, y = 0.0}
*Main> move origin 1 3
Point {x = 1.0, y = 3.0}
*main> otherpoint = move origin 4 4
*main> origin
Point {x = 0.0, y = 0.0}
*main> otherpoint
Point {x = 4.0, y = 4.0}
*main> :t otherpoint
otherpoint :: Point
Run Code Online (Sandbox Code Playgroud)

如果我试图移动一个点并将其分配给自己,则会出现问题

*main> otherpoint = move otherpoint 5 5
*main> otherpoint
^CInterrupted --Freezes
*main> otherpoint = move otherpoint 6 6
^CInterrupted --Freezes
Run Code Online (Sandbox Code Playgroud)

che*_*ner 7

=不执行任务; 它等同于两件事.您正在创建一个递归定义,通过说明otherpoint并且move otherpoint 5 5是相同的东西并且可以互换.这意味着当您尝试评估调用时move,它会尝试评估otherpoint,这会导致下一次调用move,等等.

你不能简单地在Haskell中"重新绑定"一个变量.相反,请使用其他名称.

nextpoint = move otherpoint 5 5
Run Code Online (Sandbox Code Playgroud)