Re: C Programers
- Posted by Jason Gade <jaygade at ??hoo.com> Jul 23, 2007
- 525 views
don cole wrote: > > Hello C Programers, > > Can any of you translate this to Euphoria? > > int sum_list(struct list_node *l) > { > if(l == NULL) > return 0; > return l.data + sum_list(l.next); > } > > Don Cole I've read the replies to this, but I wanted to go back to the original. This example in C doesn't translate very well into Euphoria code, just so you know. First, let's examine the C version: int sum_list(struct list_node *l) -- this defines the function, returning an integer, and takes as a paramater a pointer to a list_node structure. A list_node structure is probably defined like this: struct list_node { int *data; -- or float, or double, or whichever list_node *next; }; See, it's kinda recursive on it's own. if (l == NULL){...} -- Generally, a pointer will either contain a valid RAM address (which has already been allocated) or NULL which is usually a fancy way of saying zero. NULL is almost always a marker for an invalid pointer. A linked list is one way the C uses to simulate sequence-like data that is usually iterated through in order from first to last. Another reason to use linked lists in C is that C does not have dynamic arrays -- once you declare an array its size is basically fixed. Heh. That probably sounds pretty complicated. While I could see a linked-list-type structure useful sometimes in Euphoria, it really wouldn't be needed here because sequences are dynamic. You don't have to manually allocate or deallocate memory in order to use them. -- "Any programming problem can be solved by adding a level of indirection." --anonymous "Any performance problem can be solved by removing a level of indirection." --M. Haertel "Premature optimization is the root of all evil in programming." --C.A.R. Hoare j.