Re: LISP to Ruby translation

I think that’s the first time I have seen an emoticon in ruby code

:slight_smile:

Neville B. wrote:

I think that’s the first time I have seen an emoticon in ruby code

:slight_smile:

Oh wow, you’re right. I need to fit that into some real code sometime,
heh. Well, not really, since I abhor such one-liners outside of
demonstrations, but it’d be nice…

Neil S. - [email protected]

‘A republic, if you can keep it.’ – Benjamin Franklin

A bit of fun to while away a boring afternoon

class Fixnum
def prev
self - 1
end
end

def add a, b
return a if b.zero?
if b > 0
add a.succ, b.prev
else
add a.prev, b.succ
end
end

def leval(args)
return args unless args.is_a? Array
args.map!{|a|
if a.is_a? Array
leval a
else
a
end
}
op, *args = args
functions[op][args]
end

def functions
{
:if => lambda{|args| if leval(args.first) then args[1] else args[2]
end },
:zero? => lambda{|args| not (args.first > 0 or args.first < 0) },
:+ => lambda{|args| args.inject{|s,i| add s, i}},
:- => lambda{|args| args.inject{|s,i| add s, -i}}
}
end

p leval([[:if, [:zero?, 0], :+, :-], 3,4])

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs