No doubts this is very elegant. However, I think that the underyling programming model is imperative: there's a map which is essentially a RAM. Each instruction writes to this RAM (alters the mappings in the map).
It seems that a stack-machine exhibits a more functional nature: an instruction either pushes or pops values onto/off the stack, thereby creating a local environment for subsequent computations. This approach is outlined in this series: http://www.codecommit.com/blog/cat/the-joy-of-concatenative-languages-part-1 (first chapter of three). -Itay http://javadots.blogspot.com On Dec 22, 1:59 pm, Robert Feldt <robert.fe...@gmail.com> wrote: > On Dec 22, 12:27 pm, verec <jeanfrancois.brouil...@googlemail.com> > wrote:> The design simplicity is certainly very appealing, though it appears > > that you can only operate on a predetermined fixed set of > > "registers" (ie: a and b in your example) and would need to define as > > many add_, sub_, mul_ ... variants as there are 'registers' in your > > model. > > Yes, although we can generalize: > > (defn addi [st reg i] {reg (+ (reg st) i)}) > (defn subi [st reg i] {reg (- (reg st) i)}) > (defn addr [st reg1 reg2] {reg1 (+ (reg1 st) (reg2 st))}) > > user=> (vm-exec [[:addi :a 1] [:addi :a 3] [:subi :b 1] > [:addr :a :b]]) > {:b -1, :a 3} > > > Also, simple arithmetic seems easy to implement, what about control > > flow (tests, branches, calls...) ? > > Yes, the way I envisage it is to have explicit program counter and > then assume instructions just increments it unless they set a new > value for it. Instead of reduce in the vm-exec I have to loop and set > the pc after each instruction. > > Cheers, > > Robert --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---