1. C Programers
- Posted by don cole <doncole at p?cbell.ne?> Jul 22, 2007
- 505 views
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
2. Re: C Programers
- Posted by Bernie Ryan <xotron at bluefrog??om> Jul 22, 2007
- 517 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: returns int l is a pointer to a structure called list_node if l is zero then return 0 return l's member data + sum_list( l's next pointer) Bernie My files in archive: WMOTOR, XMOTOR, W32ENGIN, MIXEDLIB, EU_ENGIN, WIN32ERU, WIN32API Can be downloaded here: http://www.rapideuphoria.com/cgi-bin/asearch.exu?dos=on&win=on&lnx=on&gen=on&keywords=bernie+ryan
3. Re: C Programers
- Posted by CChris <christian.cuvier at ?gri?ulture.gouv.fr> Jul 22, 2007
- 518 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 What is the definition of list_node? Obviously it's a structure that has fields named data and next, but that's a little short. My guess is that you have to translate a linked list of nodes into a sequence of nodes. Then you'd only need to do this:
function sum_list_(integer sum,integer index,sequence nodes) if index<=length(nodes) then return sum_list_(sum+nodes[index][DATA],index+1,nodes) else return sum end if end function function sum_list(sequence nodes) return sum_list_(0,1,nodes) end function
Not the most efficient, but the closest to a verbatim translation. CChris
4. Re: C Programers
- Posted by don cole <doncole at ?acbell.ne?> Jul 23, 2007
- 521 views
CChris wrote: > > 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 > > What is the definition of list_node? Obviously it's a structure that has > fields > named data and next, but that's a little short. > > My guess is that you have to translate a linked list of nodes into a sequence > of nodes. Then you'd only need to do this: > }}} <eucode> > function sum_list_(integer sum,integer index,sequence nodes) > if index<=length(nodes) then > return sum_list_(sum+nodes[index][DATA],index+1,nodes) > else > return sum > end if > end function > > function sum_list(sequence nodes) > return sum_list_(0,1,nodes) > end function > </eucode> {{{ > > Not the most efficient, but the closest to a verbatim translation. > > CChris Hello CChris, Thank you for your response. My understanding is, and I might be wrong, that a list_node signifies the end of a list item or a line of text. In Euphoria a believe that this is normally character 13. A null node signifies the end of the list normally -1 in Euphoria. I got this from here. http://www-128.ibm.com/developerworks/linux/library/l-recurs.html Maybe if you read this you would understand it better than I. Also to Bernie Ryan thank you for your response Don Cole
5. Re: C Programers
- Posted by CChris <christian.cuvier at agr?cultur?.gouv.fr> Jul 23, 2007
- 520 views
don cole wrote: > > CChris wrote: > > > > 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 > > > > What is the definition of list_node? Obviously it's a structure that has > > fields > > named data and next, but that's a little short. > > > > My guess is that you have to translate a linked list of nodes into a > > sequence > > of nodes. Then you'd only need to do this: > > }}} <eucode> > > function sum_list_(integer sum,integer index,sequence nodes) > > if index<=length(nodes) then > > return sum_list_(sum+nodes[index][DATA],index+1,nodes) > > else > > return sum > > end if > > end function > > > > function sum_list(sequence nodes) > > return sum_list_(0,1,nodes) > > end function > > </eucode> {{{ > > > > Not the most efficient, but the closest to a verbatim translation. > > > > CChris > > Hello CChris, > > Thank you for your response. My understanding is, and I might be wrong, > that > a list_node signifies the end of a list item or a line of text. In Euphoria > a believe that this is normally character 13. A null node signifies the end > of the list normally -1 in Euphoria. I got this from here. > > <a > href="http://www-128.ibm.com/developerworks/linux/library/l-recurs.html">http://www-128.ibm.com/developerworks/linux/library/l-recurs.html</a> > > Maybe if you read this you would understand it better than I. > > Also to Bernie Ryan thank you for your response > > Don Cole Errrm not quite. My guess was right (I had read this paper ;) ). A node is some sort of blob of data. A list of these, at the RAM level (and C's too) is a collection of such blobs scattered in RAM, each of the blobs having a pointer to the next blob. This has nothing to do with text and line terminators. If your nodes were in RAM (foreign objects), another translation could be as follows:
function sum_list(atom node_ptr) if not node_ptr then return 0 else return peek4u(node_ptr+DATA)+sum_list(pddk4u(node_ptr+NEXT)) end function
where DATA us the offset in the RAM structure of the things to add, and NEXT the offset at which to find the address of the next node. If your objects are Euphoria sequences, then you don't have pointers - you got to emulate them through a sequence. In my previous reply, noe_ptr corresponds to nodes[index], and the next node is nodes[index+1]. HTH CChris
6. Re: C Programers
- Posted by Jason Gade <jaygade at ??hoo.com> Jul 23, 2007
- 526 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.
7. Re: C Programers
- Posted by Craig Welch <euphoriah at cwe?ch.or?> Jul 23, 2007
- 511 views
This just makes me glad I'm not a C programmer ... -- Craig
8. Re: C Programers
- Posted by don cole <doncole at pac?e?l.net> Jul 23, 2007
- 538 views
- Last edited Jul 24, 2007
Jason Gade wrote: > > 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. First off I would like to thank everyone for they're respone on this. Now I am more confused than ever. This is why I never pursued C very much. I could never get started. I have open Watcom on my cumputer but never use it except for the demo. But I believe I do understand recursion a little better. I think walk_dir is the best example for recursion of sequences. As for the fractorial example (integers only), I do understand that but I have never had the need to find the fractorial of a number. Don Cole
9. Re: C Programers
- Posted by Jason Gade <jaygade at yah?o?com> Jul 23, 2007
- 522 views
- Last edited Jul 24, 2007
don cole wrote: > > First off I would like to thank everyone for they're respone on this. > Now I am more confused than ever. This is why I never pursued C very much. I > could never get started. I have open Watcom on my cumputer but never use it > except for the demo. But I believe I do understand recursion a little better. > I think walk_dir is the best example for recursion of sequences. As for the > fractorial example (integers only), I do understand that but > I have never had the need to find the fractorial of a number. > > Don Cole Yeah, factorial is just one of those "Hello, World!" type of examples -- something simple to give you a feel for the idea. -- "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.