Skip to main content
added 80 characters in body
Source Link

You're working in an office. If a question comes up, the first person toyou consult is youyourself. If the person to consult doesn't know the answer, they consult their manager, and upon receiving the answer pass it down the hierarchy again. Recurse until you have the answer.

So:

ask( person from, person to ):
  if not: from knows the answer
     ask( from's manager, from )
  tell( from, to )

and invoke this with

ask(me,me)

(You still have to define tell(from,to) and make sure that's a no-op if from==to.)

I like this example because it is not trivially iteratable as tail recursion. This explicitly goes up and down the stack.

Next step: if you ask something of someone, you take out a notepad to record what they tell you. If that person doesn't know, they take out a notepad and ask further. Now you know where stack overflow comes from: there is a bunch of notepads open simultaneously, and they all take up some amount of memory.

You're working in an office. If a question comes up, the person to consult is you. If the person to consult doesn't know the answer, consult their manager. Recurse until you have the answer.

So:

ask( person from, person to ):
  if not: from knows the answer
     ask( from's manager, from )
  tell( from, to )

and invoke this with

ask(me,me)

(You still have to define tell(from,to) and make sure that's a no-op if from==to.)

I like this example because it is not trivially iteratable as tail recursion. This explicitly goes up and down the stack.

Next step: if you ask something of someone, you take out a notepad to record what they tell you. If that person doesn't know, they take out a notepad and ask further. Now you know where stack overflow comes from: there is a bunch of notepads open simultaneously, and they all take up some amount of memory.

You're working in an office. If a question comes up, the first person you consult is yourself. If the person to consult doesn't know the answer, they consult their manager, and upon receiving the answer pass it down the hierarchy again. Recurse until you have the answer.

So:

ask( person from, person to ):
  if not: from knows the answer
     ask( from's manager, from )
  tell( from, to )

and invoke this with

ask(me,me)

(You still have to define tell(from,to) and make sure that's a no-op if from==to.)

I like this example because it is not trivially iteratable as tail recursion. This explicitly goes up and down the stack.

Next step: if you ask something of someone, you take out a notepad to record what they tell you. If that person doesn't know, they take out a notepad and ask further. Now you know where stack overflow comes from: there is a bunch of notepads open simultaneously, and they all take up some amount of memory.

added 310 characters in body
Source Link

You're working in an office. If a question comes up, the person to consult is you. If the person to consult doesn't know the answer, consult their manager. Recurse until you have the answer.

So:

ask( person from, person to ):
  if not: from knows the answer
     ask( from's manager, from )
  tell( from, to )

and invoke this with

ask(me,me)

(You still have to define tell(from,to) and make sure that's a no-op if from==to.)

I like this example because it is not trivially iteratable as tail recursion. This explicitly goes up and down the stack.

Next step: if you ask something of someone, you take out a notepad to record what they tell you. If that person doesn't know, they take out a notepad and ask further. Now you know where stack overflow comes from: there is a bunch of notepads open simultaneously, and they all take up some amount of memory.

You're working in an office. If a question comes up, the person to consult is you. If the person to consult doesn't know the answer, consult their manager. Recurse until you have the answer.

So:

ask( person from, person to ):
  if not: from knows the answer
     ask( from's manager, from )
  tell( from, to )

and invoke this with

ask(me,me)

(You still have to define tell(from,to) and make sure that's a no-op if from==to.)

I like this example because it is not trivially iteratable as tail recursion. This explicitly goes up and down the stack.

You're working in an office. If a question comes up, the person to consult is you. If the person to consult doesn't know the answer, consult their manager. Recurse until you have the answer.

So:

ask( person from, person to ):
  if not: from knows the answer
     ask( from's manager, from )
  tell( from, to )

and invoke this with

ask(me,me)

(You still have to define tell(from,to) and make sure that's a no-op if from==to.)

I like this example because it is not trivially iteratable as tail recursion. This explicitly goes up and down the stack.

Next step: if you ask something of someone, you take out a notepad to record what they tell you. If that person doesn't know, they take out a notepad and ask further. Now you know where stack overflow comes from: there is a bunch of notepads open simultaneously, and they all take up some amount of memory.

Source Link

You're working in an office. If a question comes up, the person to consult is you. If the person to consult doesn't know the answer, consult their manager. Recurse until you have the answer.

So:

ask( person from, person to ):
  if not: from knows the answer
     ask( from's manager, from )
  tell( from, to )

and invoke this with

ask(me,me)

(You still have to define tell(from,to) and make sure that's a no-op if from==to.)

I like this example because it is not trivially iteratable as tail recursion. This explicitly goes up and down the stack.