3 ms·
> In my experience, telling them to "just trust it" and such seems to not help much. Just call that recursive function something else. Give it some other name,
by kazinator 9y ago
> In my experience, telling them to "just trust it" and such seems to not help much.
Just call that recursive function something else. Give it some other name, and say that we must take it for granted that this other name is a function which is already debugged and tested by another team of programmers:
(defun our-member (item list)
(cond
((atom list) nil)
((eql item (car list)) list)
(t (member item (cdr list)))))
Here, we rely on member, a standard function in the language already, and so there is no recursion.
We first convince ourselves that what we have works.
And then, final step: since what we have works, it means that our-member is a correct implementation of member, which means that the two are equivalent. Equivalent functions are substitutable. And so we can just perform that substitution: replace the member call with our-member.
By the way, it occurs to me that Hofstadter's Gödel, Escher and Bach might be a good source of ideas on how to deal with presenting and teaching self-referential topics.
- userbinator 9y agoGive it some other name, and say that we must take it for granted that this other name is a function which is already debugged and tested by another team of programmers That seems like it's "begging the question" --- as in, "if there's already a function written to do what I want, why do I need it to write one to do it...?"
- kazinator 9y agoBut that problem is present in virtually all homework in CS. Someone already wrote a Celsius to Fahrenheit conversion function, a simple ray-tracer, a quicksort, a toy Lisp, a text-file-based simple inventory system, etc ... We could make up a back story about this to give it real-world plausibility: yes, the function is already written, but it's in an expensive, proprietary third-party library which we cannot ship without paying royalty fees and being bogged down with various inconvenient restrictions.
- dingo_bat 9y agoBut you don't call a library bubble sort function inside your own implementation of bubble sort!
- aidenn0 9y agoMy idea for teaching it is to start with just a function that calls the library function: (defun our-member (item list) (cond (t (member item list)))) Now everyone in the class should quickly agree that this function works. Next say "our goal is to write our own implementation of member, so we will handle one special case at a time until we don't need to call member anymore" so step 1: (defun our-member (item list) (cond ((atom list) nil) ;; if it's not a non-empty list, it can't be a list containing item (t (member item (cdr list))))) step 2: (defun our-member (item list) (cond ((atom list) nil) ;; if it's not a non-empty list, it can't be a list containing item ((eql (car list) item) list) ;; if the first element of the list is our item, ;; we have a match and return the whole list (t (member item list)))) step 3: (defun our-member (item list) (cond ((atom list) nil) ;; if it's not a non-empty list, it can't be a list containing item ((eql (car list) item) list) ;; if the first element of the list is our item, ;; we have a match and return the whole list (t (member item (cdr list))))) ;; we already know that the first item is not a match, so we can just run ;; member on the tail of the list And now demonstrate that replacing member with our-member will still be correct. [edit] derp (atom nil) => T
- chii 9y agoI'd say that the function only handles input n-1, and your function needs to do n.
- aidenn0 9y agoThat is a brilliant idea. I'm going to borrow it next time I teach recursion.