Created
September 25, 2014 01:09
-
-
Save siwka/c01f523d9f32ab827e0e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lisp_eval(x) | |
if x == '1' | |
1 | |
elsif x == '#t' | |
true | |
else | |
list = x.gsub(" ", '').chars | |
parser(list) | |
end | |
end | |
def parser(list) | |
return list.first.to_i unless list.join('').match(/\(.*\)/) | |
sub_list = list[1..-2] | |
if sub_list.join('').match(/\(.*\)/) | |
start_idx = sub_list.index('(') | |
end_idx = sub_list.length - sub_list.reverse.index(')') | |
val = parser(sub_list[start_idx..end_idx]) | |
before = sub_list[0..start_idx] | |
after = sub_list[end_idx..sub_list.length] | |
sub_list = (before << val) + after | |
end | |
operator = sub_list.shift | |
sub_list.reduce { |x,y| x.to_i.send(operator, y.to_i)} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment