Arc Forumnew | comments | leaders | submitlogin
2 points by zachbeane 6139 days ago | link | parent

A less gross solution is available if you can ask if a key is present in a table.


1 point by randallsquared 6139 days ago | link

We differ on whether that would be less gross. :)

-----

2 points by zachbeane 6139 days ago | link

If there was an "intable" function, you could do it in 11 forms (20 symbols), but your solution requires 13 forms, or 22 symbols. The shorter solution would be less gross, by definition!

  (def memo (f)
    (let cache (table)
      (fn args
	(if (intable cache args)
	    (table args)
	    (= (table args) (apply f args))))))

-----

1 point by randallsquared 6139 days ago | link

If intable could be made to return the actual value in the table, I'd have no reservations, since you could

    (aif (intable cache args)
         (thevalue it) ; where the fn thevalue does cadr
                       ; or whatever it needs to do
         (= (table args) (apply f args)))
Scanning over the entire table twice, once to see if the key is there, and once to actually get the value, is what seemed gross to me about checking for the key first. No doubt that could be optimized by having tables keep a list of keys separately, but that seemed like a heavyweight fix.

I agree that your version is prettier on the surface, though.

-----