·········@gmail.com wrote:
> i have just wrote a pascal's triangle program , since i'm a newbie to
> lisp, i dont know whether what i have written is an effective solution.
Effective as in what? Having effect? What kind of effect are you
looking for?
Your program does have the intended effect of printing out the first
several
rows of Pascal's Triangle.
Moreover, it looks pretty, provided that the numbers don't grow beyond
three digits.
> (defun pasc (row col)
> (cond ((or (< col 1) (< row 0)) 0)
> ((= col 1) 1)
> ((= col row) 1)
> (t (+ (pasc (- row 1) (- col 1)) (pasc (- row 1) col)))))
Ouch! You are doing full recursion all the way to the top of the
triangle in order to compute each cell in the triangle.
Most programs that print the triangle keep a buffer so the next row can
be computed in the straightforward way from the previous row.
Memoization of the pasc function will do effectively the same thing. It
means that once you compute (pasc x y) for some X and Y, the result is
not only returned but also cached in a storage object associated with
the function. The next time (pasc x y) is called with the same X and Y,
it will retrieve the previously computed result from the cache.
There is at least one memoization packages for Lisp which simply lets
you annotate a function as memoized. The nice thing about this is that
you don't have to change the structure of your program at all.
Kaz Kylheku wrote:
> ·········@gmail.com wrote:
> > i have just wrote a pascal's triangle program , since i'm a newbie to
> > lisp, i dont know whether what i have written is an effective solution.
>
> Effective as in what? Having effect? What kind of effect are you
> looking for?
>
> Your program does have the intended effect of printing out the first
> several
> rows of Pascal's Triangle.
>
> Moreover, it looks pretty, provided that the numbers don't grow beyond
> three digits.
>
> > (defun pasc (row col)
> > (cond ((or (< col 1) (< row 0)) 0)
> > ((= col 1) 1)
> > ((= col row) 1)
> > (t (+ (pasc (- row 1) (- col 1)) (pasc (- row 1) col)))))
>
> Ouch! You are doing full recursion all the way to the top of the
> triangle in order to compute each cell in the triangle.
>
yes , u are right, can i print the number along when i'm finding the
values, .. within the recursion, (instead of caching)
·········@gmail.com wrote:
> i have just wrote a pascal's triangle program , since i'm a newbie to
> lisp, i dont know whether what i have written is an effective solution.
> can any one check this out... thankx in advance
>
>
> (defun pascal (rows)
> (do ((i 1 (1+ i))) ((> i rows))
> (do ((sp (- rows i) (1- sp))) ((= 0 sp)) (format t " "))
> (do ((j 1 (1+ j))) ((> j i))
> (format t "~3a " (pasc i j)))
> (format t "~%")))
>
>
> (defun pasc (row col)
> (cond ((or (< col 1) (< row 0)) 0)
> ((= col 1) 1)
> ((= col row) 1)
> (t (+ (pasc (- row 1) (- col 1)) (pasc (- row 1) col)))))
>
>
How about this?
(defun pascal (rows)
(loop repeat rows for line = '(1)
then (maplist #'(lambda (cons) (+ (first cons)
(or (second cons) 0))) (cons 0 line))
do (format t "~A~{~6D~}~%"
(make-string (* 3 (- rows (length line)))
:initial-element #\Space) line)))
Sorry about the indentation, it's really only supposed to be 4 lines
but this dang Google Groups message editor won't support the longer
lines (they're not THAT long!). The "~6D" and "(* 3" above allow the
function to display a properly formatted triangle up to 20 rows. If you
change "~6D" to handle displaying the bigger numbers required in more
rows than this, make sure to change the "(* 3" to half the width you
choose. This could probably be handled programatically, but the code
would be uglier.
Geoffrey Summerhayes wrote:
> (format t "~&~VT~{~6D~}~%" (* 3 (- rows (length line))) line)
>
Thanks, I've never been too great with format. However, I'm not sure
why but this version offsets the last row by one space, as opposed to
the version with make-string.
/goes digging into the format spec once again...
prabuinet wrote:
> Geoffrey Summerhayes wrote:
>
>>Geoffrey Summerhayes wrote:
>>
>>>John Stoneham wrote:
>>>
>>>>How about this?
>>>>
>>>>(defun pascal (rows)
>>>> (loop repeat rows for line = '(1)
>>>> then (maplist #'(lambda (cons) (+ (first cons)
>>>> (or (second cons) 0))) (cons 0 line))
>>>> do (format t "~A~{~6D~}~%"
>>>> (make-string (* 3 (- rows (length line)))
>>>> :initial-element #\Space) line)))
>>>>
>>>
>>> (format t "~&~VT~{~6D~}~%" (* 3 (- rows (length line))) line)
>>
>>*SIGH* just can't resist monkeying with code. Longer though :-(
>>
>>(defun pascal (rows &optional (stream *standard-output*))
>> (loop repeat rows
>> for leading-spaces downfrom (* 3 (- rows 1)) by 3
>> for line = '(1)
>> then (maplist #'(lambda (cons)
>> (+ (first cons) (or (second cons) 0)))
>> (cons 0 line))
>> do (format stream "~&~VT~{~6D~}~%" leading-spaces line)))
>>
>>----
>>Geoff
>
>
>
>
>
> I dont know how you people were able to think like this,
I have the same feeling when I look at my own code. It does not spring
whole from my mind, it evolves as I react to compiler errors, bugs, and
unforeseen cases. Often in the end I look at the working result and only
then see how it can be collapsed into more functional form to make it
more maintainable and concise.
When I look at it a month or day later, having forgotten the process by
which it arose, I wonder where it came from.
And in this case of Pascal's triangle, there is likely some "showing
off" going on.
> I really
> wonder how long you will be programming in LISP
>
having read your later clarification of this to mean "how long they have
been programming in Lisp", there is that, too. :)
kt
--
Cells: http://common-lisp.net/project/cells/
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
Ken Tilton <·········@gmail.com> wrote [in a different order]:
+---------------
| > I dont know how you people were able to think like this,
|
| I have the same feeling when I look at my own code. ...
| When I look at it a month or day later, having forgotten the process
| by which it arose, I wonder where it came from.
+---------------
The same thing happens to me, but too often it's "I wonder
where the ·@·····@ this awful mess came from??!?!" ;-} ;-}
+---------------
| Often in the end I look at the working result and only then
| see how it can be collapsed into more functional form to make it
| more maintainable and concise.
+---------------
In my experience, that getting-it-working-first and *then*
[possibly setting it aside for a while and then] re-reading it
is absolutely key to the creation of code which is elegant,
robust, and long-term maintainable. Without such re-reading
and "editing" [in the sense of editing a book for grammar
and style], the original hacks and misunderstanding remain,
and further hacks [as the code evolves] just pile up and the
code becomes less & less readable... and thus less & less
maintainable in the long run.
One of the "problems" of those higher-level languages [such
as Lisp!] which encourage correct programming the first time
is that this subsequent re-reading and editing never gets done.
C.f. "Warnock's Rule for Why BLISS Programs Are Big", circa 1971
or so [commonly voiced around the DECsystem-10 SIGIL meetings].
Hint: They tended to work the first time, so they didn't get
re-read (and thus didn't get re-written).
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
Le Thu, 17 Aug 2006 05:42:39 -0700, prabuinet a écrit :
> I dont know how you people were able to think like this, I really
> wonder how long you will be programming in LISP
Probably for my lifetime (and hopefully), as far as I'm concerned. The
fact that I'm my own boss and roll my own systems for my customers helps
a lot. (that is, my current customer won't ever see I've switched from
C++ to Lisp if I don't tell him, and doesn't care)
Happily,
Nowhere man
--
···········@levallois.eu.org
OpenPGP 0xD9D50D8A
Pierre THIERRY wrote:
> Le Thu, 17 Aug 2006 05:42:39 -0700, prabuinet a écrit :
> > I dont know how you people were able to think like this, I really
> > wonder how long you will be programming in LISP
>
> Probably for my lifetime (and hopefully), as far as I'm concerned. The
> fact that I'm my own boss and roll my own systems for my customers helps
> a lot. (that is, my current customer won't ever see I've switched from
> C++ to Lisp if I don't tell him, and doesn't care)
>
> Happily,
> Nowhere man
> --
> ···········@levallois.eu.org
> OpenPGP 0xD9D50D8A
sorry, for my bad english, actually i tried to ask, how long u are
using lisp?
Le Thu, 17 Aug 2006 06:25:23 -0700, prabuinet a écrit :
> sorry, for my bad english, actually i tried to ask, how long u are
> using lisp?
Two months, actually! ;-)
Recently,
Nowhere man
--
···········@levallois.eu.org
OpenPGP 0xD9D50D8A