I have a problem. I'm a computer science student and my teacher of
artificial intellicence wants a program wich:
you give a number (n) and the program gives you the n-first
rows of the tartaglia triangle. I have been working about it, but
clisp and me aren't good friends.
Thanks and sorry for my bad english (i'm catalonian ).
From: Martti Halminen
Subject: Re: a simple question about tartaglia triangle
Date:
Message-ID: <38D248BE.76779542@solibri.com>
Josep Serra Gallego wrote:
> I have a problem. I'm a computer science student and my teacher of
> artificial intellicence wants a program wich:
> you give a number (n) and the program gives you the n-first
> rows of the tartaglia triangle. I have been working about it, but
> clisp and me aren't good friends.
> Thanks and sorry for my bad english (i'm catalonian ).
What was the question?
As your purpose hopefully is learning something, getting
somebody else to do all your homework isn't the best method.
How about posting what you have so far, so that we could see
where you have the problem?
Might help some, if you also told us what a "tartaglia triangle"
is...
--
From: Fernando D. Mato Mira
Subject: Re: a simple question about tartaglia triangle
Date:
Message-ID: <38D253D6.B7FD0753@iname.com>
Martti Halminen wrote:
> Might help some, if you also told us what a "tartaglia triangle"
> is...
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
...
--
Fernando D. Mato Mira
Real-Time SW Eng & Networking
Advanced Systems Engineering Division
CSEM
Jaquet-Droz 1 email: matomira AT acm DOT org
CH-2007 Neuchatel tel: +41 (32) 720-5157
Switzerland FAX: +41 (32) 720-5720
www.csem.ch www.vrai.com ligwww.epfl.ch/matomira.html
In Germany we call it Pascal's Triangle!
The k-th entry in the n-th row is n over k that is n!/(k!(n-k)!. I hope I
can tell him so much as this has nothing to do with programming.
Now it ist not clear if formatting of the output ist required.
Janos Blazi
Fernando D. Mato Mira <········@iname.com> schrieb in im Newsbeitrag:
·················@iname.com...
> Martti Halminen wrote:
>
> > Might help some, if you also told us what a "tartaglia triangle"
> > is...
>
> 1
> 1 1
> 1 2 1
> 1 3 3 1
> 1 4 6 4 1
> ...
>
> --
> Fernando D. Mato Mira
> Real-Time SW Eng & Networking
> Advanced Systems Engineering Division
> CSEM
> Jaquet-Droz 1 email: matomira AT acm DOT org
> CH-2007 Neuchatel tel: +41 (32) 720-5157
> Switzerland FAX: +41 (32) 720-5720
>
> www.csem.ch www.vrai.com ligwww.epfl.ch/matomira.html
>
>
>
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
From: Fernando D. Mato Mira
Subject: Re: a simple question about tartaglia triangle
Date:
Message-ID: <38D2721E.86B66A6B@iname.com>
Janos Blazi wrote:
> In Germany we call it Pascal's Triangle!
In China it's called Yang Hui's triangle. He was first.
--
Fernando D. Mato Mira
Real-Time SW Eng & Networking
Advanced Systems Engineering Division
CSEM
Jaquet-Droz 1 email: matomira AT acm DOT org
CH-2007 Neuchatel tel: +41 (32) 720-5157
Switzerland FAX: +41 (32) 720-5720
www.csem.ch www.vrai.com ligwww.epfl.ch/matomira.html
On Fri, 17 Mar 2000 16:48:38 +0100, Fernando D. Mato Mira <········@iname.com> wrote:
>Martti Halminen wrote:
>
>> Might help some, if you also told us what a "tartaglia triangle"
>> is...
>
> 1
> 1 1
> 1 2 1
> 1 3 3 1
> 1 4 6 4 1
> ...
>
I have always heard this refered to as Pascal's Triangle... has Pascal
lost this honor?
--
Kenneth P. Turvey <······@SprocketShop.com>
--------------------------------------------
It is not only [the juror's] right, but his duty, in that case, to
find the verdict according to his own best understanding, judgment, and
conscience, though in direct opposition to the direction of the court.
-- John Adams
From: Fernando D. Mato Mira
Subject: Re: a simple question about tartaglia triangle
Date:
Message-ID: <38D51B66.8DECFBE@iname.com>
"Kenneth P. Turvey" wrote:
> On Fri, 17 Mar 2000 16:48:38 +0100, Fernando D. Mato Mira <········@iname.com> wrote:
> >Martti Halminen wrote:
> >
> >> Might help some, if you also told us what a "tartaglia triangle"
> >> is...
> >
> > 1
> > 1 1
> > 1 2 1
> > 1 3 3 1
> > 1 4 6 4 1
> > ...
> >
>
> I have always heard this refered to as Pascal's Triangle... has Pascal
> lost this honor?
Probably the name stuck via Newton in some countries.
--
Fernando D. Mato Mira
Real-Time SW Eng & Networking
Advanced Systems Engineering Division
CSEM
Jaquet-Droz 1 email: matomira AT acm DOT org
CH-2007 Neuchatel tel: +41 (32) 720-5157
Switzerland FAX: +41 (32) 720-5720
www.csem.ch www.vrai.com ligwww.epfl.ch/matomira.html
Josep Serra Gallego <············@campus.uab.es> writes:
> Thanks and sorry for my bad english (i'm catalonian ).
Your English is excellent.
However, you seem to forgotten to tell us what your problem is.
It would be best if you posted your code as is, this would give us the
best idea of how far you have come.
Stig Hemmer,
Jack of a Few Trades.
From: Josep Serra Gallego
Subject: Re: a simple question about tartaglia triangle
Date:
Message-ID: <38D4101F.22D6B844@campus.uab.es>
Hello
It's true that my question wasn't very clear. I'll try to do better
now:
I'm doing a little program wich you give one number (n) and it returns
to you all the rows of the tartaglia triangle (or pascal triangle)
since the row number (n). example:
> (tartaglia 5)
>((1)(1 1)(1 2 1)(1 3 3 1)(1 4 6 4 1))
I've been working in something like this:
this function generates the next row of the tartaglia triangle:
( defun tarta( a L )
( if ( = ( length L ) 1 )
'( 1 )
( if ( = ( length L ) a )
( append '(1)
( cons ( + ( car L ) ( cadr L ) )
( tarta a ( cdr L ) ) ) )
( cons ( + ( car L ) ( cadr L ) )
( tarta a ( cdr L ) ) ) ) ) )
and this function generates the n-rows:
(defun ntar( N )
(if ( equal 1 N )
( cons 1 1 )
( cons ( tar ( car ( ntar ( - N 1 ) ) ) )
( ntar ( - N 1 ) ) )
))
but it doesn't work. (What I'm doing bad?)
** In general: For each function you define, try to explain exactly
** what you want this function to do. Give examples. Try them out.
** Look at the simplest example that doesn't do what you want it to
** do. Find out why by going over the function by hand. If you are
** still stuck, return here.
I've taken the liberty of reformatting your code, to make it more
readable to most people on this newsgroup.
Josep Serra Gallego <············@campus.uab.es> writes:
> this function generates the next row of the tartaglia triangle:
Given what input?
> (defun tarta (a L)
> (if (= (length L) 1)
> '(1)
> (if (= (length L) a)
> (append '(1)
> (cons (+ (car L) (cadr L))
> (tarta a (cdr L))))
> (cons (+ (car L) (cadr L))
> (tarta a (cdr L))))))
By trying various parameters I find that:
: (tarta 2 '(1 1))
(1 2 1)
: (tarta 3 '(1 2 1))
(1 3 3 1)
Does this match what you wanted the function to do?
If so, I guess
: (tarta 1 '(1))
(1)
really should have returned (1 1).
Can you seen what happens here?
> and this function generates the n-rows:
> (defun ntar (N)
> (if (equal 1 N)
> (cons 1 1)
> (cons (TARTA (car (ntar (- N 1))))
> (ntar (- N 1)))))
You never defined a function TAR, and you never used the function
TARTA, so I assume TAR was just a mistake for TARTA.
The exercise asked for you to define a function TARTAGLIA. Your code
has no such function. I assume your NTAR is supposed to be it?
This function was supposed to return a list of lists, right?
Like e.g. ((1) (1 1) (1 2 1))
Now, the simplest case, (ntar 1), doesn't do that, it returns (cons 1
1) which isn't a list at all. What were you thinking here?
Also, if NTAR is supposed to be TARTAGLIA, (car (ntar ...)) should
always return (1), so this is probably not what you meant.
By the way, instead of CAR, CADR and CDR you could use FIRST, SECOND
and REST. Even if English isn't your native language, this should be
easier to read. (Your English is quite good, you know)
Stig Hemmer,
Jack of a Few Trades.
From: Johan Kullstam
Subject: Re: a simple question about tartaglia triangle
Date:
Message-ID: <m2u2i262e3.fsf@euler.axel.nom>
Stig Hemmer <····@gnoll.pvv.ntnu.no> writes:
> ** In general: For each function you define, try to explain exactly
> ** what you want this function to do. Give examples. Try them out.
> ** Look at the simplest example that doesn't do what you want it to
> ** do. Find out why by going over the function by hand. If you are
> ** still stuck, return here.
>
> I've taken the liberty of reformatting your code, to make it more
> readable to most people on this newsgroup.
>
> Josep Serra Gallego <············@campus.uab.es> writes:
> > this function generates the next row of the tartaglia triangle:
>
> Given what input?
>
> > (defun tarta (a L)
> > (if (= (length L) 1)
> > '(1)
> > (if (= (length L) a)
> > (append '(1)
> > (cons (+ (car L) (cadr L))
> > (tarta a (cdr L))))
> > (cons (+ (car L) (cadr L))
> > (tarta a (cdr L))))))
>
> By trying various parameters I find that:
> : (tarta 2 '(1 1))
> (1 2 1)
> : (tarta 3 '(1 2 1))
> (1 3 3 1)
>
> Does this match what you wanted the function to do?
>
> If so, I guess
> : (tarta 1 '(1))
> (1)
> really should have returned (1 1).
> Can you seen what happens here?
>
> > and this function generates the n-rows:
>
> > (defun ntar (N)
> > (if (equal 1 N)
> > (cons 1 1)
> > (cons (TARTA (car (ntar (- N 1))))
> > (ntar (- N 1)))))
>
> You never defined a function TAR, and you never used the function
> TARTA, so I assume TAR was just a mistake for TARTA.
>
> The exercise asked for you to define a function TARTAGLIA. Your code
> has no such function. I assume your NTAR is supposed to be it?
>
> This function was supposed to return a list of lists, right?
> Like e.g. ((1) (1 1) (1 2 1))
>
> Now, the simplest case, (ntar 1), doesn't do that, it returns (cons 1
> 1) which isn't a list at all. What were you thinking here?
>
> Also, if NTAR is supposed to be TARTAGLIA, (car (ntar ...)) should
> always return (1), so this is probably not what you meant.
>
> By the way, instead of CAR, CADR and CDR you could use FIRST, SECOND
> and REST. Even if English isn't your native language, this should be
> easier to read. (Your English is quite good, you know)
FIRST SECOND &c are good. ELT or NTH might be pretty good too
depending upon your style.
you might want to try MAPCAR and #'+.
check this out
USER(1): (mapcar #'+ '(1 2 1) (cdr '(1 2 1)))
(3 3)
USER(2): (mapcar #'+ '(1 3 3 1) (cdr '(1 3 3 1)))
(4 6 4)
now all you need to do is to paste 1's to the front and back and apply
a little looping or recursion.
hope this helps.
--
J o h a n K u l l s t a m
[········@ne.mediaone.net]
Don't Fear the Penguin!
From: Josep Serra Gallego
Subject: Re: a simple question about tartaglia triangle
Date:
Message-ID: <38D5589F.F1F8C06F@campus.uab.es>
Josep Serra Gallego escribi�:
** In general: For each function you define, try to explain exactly
** what you want this function to do. Give examples. Try them out.
** Look at the simplest example that doesn't do what you want it to
** do. Find out why by going over the function by hand. If you are
** still stuck, return here.
Yes, it's true. (It's the first time I write something in a news group.)
then... :
> > Given what input?
You are right. The input I give is the number of the row and the row.
The output is the next row.
>
> > > (defun tarta (a L)
> ...
> > > (tarta a (cdr L))))))
> >
> > By trying various parameters I find that:
> > : (tarta 3 '(1 2 1))
> > (1 3 3 1)
> >
> > Does this match what you wanted the function to do?
I want the function give me (1 3 3 1) if I give the input (1 2 1).
Example :
(tarta '(1 2 1))
(1 3 3 1)
(tarta '(1 3 3 1))
(1 4 6 4 1)
>
> > If so, I guess
> > : (tarta 1 '(1))
> > (1)
> > really should have returned (1 1).
> > Can you seen what happens here?
> >
Yes, I know that is not rigth, but it's the best I've been abble to do.
How do it better? I'll be very happy if you give me a better solution.
(How to do a function wich you give it a single row and returns the
next row?).
> > > and this function generates the n-rows:
> >
> > > (defun ntar (N)
> > > (if (equal 1 N)
> > > (cons 1 1)
> > > (cons (TARTA (car (ntar (- N 1))))
> > > (ntar (- N 1)))))
> >
> > You never defined a function TAR, and you never used the function
> > TARTA, so I assume TAR was just a mistake for TARTA.
>
Yes, it was a mistake (I don't know what was i'm thinking about).
> >
> > The exercise asked for you to define a function TARTAGLIA. Your > >code has no such function. I assume your NTAR is supposed to be it?
Yes. tartaglia seems to be so long that I changed the name.
> > This function was supposed to return a list of lists, right?
> > Like e.g. ((1) (1 1) (1 2 1))
> >
Yes.
> > Now, the simplest case, (ntar 1), doesn't do that, it returns (cons 1
> > 1) which isn't a list at all. What were you thinking here?
> >
It's the best I've been able to do. I know that isn't correct enough,
(but the worse of all is that my teacher will know it too!.). Could
you give me a better idea?
> > Also, if NTAR is supposed to be TARTAGLIA,
Yes it is.
> > always return (1), so this is probably not what you meant.
Yes, I don't want it!
> > By the way, instead of CAR, CADR and CDR you could use FIRST, SECOND
> > and REST. Even if English isn't your native language, this should be easier to read.
>
OK. I'll note it for the next time.
>
> > (Your English is quite good, you know)
...Are you sure? You are very kind, but i've a lot of thinks to learn.
>
Well, I hope you could give me some way for solving this problem in a
better way. Thanks for all!
From: David Hanley
Subject: Re: a simple question about tartaglia triangle
Date:
Message-ID: <38D66F7F.58B5E9F6@ncgr.org>
You want a function where you pass it one row, say ( 1 3 3 1 ) and it
gives you back the next row ( 1 4 6 4 1 ), right? Well, first, you have
to state the problem to yourself clearly:
1) start with a one in my list
2) for each consecutive pair of numbers, add them and place this into
the result row
3) add a one onto the end.
There's a function like this in lisp already, reduce, and the function can
be written using reduce in just a few lines. On the honor system,
and the fact the teacher hasn't shown you this function and will know you
didn't write it, here's the code:
(defun consume( old-row )
(let ((out '(1)))
(reduce #'(lambda(n1 n2)(push (+ n1 n2) out) n2) old-row)
(push 1 out)))
CL-USER 4 : 1 > (consume '(1 3 3 1))
(1 4 6 4 1)
So for your assignment you (probably) need to write a recursive function.
Here's a little pseudocode to get you started:
(consumer( list )
(if one-element-left, return a one element list with just a one in it)
(if more than one element (otherwise), construct a list with the sum of the first two elements
plus the consumed rest of the list))
Hopefully, this will help.
By the way, your english is perfectly fine. You do a lot better than I did in spain.
dave