From: eugene kim
Subject: my first lisp function... give me some comments plz..
Date: 
Message-ID: <aakfff$shl$1@newsreader.mailgate.org>
hi..
i just got started working on lisp..
fun recursive..!

here's my first lisp function..
it's prefix->infix converter(excercise problem)
even though it's very short program, and i wrote it myself..
it doesn't seem to look clear what it's doing 
as other language such as java does.

i'm wondering how people can read other people's lisp codes....
just matter of practice?

any comment/critique will be greatly appreciated
---------------------------
(defun infix (lst)
  (let ((secondAtom (second lst))
        (firstAtom (first lst)))
    (cond
     ( (> (length lst) 2) ( append (list
                                    (if (listp secondAtom)
                                        (infix secondAtom)
                                      secondAtom)
                                    )
                                   (list firstAtom)
                                   (infix (append (list firstAtom) (rest 
(rest lst))))))
     ( (= (length lst) 2) ( list
                            (if (listp secondAtom)
                                (infix secondAtom)
                              secondAtom)
                            ))
     )
    )
  )

--------------------------------------------
(infix '(+ (* 3 4) 2 ))
(setq tm '(+ (* 1 2 pi) 3 (- 4 5)))
(infix tm)

From: Matthieu Villeneuve
Subject: Re: my first lisp function... give me some comments plz..
Date: 
Message-ID: <3CCDC673.A0AD6CE@tumbleweed.com>
Two simple pieces of advice to make your program easier to read:
- follow the usual rules for indentation (very important)
- give meaningful names to variables and functions (if secondAtom can be
a list, its name is wrong)

(defun prefix->infix (list)
  (let ((second (second list))
        (first (first list)))
    (cond ((> (length list) 2) 
           (append (list (if (listp second)
                             (infix second)
                             second))
                   (list first)
                   (infix (append (list first)
                                  (rest (rest list))))))
          ((= (length list) 2)
           (list (if (listp second)
                     (infix second)
                     second))))))


--Matthieu


eugene kim wrote:
> 
> hi..
> i just got started working on lisp..
> fun recursive..!
> 
> here's my first lisp function..
> it's prefix->infix converter(excercise problem)
> even though it's very short program, and i wrote it myself..
> it doesn't seem to look clear what it's doing
> as other language such as java does.
> 
> i'm wondering how people can read other people's lisp codes....
> just matter of practice?
> 
> any comment/critique will be greatly appreciated
> ---------------------------
> (defun infix (lst)
>   (let ((secondAtom (second lst))
>         (firstAtom (first lst)))
>     (cond
>      ( (> (length lst) 2) ( append (list
>                                     (if (listp secondAtom)
>                                         (infix secondAtom)
>                                       secondAtom)
>                                     )
>                                    (list firstAtom)
>                                    (infix (append (list firstAtom) (rest
> (rest lst))))))
>      ( (= (length lst) 2) ( list
>                             (if (listp secondAtom)
>                                 (infix secondAtom)
>                               secondAtom)
>                             ))
>      )
>     )
>   )
> 
> --------------------------------------------
> (infix '(+ (* 3 4) 2 ))
> (setq tm '(+ (* 1 2 pi) 3 (- 4 5)))
> (infix tm)
From: Barry Margolin
Subject: Re: my first lisp function... give me some comments plz..
Date: 
Message-ID: <LUjz8.40$0j2.1455@paloalto-snr2.gtei.net>
In article <················@tumbleweed.com>,
Matthieu Villeneuve  <···················@tumbleweed.com> wrote:
>Two simple pieces of advice to make your program easier to read:
>- follow the usual rules for indentation (very important)
>- give meaningful names to variables and functions (if secondAtom can be
>a list, its name is wrong)
>
>(defun prefix->infix (list)
>  (let ((second (second list))
>        (first (first list)))
>    (cond ((> (length list) 2) 
>           (append (list (if (listp second)
>                             (infix second)
>                             second))
>                   (list first)
>                   (infix (append (list first)
>                                  (rest (rest list))))))
>          ((= (length list) 2)
>           (list (if (listp second)
>                     (infix second)
>                     second))))))

You forgot to change the function name from "infix" to "prefix->infix" in
the recursive calls.

This can be further simplified by allowing the function to take atoms, as
well as lists, so that the check for it being a list is done once, rather
than with the redundant "if" statements.

(defun prefix->infix (expression)
  (cond ((atom expression) expression)
        ((> (length expression) 2)
         (let ((operator (first expression))
               (first-arg (second expression))
               (rest-args (cddr expression)))
           `(,(prefix->infix first-arg)
             ,operator
             ,@(prefix->infix `(,operator ,@rest-args)))))
        ((= (length expression 2))
         (list (prefix->infix (second expression))))
        (t (error "Invalid expression: %S" expression))))

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: eugene kim
Subject: Re: my first lisp function... give me some comments plz..
Date: 
Message-ID: <aar822$5ip$1@newsreader.mailgate.org>
thank you all..

i thought it was time to get corrections before moving forward..
however i got too many corrections =)

i got questions to ask..

when i use defstruct..
and try to add a slot..
it's an error..
so i kill lisp (by kill -9 pid) and launch it again..

also, when a function got trapped in infinite recursion..
i also kill lisp and restart it..

is there any around for this?

thank you
From: Coby Beck
Subject: Re: my first lisp function... give me some comments plz..
Date: 
Message-ID: <HldA8.8306$GG6.617760@news3.calgary.shaw.ca>
eugene kim <··········@hotmail.com> wrote in message
·················@newsreader.mailgate.org...
> thank you all..
>
> i thought it was time to get corrections before moving forward..
> however i got too many corrections =)
>
> i got questions to ask..
>
> when i use defstruct..
> and try to add a slot..
> it's an error..
> so i kill lisp (by kill -9 pid) and launch it again..
>
> also, when a function got trapped in infinite recursion..
> i also kill lisp and restart it..

