Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Recursion or loop, design questions Date: Sat, 18 Jul 2015 15:44:31 -0700 Organization: None to speak of Lines: 48 Message-ID: References: <5f394a20-64ec-4a15-9d2b-a8363fcbb248@googlegroups.com> <87oajce0uv.fsf@bsb.me.uk> <87mvyvc5a8.fsf@bsb.me.uk> <87wpxxby1o.fsf@bsb.me.uk> <644aa8d6-46f4-474f-a668-a5a8990f37e2@googlegroups.com> <87380l9pi7.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="12141"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194+ohNZzJDteZ+bFN6SX2z" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:rC+F2VoCgCdRtfGpKmg1GuY9dIw= sha1:6OGivOSRCKHTycv7nUpvc9FZLOo= Xref: csiph.com comp.lang.c:65869 Malcolm McLean writes: > On Saturday, July 18, 2015 at 11:01:12 PM UTC+1, Keith Thompson wrote: >> Malcolm McLean writes: >> > On Saturday, July 18, 2015 at 9:31:44 PM UTC+1, Ben Bacarisse wrote: >> >> Malcolm McLean writes: >> >> >> >> > However using a pointer is kind of cluntsy. So functional programming >> >> > language will let you do something along the lines of >> >> > >> >> > int countnodes(root) >> >> > { >> >> > if(root is null) return 0; >> >> > return sum(1, countnodes(root->left), countnodes(root->right) ); >> >> > } >> >> > >> >> > In C, that's not tail recursive, because countnodes must be called >> >> > before sum. >> >> >> >> That's not a tail call in any language. >> >> >> > >> > From Ben: >> > [defining tail recursion] >> > In a non-void function its a return statement whose expression is a >> > function call. In a void function it's an expression statement that >> > consists of nothing but a function call immediately preceding a return >> > statement. >> >> By that definition, the call to sum() is a tail call, but it's not tail >> recursive. The (recursive) calls to countnodes() are not tail calls. >> > In C, it isn't. What isn't what? > Because the two recursive calls must be made before > the call to sum(). But if we devise a language which calls sum, > the calls the arguments, then that objection disappears, it's > a case of two mutually recursive functions. You're suggesting a language in which a function is called before its arguments are evaluated? I fail to see how that could make sense. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"