Fixed point combinator in Haskell

前端 未结 5 1507
[愿得一人]
[愿得一人] 2020-12-16 16:31

The fixed point combinator doesn\'t always produce the right answer given the definition:

fix f = f (fix f)

The following code does not ter

5条回答
  •  不思量自难忘°
    2020-12-16 16:38

    You can't define fix the way you've mentioned since f x may not even be comparable. For instance, consider the example below:

    myFix f x | f x == f (f x)  = f x
              | otherwise       = myFix f (f x)
    
    addG f a b =
      if a == 0 then
        b
      else
        f (a - 1) (b + 1)
    
    add = fix addG -- Works as expected.
    -- addM = myFix addG (Compile error)
    

提交回复
热议问题