Hacker News new | past | comments | ask | show | jobs | submit login

One that comes immediately to mind is that you might want + to handle vectors as well as scalars. Something like:

    (define scalar-sum +)

    (define (+ . arguments)
        (if (vectors? 'arguments)
            (vector-sum 'arguments)
            (apply scalar-sum arguments)))


    vectors? and vector-sum not shown for clarity.

Edit: or maybe you want any sum in excess of (say) $5,000 to alert a manager.

    (define old-plus +)
    (define (+ . args)
      (let ((value (apply old-plus args)))
        (if (> value 5000)
            (begin
              (alert-manager)
              value)
            value)))
The nice thing here is that this Just Works anywhere + appears in the code. You don't need to put the conditional and the call to alert-manager everywhere you're calculating a sum. Of course, in a real implementation you'd want to pass stuff to alert-manager like what module you're in, the transaction number, etc.



Fair, I should have tried to use my imagination there. Basically operator overloading? I forgot that generic functions are different beasts, and I've rarely had need for this, myself.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: