Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Lisp Rocks

by japhy (Canon)
on May 23, 2001 at 22:42 UTC ( [id://82668]=perlmeditation: print w/replies, xml ) Need Help??

Whew. I just finished my first full-fledged Lisp program. Here's the last page or so. Mind telling me what you think?
) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )


japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re (tilly) 1: Lisp Rocks
by tilly (Archbishop) on May 23, 2001 at 23:04 UTC
    Observations.
    1. You have not picked up the Lisp parenthesizing style yet. Do so. It helps. (You let the parens pile up and let your editor pair them for you.)
    2. Your logic is nesting waaay too deep. Lisp, even more than Perl, is lots of little functions that call each other.
    3. To help the structuring of your program, think of your entire program as being the building of a model in which your problem is easily solved. And of course when you build the model you approach it the same way.
    4. Remember, most of Lisp is written in Lisp, and in any significant piece of Lisp code you expect to add customizations to the language for your needs. This again affects how much you need to nest it.
    BTW was it fun? :-)

      I basically agree with Tilly. Almost always parentheses should not appear on a line by themselves (don't line them up like you would line up {} in Perl, etc.). You just pile them up at the end of a line until you've closed off the expression that you're finishing (a decent editor will highlight the starting parens so that you know when you're done). Then just let the editor do the indenting and you should have reasonably well formatted code. You probably do need to break things down more - I don't think I have anything with more than about 7 or 8 closing parens in my Lisp code, and even that is pretty rare. (Disclaimer: I don't necessarily want to present my Lisp code as being particularly good. It does its job, though. :-)

      For a book with lots of good example (Common) Lisp code, see Paradigms of Artificial Intelligence Programming by Norvig (which is of interest to anyone programming in Common Lisp, even those with no interest in AI). Paul Graham's books are pretty good too, but some of the code is considerably more abstruse.

      <SNL who="chris farley">Why, THANK YOU Pepper Boy! That was JUST the right amount of pepper I needed!</SNL>

      japhy -- Perl and Regex Hacker
Re: Lisp Rocks
by JSchmitz (Canon) on May 23, 2001 at 23:45 UTC
    Nice - however I would go at it from another angle here is what I mean:
    (lotso fodder (fn lst) (let ((acc nil)) (do ((Sierra Nevada(cdr src))) ((or (null src) (listen to Sonic Youth fn (car src))) (values (ride Trek acc) src)) (push (car src) acc)))) (defun most (fn lst) (if (null lst) (use perl) (let* ((wins (car lst)) (max (Neve Campbell growl! wins))) (dolist (obj (cdr lst)) (let ((drunk always (funcall fn obj))) (when (> score max) (setq wins obj max score)))) (Lisp is an atrocity))))
Re: Lisp Rocks
by princepawn (Parson) on May 23, 2001 at 23:01 UTC
    Larry Wall said in Issue #1 of the Perl Journal that Lisp looks like oatmeal with fingernail clippings thrown in.
Re: Lisp Rocks
by tinman (Curate) on May 23, 2001 at 23:06 UTC

    I have no idea what the actual application is, what it does or the situation in hand... :o)

    with that disclaimer in mind, perhaps you should review your indentation and coding model :o)
    I've found several examples of lisp code on the Net (sadly, I am more familiar with Prolog than Lisp) which look pretty normal.. I know I'd be a bit taken aback if I saw a pageful of parantheses :o) (yes, yes, I know.. get an editor that can match parentheses)

    Some links to Lisp code and coding style(if anyone is interested)
    AutoCad routines
    Lisp coding style
    has some stuff on Lisp style indenting

    just my <$0.02

Re: Lisp Rocks
by Dominus (Parson) on May 24, 2001 at 10:50 UTC
      Come on, give japhy a break. He has an excuse for being a bit behind the times.

      After all he wasn't even born yet in 1977...

Re: Lisp Rocks
by davorg (Chancellor) on May 24, 2001 at 11:55 UTC

    Someone once told me that Lisp could use ']' to mean "close all currently open parentheses". They may well have been winding me up but, as your code demonstrates, it should be true :)

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      This isn't true of (ANSI) Common Lisp or Scheme, but some editors will close the current expression if you press ]

      Update: Corrected in response to followup.

