Just for fun, here's an example of how one can completely ignore the bias towards a statement oriented programming style that's usually prevalent in Python and handle use I/O in a purely functional manner.
def appliedto(x): return lambda y:y(x())
def adder(x): return lambda y:int(x)+int(y)
def l_msg(s): return lambda:prnt(s)
def l_msgc(s): return lambda:prntc(s)
def l_v(v): return lambda:v
def ife(p,t,e): return lambda x: t() if p(x) else e()
def input(): return raw_input()
def seqe(*a): return reduce(binde,a)
def seq(*a): return reduce(bind,a)
def binde(x,y): return lambda:y(x())
def bind(x,y):
def actionpair(*a):
x(*a)
return y()
return actionpair
def passahead(x,y):
def actionpair(*a):
x()
return y(*a)
return actionpair
def prnt(s):
print s
return True
def prntc(s):
print s,
return True
hello = l_msg("Hello!")
goodbye = l_msg("Goodbye.")
question1 = l_msgc("Enter the first addend: ")
question2 = l_msgc("Enter the second addend:")
desc = l_msg("Enter two numbers, and I will add them.")
smsg = l_msgc("The sum of the numbers is ")
printnl = l_msg("")
seq(hello
,printnl
,desc
,printnl
,question1
,seqe(input
,adder
,appliedto(seq(question2
,input))
,passahead(seq(printnl
,smsg)
,prnt))
,printnl
,goodbye
,printnl)()
((lambda (x) (x x)) (lambda (x) (x x)))
Monday, February 16, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment