Recursive functions

A

Army1987

David Thompson said:
<OT> Aside: IIRC IME the base case was Steele or maybe Minsky, but the
principle obviously is invariant under a 'translation' like that.

Do you mean distance(someone,Hofstadter) < distance(you,Hofstadter),
which is a good example of recursion, but should properly be written
in your sentence form with 'I', or ...

distance(someone,Hofstadter) < distance(someone,you) which is better
as an example of heuristic or at least depth-first search?

;-o </>

That's how my teacher implements the function to add an item at the
end of a list (I copied and pasted, translated the identifiers from
Italian to English -- keeping the capitalization style, removed his
comments and added mine):

typedef struct LI {
InfoType Info;
struct LI *Next;
} ListItemType, *ListType;
#include <stdlib.h>
int EmptyList (ListType List)
/* Why don't directly use (List == NULL) ? */
{
if (List == NULL)
return 1;
else
return 0;
}
void InsertAtEnd (ListType *List, InfoType Info)
{
ListType Ptr;
if (EmptyList (*List))
{
Ptr = malloc(sizeof(TipoElemLista));
Ptr->Next = NULL;
/* I've heard that, on 26 December 2006, someone used a
* program with that function on a DS9K on board of a small
* boat somewhere in the Pacific Ocean, and the malloc above
* had failed. */
Ptr->Info = Info;
/* What if InfoType is an array type? */
*List = Ptr;
}
else InsertAtEnd(&((*List)->Next), Info);
/* This could easily be the ugliest looking line of code I've
* ever seen in any language, and is the most effective way of
* convincing one's students that linked lists are evil, and
* to prevent them from ever using linked lists. */
}

Why? Because this is the *only* way our textbook implements such a
function. (Actually, it uses
typedef enum {false, true} boolean;
and declares EmptyList to return such a type.) Even if the teacher
also gave an almost sane iterative version of the function (yes, it
uses a while when a for would do that, and it doesn't check the
result of malloc, but it is much less insane than the one in the
book), the only fact that he copied such a function from that book
and showed it to us caused *all* the students which took the only
exam in which he ever asked to write a function to add an element
at the end of a list, to fail.
 
A

Army1987

typedef struct LI {
InfoType Info;
struct LI *Next;
} ListItemType, *ListType;
#include <stdlib.h>
int EmptyList (ListType List)
/* Why don't directly use (List == NULL) ? */
{
if (List == NULL)
return 1;
else
return 0;
}
void InsertAtEnd (ListType *List, InfoType Info)
{
ListType Ptr;
if (EmptyList (*List))
{
Ptr = malloc(sizeof(TipoElemLista));

That is, ListItemType.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top