Re: C Programers
- Posted by CChris <christian.cuvier at agr?cultur?.gouv.fr> Jul 23, 2007
- 519 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