From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,97188312486d4578 X-Google-Attributes: gidfc89c,public X-Google-Thread: 103376,97188312486d4578 X-Google-Attributes: gid103376,public X-Google-Thread: 1014db,6154de2e240de72a X-Google-Attributes: gid1014db,public X-Google-Thread: 109fba,baaf5f793d03d420 X-Google-Attributes: gid109fba,public From: mdw@excessus.demon.co.uk (Mark Wooding) Subject: Re: What's the best language to start with? [was: Re: Should I learn C or Pascal?] Date: 1996/08/12 Message-ID: <4uo4b8$ug0@excessus.demon.co.uk>#1/1 X-Deja-AN: 173782664 x-nntp-posting-host: excessus.demon.co.uk references: <31FBC584.4188@ivic.qc.ca> <01bb83ad$29c3cfa0$87ee6fce@timpent.airshields.com> <4u89c4$p7p@solutions.solon.com> <01bb83f5$923391e0$87ee6fce@timpent.airshields.com> <4uah1k$b2o@solutions.solon.com> <01bb853b$ca4c8e00$87ee6fce@timpent.airshields.com> <320b35a2.43769707@nntp.ix.netcom.com> <01bb8609$59339140$87ee6fce@timpent.airshields.com> <320bf032.7193774@nntp.ix.netcom.com> <01bb8802$a5ff3a60$32ee6fce@timhome2> <320f14e5.213196860@nntp.ix.netcom.com> organization: Straylight Development Lab reply-to: mdw@excessus.demon.co.uk newsgroups: comp.lang.c,comp.lang.c++,comp.unix.programmer,comp.lang.ada Date: 1996-08-12T00:00:00+00:00 List-Id: Mike Rubenstein wrote: > "Tim Behrendsen" wrote: > > > I'm not sure if that's good or not. One of the famous ways to > > show recursion is a factorial computation. Does that mean we > > actually *want* people to implement factorial that way? > > In scheme (and some other languages) it certainly is the way to > implement it. Are we talking about implementing it the truly stupid way as a single function, e.g., (define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1))))) or properly using another function which tail-recurses, e.g., (define (factorial n) (fact-assist 1 n)) (define (fact-assist a n) (if (zero? n) a (fact-assist (* a n) (- n 1)))) If the former, then please report for re-education; if the latter, then well and good: tail recursion is the functional-language equivalent of a loop in a language like C. The first version above is similar in style (and in the sorts of problems it causes) to the traditional int factorial(int n) { return (n ? n*factorial(n-1) : 1); } recursive C implementation, while the second is similar in spirit to the iterative version. (My Scheme isn't terribly good: please don't moan at me about it if it's wrong or nasty. Also, all of the routines above assume they get valid inputs, and will do really bad things if they don't: issues of input checking aren't really relevant to the discussion in hand.) -- [mdw] `When our backs are against the wall, we shall turn and fight.' -- John Major