88827b8336 2010-12-02 kinaba: # I'm lazy. So I define only the primitives needed for tarai. 88827b8336 2010-12-02 kinaba: @@lazy(x) { fun(){x} }; 88827b8336 2010-12-02 kinaba: @lazy - (x,y) { fun(){@value(@lazy(x)() - @lazy(y)()) } }; 88827b8336 2010-12-02 kinaba: @lazy <= (x,y) { fun(){@value(@lazy(x)() <= @lazy(y)()) } }; 88827b8336 2010-12-02 kinaba: @lazy if (c,t,e) { fun(){@value(if @lazy(c)() then @lazy(t())() else @lazy(e())()) } }; 88827b8336 2010-12-02 kinaba: 88827b8336 2010-12-02 kinaba: def tarai(x,y,z) 88827b8336 2010-12-02 kinaba: { 88827b8336 2010-12-02 kinaba: if x<=y then 88827b8336 2010-12-02 kinaba: y 88827b8336 2010-12-02 kinaba: else 88827b8336 2010-12-02 kinaba: tarai( tarai(x-1,y,z), tarai(y-1,z,x), tarai(z-1,x,y) ) 88827b8336 2010-12-02 kinaba: }; 88827b8336 2010-12-02 kinaba: 88827b8336 2010-12-02 kinaba: # print( tarai(12, 6, 0) ); 88827b8336 2010-12-02 kinaba: print( @lazy( tarai(12, 6, 0) )() ); 88827b8336 2010-12-02 kinaba: 88827b8336 2010-12-02 kinaba: 88827b8336 2010-12-02 kinaba: #### Omake: only Z is made lazy. 88827b8336 2010-12-02 kinaba: def taraiZ(x,y,z @lazy) 88827b8336 2010-12-02 kinaba: { 88827b8336 2010-12-02 kinaba: if x<=y then 88827b8336 2010-12-02 kinaba: y 88827b8336 2010-12-02 kinaba: else 88827b8336 2010-12-02 kinaba: taraiZ( taraiZ(x-1,y,z), taraiZ(y-1,@lazy(z)(),x), taraiZ(@lazy(z)()-1,x,y) ) 88827b8336 2010-12-02 kinaba: }; 88827b8336 2010-12-02 kinaba: print( taraiZ(12,6,0) );