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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 107f24,626a0a064b320310 X-Google-Attributes: gid107f24,public X-Google-Thread: 10259a,626a0a064b320310 X-Google-Attributes: gid10259a,public X-Google-Thread: f4fd2,626a0a064b320310 X-Google-Attributes: gidf4fd2,public X-Google-Thread: 103376,ea8ea502d35ca2ce X-Google-Attributes: gid103376,public X-Google-Thread: 114809,626a0a064b320310 X-Google-Attributes: gid114809,public X-Google-Thread: 1164ba,626a0a064b320310 X-Google-Attributes: gid1164ba,public X-Google-Thread: 103d24,626a0a064b320310 X-Google-Attributes: gid103d24,public X-Google-ArrivalTime: 2001-05-14 13:33:40 PST Path: archiver1.sj.google.com!newsfeed.google.com!sn-xit-02!supernews.com!isdnet!freenix!fr.usenet-edu.net!usenet-edu.net!newsfeeds.belnet.be!news.belnet.be!news2.euro.net!uunet!ash.uu.net!nntphub.cb.lucent.com!news.research.bell-labs.com!news From: Matthias Blume Newsgroups: comp.lang.ada,comp.lang.lisp,comp.lang.smalltalk,comp.lang.basic,comp.lang.functional,comp.lang.scheme,comp.lang.perl Subject: Re: Beginner's Language? Date: Mon, 14 May 2001 16:14:26 -0400 Organization: Lucent Technologies, Bell Labs Message-ID: <3B003CA2.D24D083C@research.bell-labs.com> References: <9cukad$nn68@news-dxb> <9d6b6e$1bt$1@nh.pace.co.uk> <87snihxiwc.fsf@frown.here> <9dbi83$sji$1@nh.pace.co.uk> <87heyu7cqd.fsf@frown.here> <9dc20p$hh15e$1@ID-37382.news.dfncis.de> NNTP-Posting-Host: blume-pcmh.research.bell-labs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.75 [en] (X11; U; Linux 2.2.16-3 i686) X-Accept-Language: en, de, ja Xref: archiver1.sj.google.com comp.lang.ada:7498 comp.lang.lisp:10037 comp.lang.smalltalk:9668 comp.lang.functional:5638 comp.lang.scheme:3796 comp.lang.perl:2737 Date: 2001-05-14T16:14:26-04:00 List-Id: Johan Kullstam wrote: > > a.krennmair@aon.at (Andreas Krennmair) writes: > > > Friedrich Dominicus wrote: > > > "Marin David Condic" writes: > > > > > > > > If you are familiar with Lisp, try this: Write a small program to read in a > > > > couple of numbers from a keyboard, do some math with them and print the > > > > result to the screen. > > > Gosh, how much simpler as in Lisp can it be? No declarations, no > > > puzzling about counting probably etc etc. > > The problem is that Lisp is in no way similar to a natural language. And > > natural language can be understood easier than some functional, theoretical > > stuff with a unique concept like Lisp. Don't get me wrong, Lisp is a great > > language, and I personally find it quite exciting as four year Pascal and > > two year C/C++ programmer, but it's hardly usable for teaching, because it > > is so unique (well, you _could_ to functional programming in C, but it would > > be considered bad style). > > in C, functions are not first class objects; you cannot do functional > programming in C. false and (in some sense) true (Functions _are_ first-class objects in C; and you cannot do _elegant_ functional programming in C.) > > how do i take a function of two args in C and pass it to a function > which expects a funciton of only one arg? What does this have to do with first-class-ness and/or functional programming? In ML or Haskell, I cannot take a function of two arguments and pass it to a function that expects a function of only one argument. > (defun foo (x y) (...)) > > (bar #'(lambda (u) (foo u 4))) > > in C, this can be kluged by using a global variable and a named helper > function which accesses the global and calls foo. it's very ugly and > not re-entrant. Oh, I see. You are talking about closures and partial application. In C this can be (and often has been) done using explicit closure passing style. See your favorite X windows toolkit for plentiful examples. (I didn't say it was pretty.) To go back to ML or Haskell (or Lisp or Scheme): What you are doing is not "passing of a function of two args to a place that expects a function of one arg", you are actually passing a function that expects one arg (and which happens to call the original function of two args). > example 2 > > how do i have a function *create* and return a *function*? > > (defun power-function (power) > #'(lambda (x) (expt x power))) > > now use it to get > > lisp> (mapcar (power-function 3) '(1 2 3)) > (1 8 27) > > afaik this cannot be done in C at all. Can be done. Same trick, explicit closure passing. (Of course, you won't be passing "just a function" then in C.) Matthias