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
/* I was searching for an algorithm that gave you the next integer (counting | |
* upwards) that has the same hamming weight as the current number, and I | |
* couldn't find anything. So here's a function that does that for you. If | |
* there is no 'next number' (i.e. this is the largest number that has this | |
* hamming weight), it simply returns the same number again. | |
* | |
* This can probably be optimized; I whipped it up pretty quickly. Relatedly, I | |
* only tested a few edge cases I could think of, so if you use this, be sure | |
* to test it more thoroughly. | |
* |
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
class Lisp | |
Fs = { | |
:label => lambda {|name,value| Fs[name] = lambda { value } }, | |
:car => lambda {|sexp| sexp.first }, | |
:cdr => lambda {|sexp| sexp.slice(1, sexp.size) }, | |
:cons => lambda {|head,tail| [head] + tail }, | |
:atom => lambda {|sexp| !sexp.is_a?(Array) }, | |
:eq => lambda {|a,b| a == b }, | |
:if => lambda {|cnd,thn,els| cnd ? thn : els } | |
} |