Arc Forumnew | comments | leaders | submitlogin
2 points by CatDancer 5838 days ago | link | parent

When you have two expressions that are similar,

  (((app* area) 'budget*) sub-area)

  (((app* area) 'forecast*) sub-area)
write a function consisting of the parts that are the same, and make the parts that are different into variables:

  (def (price area type subarea)
    (((app* area) type) subarea))

  (def area-situation (area sub-area)
    (let area-budget (price area 'budget* sub-area)
      (let area-forecast (price area 'forecast* sub-area)
         (let delta (- area-forecast area-budget)
I would continue iteratively with this process of turning similar expressions into functions, but it looks like you may have a bug in your code? You store a new budget but print out the old one. Unless that's what you wanted?

There are a couple of advantages to this process of noticing expressions in your code that are similar and turning them into functions. First, you don't have to figure everything out in advance. You write some code and get it to work right. Then you look for similar expressions and turn them into functions. In practice this works a lot better than trying to figure out what functions you're going to need ahead of time.

The second advantage is that it makes your code easier to change. For example, suppose you wanted to change your representation of your prices from tables inside of tables into lists, or perhaps one table with a compound key. If you have lots of places where you say (((app* area) type) subarea)) then you'd need to change all of those, but if you have a few functions like price then you only need to change those.



1 point by thaddeus 5837 days ago | link

ugh, sorry.

The code didn't have the bug the results I gave did (too much copying and pasting) :)

the first function actually returns 200. (forecast 1996.0 budget 200)

what I had intended to convey was that I had believed I was required to use the full syntax (((app* area) 'budget*) sub-area) in order to get the 200, which I did, but I wanted to just use 'area-budget' to make it more readable.

But as I learned there were so many things I was doing wrong it wasn't funny ... even though I am laughing now. bewahahahah ! :)

Thanks for the reply. T.

-----