views:

369

answers:

4

What 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)
+3  A: 

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
Greg Bacon
A: 

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
Matt Ellen
A: 

In lambda calculus:

λprint. print 77
Apocalisp
The Python program doesn't take an argument for the `print` function.
MtnViewMark
It does, implicitly.
Apocalisp
+3  A: 

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.

Don Stewart
+1 just for using Church numerals. I lol'ed.
Meredith L. Patterson