开发者

Convert Python to Haskell / Lambda calculus

Wha开发者_如何学编程t is the Python code in Haskell and Lambda calculus?

def f1():
   x = 77
   def f2():
      print x
   f2
f1

My attempt in lambda calculus

\x. 77 (\x.x)


In Haskell:

f1 = f2
    where x  = 77
          f2 = print x

Refactoring, since IO just confuses the issue:

f1 = f2
    where x  = 77
          f2 = x

Refactoring:

f1 = x
    where x = 77

Refactoring, since you want the variable?

f1 = (\x -> x) 77

Beta reduce:

f1 = 77

And you have your program in Haskell.

Lambda calculus doesn't have numeric literals (unlike Haskell), so we must use Church numerals. So compiling "77" to lambda calculus:

f1 = \f.\x. f (f (f (f ( ... ) x)))

And there's your Python program in Lambda Calculus.


Haskell:

f1 :: IO ()
f1 = let x = 77
         f2 = print x
     in f2

main :: IO ()
main = f1

Or to be more like your lambda calculus:

f1 :: Int
f1 = let f2 = x
         x = 77
     in f2

main :: IO ()
main = print f1


I don't know python, so I could be completely wrong, but this is my Haskell interpretation.

f1 = let x = 77 in show x

Or, since you've got a constant there

f1 = show 77


In lambda calculus:

λprint. print 77
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