DEV Community

Discussion on: Every developers 'oh my god I get it' moment.

Collapse
 
craser profile image
Chris Raser

I remember the exact moment I understood recursion. I was sitting on this bench, and trying to wrap my head around this function:

(define sum (lambda (ls)
                (if (null? ls)
                    0
                    (+ (car ls) (sum (cdr ls))))))

(sum '(8 6 7 5 3 0 9))

38

I really had to struggle with the idea that the function added up the whole list without there being any variable that actually held that number.

At some point, my brain pictured a red carpet unrolling infinitely into space, and the light went on. The idea locked into place and I totally understood. I loved that class. (C211) It was a summer session, so it was warm days in flip-flops practicing my tuba, and evenings hanging out in the CS dept computer lab with the other students. Good memories.

Collapse
 
moopet profile image
Ben Sinclair

I think the toughest thing for someone who doesn't know it coming to this code is translating things from "car" and "ls" and "cdr" and so on. In fact, "define", "sum" and "if" are the only words here that are obvious to a newcomer.

Collapse
 
elkhan profile image
Elkhan

Awesome piece of code to bring one back down to Earth. A-ha moment killer. 😉

Collapse
 
craser profile image
Chris Raser

I was a music student at the time, so programming was completely new and amazing to me.