Re: Lisp Rocks
by shotgunefx (Parson) on May 24, 2001 at 04:46 UTC
    My exposure to Lisp has been almost nil (no pun intended). I started researching it but it just wasn't a fit for my mindset but there seems to be some interesting possibilities for it's use.

    An good article about using Lisp for Web applications can be found at Paul Graham's web site. I found this a very interesting read.

    -Lee

    "To be civilized is to deny one's nature."
Re: Lisp Rocks
by BigGuy (Friar) on May 25, 2001 at 07:57 UTC
    It's kind of funny that you post this now I am taking
    comparitive programming languages right now and we are covering lisp
    you have piqued my curiosity now i would like to see the
    program now here is the very fisrt lisp program i have written actually it is not even that
    just a little function
    (defun multi-negative(alist) (cond ((null alist) 1); null list return nil ((and (numberp(car alist)) (< (car alist) 0) (* (multi-negative (cdr alist)) (car alist)))) (t (multi-negative(cdr alist))) );cond );defun

    BigGuy "One World, one Web, one Program" - Microsoft promotional ad
    "Ein Volk, ein Reich, ein Fuhrer" - Adolf Hitler

      Just some (hopefully) constructive criticism. If I understand correctly, this is supposed to take a list of numbers and multiply only the negative ones together (or return 1 if there are none)? (Which to be fair, it does.) The intent is a little obscured - the second clause in the cond could be more clearly written - as is, you have the result you want as the third condition of the "and" rather than the result that is returned when the "and" clause tests to true - since "and" returns the value of the last argument if all of them are true, and since a cond clause returns the value of the test if it is true but no result clause is specified, this works, but is unclear. Also, the first comment is misleading. You might want to avoid using the name "alist" here - an alist is a specific type of data structure that Lisp programmers will probably expect to see here if you use that name (that threw me off a little at first!). You can just say "list". Finally, Lisp style generally tells us to avoid having parentheses alone on lines. A cleaned up version might be (oh, I slipped minusp in there too :-):

      (defun multi-negative (list) "Return the product of the negative numbers in list." (cond ((null list) 1) ((and (numberp (car list)) (minusp (car list))) (* (multi-negative (cdr list)) (car list))) (t (multi-negative (cdr list)))))

      There are lots of other fun ways to write this in Lisp, too. Have fun discovering them!

      Update: Added a little clarification w.r.t. the "and" stuff. Also, it'd be better to use realp than numberp, as minusp will choke on a complex number.

Re: Lisp Rocks
by thabenksta (Pilgrim) on May 24, 2001 at 18:28 UTC

    I wonder how long it actually took him to tab all that out?

    -thabenksta
    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
      If he was using a decent editor, no time at all...
      C-SPC M-> C-M-\
      Go emacs!
      hmmm, since my name starts with vowel I think my nickname should go like this:

      my $name = 'Adam Russell';
      $name=~s/^(.+)\s(.).+$/\L$1$2/g;
      my $nick = 'tha' . $name . 'sta';
      $nick=~s/tha(aeiou)/th\'$1/;
Re: Lisp Rocks
by jorg (Friar) on May 24, 2001 at 04:17 UTC
    From the way that last page looks, i think i might be able to create lisp proggies with a l33t ASCII text generator ((:

    Jorg

    "Do or do not, there is no try" -- Yoda
REPL-based development Rocks
by princepawn (Parson) on Jan 23, 2008 at 15:16 UTC
    (defun remove-current-ac (s) (regex-replace (myre "current \\s* a/c") s "")) ; (remove-current-ac "BANCO SANTANDER S.A. CURRENT A/C")
    Here I have my function, which I can add to the interpreter in Emacs with a single key sequence. Then I just move down to my test line and try it out.

    Lightning fast development and turnaround are the big wins with a Lisp syntax language.


    Ivan Raikov says: the first step to understanding recursion is to begin by understanding recursion.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://82668]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2024-04-19 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found