Both of these questions are dependant on which lisp implementation you are
using.  For the infinite loop problem (you said recursion, but that should
cause a stack overflow and break or crash on its own, so I think you might
mean loop) there is generally a way to send a break to your lisp process and
give you the chance to abort.

--
Coby Beck
(remove #\Space "coby 101 @ bigpond . com")
From: eugene kim
Subject: Re: my first lisp function... give me some comments plz..
Date: 
Message-ID: <aarpt5$ldh$1@newsreader.mailgate.org>
thank you..

i looked at memory state with top
and with infinite recursion.. it takes up all memory and starts to take up 
swap.. and at that point, system almost hangs..
after that observation, i kill the process asap.



sending quit signal (c-c c-\) does what i wanted to do..
i tried sending kill siginal.. i assumed "quit" would do the same thing..
From: Thomas A. Russ
Subject: Re: my first lisp function... give me some comments plz..
Date: 
Message-ID: <ymisn5aax0d.fsf@sevak.isi.edu>
eugene kim <··········@hotmail.com> writes:

> when i use defstruct..
> and try to add a slot..
> it's an error..
> so i kill lisp (by kill -9 pid) and launch it again..

You should be able to continue from the error.  Besides, most lisp
systems I've used will let you redefine defstructs.

> also, when a function got trapped in infinite recursion..
> i also kill lisp and restart it..
> 
> is there any around for this?

There should be.  It depends on which lisp you are using.  On Unix I
would normally try the Control-C key.  On the Macintosh I would try
Command-. key.


-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Kenny Tilton
Subject: Re: my first lisp function... give me some comments plz..
Date: 
Message-ID: <3CCDDF00.728E73E8@nyc.rr.com>
eugene kim wrote:
> 
> hi..
> i just got started working on lisp..
> fun recursive..!

mad cool that, eh?

> 
> here's my first lisp function..
> it's prefix->infix converter(excercise problem)
> even though it's very short program, and i wrote it myself..
> it doesn't seem to look clear what it's doing
> as other language such as java does.
> 
> i'm wondering how people can read other people's lisp codes....
> just matter of practice?

I got three observations:

(1) Is this version clearer?:

(defun infix2 (prefix-expr)
  (if (atom prefix-expr)
      prefix-expr
    (destructuring-bind (operator &rest operands)
        prefix-expr
      (list* (infix2 (first operands))
             (mapcan (lambda (operand)
                       (list operator
                             (infix2 operand)))
               (rest operands))))))

The novelty of Lisp (and recursion) to you might have been at fault for
the code being unclear, as well as...

(2) Consider this. if someone says out loud to you "take 3. add 6. take
the square root. subtract 6. add 3. what do you get?", you can do it in
your head. but if they read you a series of calculus transformations it
may be harder. Lisp is more powerful than Java, so...another analogy
might be speed-reading. Fine for Stephen King, for the Bard ya want to
slow down a hare.

(3) Well, turns out #3 is the sum of #1 and #2: yeah, I find Lisp hard
to read, but only because of unclear code or the fact that so much is
going on in just a little bit of code. I do have to slow down.

> 
> any comment/critique will be greatly appreciated
> ---------------------------
> (defun infix (lst)
>   (let ((secondAtom (second lst))
>         (firstAtom (first lst)))
>     (cond
>      ( (> (length lst) 2) ( append (list
>                                     (if (listp secondAtom)
>                                         (infix secondAtom)
>                                       secondAtom)
Moving the moral equivalent of these last few lines to the top of the
function enables it to handle atomic expressions as well as
(operator-operands) expressions. Now I can really use recursion: no
checking listp high and low, and no artificially consing up a list to
handle atomic operands (which list immediately gets tossed since its
length is two).

>                                     )
>                                    (list firstAtom)
>                                    (infix (append (list firstAtom) (rest

aside: instead of (append (list X) other-list) you can do (list* X
other-list):

   (list* firstAtom (rest (rest lst))) or (list* firstAtom (cddr lst)

... are a little less hairy. I do recommend getting comfy with the c*r
family.

Anyway, having to jam the operator back in for each operand is icky, and
should be taken as a Sign From God to find a Better Way. This is
recursion overkill. You are throwing away information (viz, that you are
looking at an operand) and instead consing the operator back to satisfy
the function infix (it needs a list as you have it) so you can call it
recursively. That, to repeat, should be taken as a clear sign that the
recursive function needs to be re-thunk.

Overall, tho, that's a decent first whack at recursion.

-- 

 kenny tilton
 clinisys, inc
 ---------------------------------------------------------------
"Harvey has overcome not only time and space but any objections."
                                                        Elwood P. Dowd
From: Thomas A. Russ
Subject: Re: my first lisp function... give me some comments plz..
Date: 
Message-ID: <ymir8kuawyx.fsf@sevak.isi.edu>
Kenny Tilton <·······@nyc.rr.com> writes:

> ... for the Bard ya want to slow down a hare.

I thought it was in Aesop that one wants to slow down a hare?

-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Kenny Tilton
Subject: Re: my first lisp function... give me some comments plz..
Date: 
Message-ID: <3CD335D8.BF9FC5A7@nyc.rr.com>
"Thomas A. Russ" wrote:
> 
> Kenny Tilton <·······@nyc.rr.com> writes:
> 
> > ... for the Bard ya want to slow down a hare.
> 
> I thought it was in Aesop that one wants to slow down a hare?

No need to, they give back their performance edge and more in down time. 

:)

-- 

 kenny tilton
 clinisys, inc
 ---------------------------------------------------------------
"Harvey has overcome not only time and space but any objections."
                                                        Elwood P. Dowd