From: nissan2000
Subject: Lisp Function Problem
Date: 
Message-ID: <3c9eed0dad6a581e3fc030de909ca40c@localhost.talkaboutprogramming.com>
Write a Function 'total' that takes an orderd list ie. 
((itemA quantityA costA)(itemB quantityB costB)....)
but returns a list giving the total cost plus the overall cost.

Eg:

LISP>(total'((book 2 10)(pen 3 2)(notepad 1 12)))
((BOOK 20)(PEN 6)(NOTEPAD 12)(TOTAL 38))

Thank you for your time

From: Karl A. Krueger
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <cfbajm$j9h$1@baldur.whoi.edu>
nissan2000 <······@campus.ie> wrote:
> Write a Function 'total' that takes an orderd list ie. 
> ((itemA quantityA costA)(itemB quantityB costB)....)
> but returns a list giving the total cost plus the overall cost.

Write a Function 'homework' that takes a homework problm ie.

	"Write a Function 'total' that takes an orderd list ie.
	((itemA quantityA costA)(itemB quantityB costB)....)
	but returns a list giving the total cost plus the overall
	cost."

but returns a function giving the answer to the homework problem.

Thank you for your time

-- 
Karl A. Krueger <········@example.edu>
Woods Hole Oceanographic Institution
Email address is spamtrapped.  s/example/whoi/
"Outlook not so good." -- Magic 8-Ball Software Reviews
From: neo88
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <6a73bb68.0408110518.2b98e26d@posting.google.com>
"Karl A. Krueger" <········@example.edu> wrote in message news:<············@baldur.whoi.edu>...
> nissan2000 <······@campus.ie> wrote:
> > Write a Function 'total' that takes an orderd list ie. 
> > ((itemA quantityA costA)(itemB quantityB costB)....)
> > but returns a list giving the total cost plus the overall cost.
 
Use mapcar. And #'+. You should already know of these if you are
trying to write this function.

-- 
May the Source Be with you.
neo88 (Philip Haddad)
From: Mark McConnell
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <d3aed052.0408110621.3cb3867@posting.google.com>
"nissan2000" <······@campus.ie> wrote in message news:<································@localhost.talkaboutprogramming.com>...
> Write a Function 'total' that takes an orderd list ie. 
> ((itemA quantityA costA)(itemB quantityB costB)....)
> but returns a list giving the total cost plus the overall cost.
> 
> Eg:
> 
> LISP>(total'((book 2 10)(pen 3 2)(notepad 1 12)))
> ((BOOK 20)(PEN 6)(NOTEPAD 12)(TOTAL 38))
> 
> Thank you for your time

Hint 1: If this exercise comes from the section of your textbook about
'do' or 'dolist', reread that material carefully.

If it comes from the section about recursive functions (functions that
call themselves), reread that material carefully and many, many times.
 This is one of the most mind-expanding parts of Lisp.  It will
improve your programming ability in any language you use in the
future.

Hint 2: Review the functions 'second' and 'third'.  Review the
function 'list' for how to make (BOOK 20) once you have the BOOK and
the 20.  Review * and +, where for instance (* 2 10) gives 20.
From: Martin Pirstitz
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <411d1538$0$12126$3b214f66@tunews.univie.ac.at>
nissan2000 wrote:
> Write a Function 'total' that takes an orderd list ie. 
> ((itemA quantityA costA)(itemB quantityB costB)....)
> but returns a list giving the total cost plus the overall cost.
> 
> Eg:
> 
> LISP>(total'((book 2 10)(pen 3 2)(notepad 1 12)))
> ((BOOK 20)(PEN 6)(NOTEPAD 12)(TOTAL 38))
> 
> Thank you for your time
> 

Here are 3 quite different solutions... one recursive, one iterative,
and one obscure. I won't do ALL your homework, so you will have to
figure out yourself how they work, which shouldn't be very hard.
Disclaimer: I'm quite new to Lisp myself, so there may be better and
more elegant solutions.

;;; 1
(defun total (lst)
   (labels ((total-rec (sublst subtot)
                       (if sublst
                         (let* ((sub (car sublst))
                                (sum (* (cadr sub) (caddr sub))))
                           (cons `(,(car sub) ,sum)
                               (total-rec (cdr sublst) (+ subtot sum))))
                         `((total ,subtot)))))
     (total-rec lst 0)))

;;; 2
(defun total (lst)
   (let ((total 0)
         (ret))
     (dolist (elt lst (nreverse (push `(total ,total) ret)))
       (let ((subtotal (* (cadr elt) (caddr elt))))
         (push `(,(car elt) ,subtotal) ret)
         (incf total subtotal)))))

;;; 3
(defun total (lst)
   (append
     (mapcar #'(lambda (e) (list (car e) (* (cadr e) (caddr e)))) lst)
     `((total ,(apply #'+ (mapcar #'(lambda (elt) (* (cadr elt) (caddr 
elt))) lst))))))


-- Martin
From: Pascal Bourguignon
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <87isbmerfc.fsf@thalassa.informatimago.com>
Martin Pirstitz <········@NOstudent.SPAMtuwien.ac.at> writes:
> Here are 3 quite different solutions... one recursive, one iterative,
> and one obscure. I won't do ALL your homework, so you will have to
> figure out yourself how they work, which shouldn't be very hard.
> Disclaimer: I'm quite new to Lisp myself, so there may be better and
> more elegant solutions.
> 
> ;;; 1
> (defun total (lst)
>    (labels ((total-rec (sublst subtot)
>                        (if sublst
>                          (let* ((sub (car sublst))
>                                 (sum (* (cadr sub) (caddr sub))))
>                            (cons `(,(car sub) ,sum)
>                                (total-rec (cdr sublst) (+ subtot sum))))
>                          `((total ,subtot)))))
>      (total-rec lst 0)))
> 
> ;;; 2
> (defun total (lst)
>    (let ((total 0)
>          (ret))
>      (dolist (elt lst (nreverse (push `(total ,total) ret)))
>        (let ((subtotal (* (cadr elt) (caddr elt))))
>          (push `(,(car elt) ,subtotal) ret)
>          (incf total subtotal)))))
> 
> ;;; 3
> (defun total (lst)
>    (append
>      (mapcar #'(lambda (e) (list (car e) (* (cadr e) (caddr e)))) lst)
>      `((total ,(apply #'+ (mapcar #'(lambda (elt) (* (cadr elt) (caddr
> elt))) lst))))))

Here is one with REDUCE:

(defun total (items)
  (nreverse 
   (reduce (lambda (tots item)
             (push (list (first item) (* (second item) (third item)))
                   (cdr tots))
             (incf (cadar tots) (cadadr tots))
             tots) items :initial-value (list (list 'total 0)))))

somewhat comparable in complexities to your #2.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.
From: Coby Beck
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <tsoTc.11088$X12.8499@edtnps84>
"Martin Pirstitz" <········@NOstudent.SPAMtuwien.ac.at> wrote in message
······························@tunews.univie.ac.at...
> nissan2000 wrote:
> > Write a Function 'total' that takes an orderd list ie.
> > ((itemA quantityA costA)(itemB quantityB costB)....)
> > but returns a list giving the total cost plus the overall cost.
> >
> > Eg:
> >
> > LISP>(total'((book 2 10)(pen 3 2)(notepad 1 12)))
> > ((BOOK 20)(PEN 6)(NOTEPAD 12)(TOTAL 38))
> >
> > Thank you for your time
> >
>
> Here are 3 quite different solutions... one recursive, one iterative,
> and one obscure. I won't do ALL your homework, so you will have to
> figure out yourself how they work, which shouldn't be very hard.
> Disclaimer: I'm quite new to Lisp myself, so there may be better and
> more elegant solutions.

All of your solutions are using the backquote as a short hand for list.  I
don't think that is a good idea as a general principle.  Unless you are
being very thoughtful about how and when you apply it (I did not try to see
if there is really any problem) you can get yourself into "Undefined
Consequence" territory.

Other opinions?


-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")


> ;;; 1
> (defun total (lst)
>    (labels ((total-rec (sublst subtot)
>                        (if sublst
>                          (let* ((sub (car sublst))
>                                 (sum (* (cadr sub) (caddr sub))))
>                            (cons `(,(car sub) ,sum)
>                                (total-rec (cdr sublst) (+ subtot sum))))
>                          `((total ,subtot)))))
>      (total-rec lst 0)))
>
> ;;; 2
> (defun total (lst)
>    (let ((total 0)
>          (ret))
>      (dolist (elt lst (nreverse (push `(total ,total) ret)))
>        (let ((subtotal (* (cadr elt) (caddr elt))))
>          (push `(,(car elt) ,subtotal) ret)
>          (incf total subtotal)))))
>
> ;;; 3
> (defun total (lst)
>    (append
>      (mapcar #'(lambda (e) (list (car e) (* (cadr e) (caddr e)))) lst)
>      `((total ,(apply #'+ (mapcar #'(lambda (elt) (* (cadr elt) (caddr
> elt))) lst))))))
>
>
> -- Martin
From: Pascal Costanza
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <cfl6p7$279$1@newsreader2.netcologne.de>
Coby Beck wrote:

> All of your solutions are using the backquote as a short hand for list.  I
> don't think that is a good idea as a general principle.  Unless you are
> being very thoughtful about how and when you apply it (I did not try to see
> if there is really any problem) you can get yourself into "Undefined
> Consequence" territory.
> 
> Other opinions?

He doesn't use the so constructed lists in destructive operations, so I 
don't see why this should be a problem. I am using backquoting in 
similar ways outside of macros because I think it makes the code easier 
to read (not always, of course).

What worries me more is his use of "lst" instead of "list" as a variable 
name. ;)


Pascal

-- 
Tyler: "How's that working out for you?"
Jack: "Great."
Tyler: "Keep it up, then."
From: neo88
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <6a73bb68.0408150459.6411aa5f@posting.google.com>
> What worries me more is his use of "lst" instead of "list" as a variable 
> name. ;)

Why wuold you want to use the variable name "list"? There is already a
function with that name. So as not to be confused, wouldn't be better
to name the args to the function as "lst"? That was always me
impression anyways....
> 
> Pascal

-- 
May the Source be with you.
neo88 (Philip Haddad)
From: Martin Pirstitz
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <411f82a7$0$10320$3b214f66@tunews.univie.ac.at>
neo88 wrote:
> Why wuold you want to use the variable name "list"? There is already a
> function with that name. So as not to be confused, wouldn't be better
> to name the args to the function as "lst"? That was always me
> impression anyways....

Well, as a matter of fact, no, they cannot be confused. Someone please
correct me if I'm wrong, but a symbol has both a value and a function
property (among other things), and the function property is never used
except if the symbol turns out to be the first element of a list, in
other words in a function call. As long as one bears this in mind, there
should be no point of confusion, even if you do something like

(setf list 123)
(list list list list)

because you know exactly what's the function and what are the variables
(although I admit it looks plain 'wrong' to someone used to imperative
languages [like myself], which probably is the reason I used 'lst'
instead of 'list'... old habits are hard to get rid of).

--Martin
From: Pascal Bourguignon
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <87llgf9x2x.fsf@thalassa.informatimago.com>
Martin Pirstitz <········@NOstudent.SPAMtuwien.ac.at> writes:
> [...]
> (setf list 123)
> (list list list list)
> [...]
> (although I admit it looks plain 'wrong' to someone used to imperative
> languages [like myself], which probably is the reason I used 'lst'
> instead of 'list'... old habits are hard to get rid of).

Do you mean more wrong or less wrong than the following correct C code:

typedef int list;
struct list { list list; };
list f(struct list* list){ return(list->list); }


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.
From: Harald Hanche-Olsen
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <pcooelcl6sq.fsf@shuttle.math.ntnu.no>
+ ······@truevine.net (neo88):

| > What worries me more is his use of "lst" instead of "list" as a variable 
| > name. ;)
| 
| Why wuold you want to use the variable name "list"? There is already a
| function with that name. So as not to be confused, wouldn't be better
| to name the args to the function as "lst"? That was always me
| impression anyways....

Common Lisp has separate name spaces for functions and other values,
precisely so that this confusion will not arise.  Therefore, whenever
you see someone use variable names like LST, that is a clear
indication that they are probably confused about the name space issue
and worry needlessly about a non-problem.  This has been discussed at
length on this newsgroup earlier, and all the long-term residents must
be kind of tired of this discussion.  Google the group for articles by
Kent Pitman containing "Lisp-2", and you should find plenty of food
for thought.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- Debating gives most of us much more psychological satisfaction
  than thinking does: but it deprives us of whatever chance there is
  of getting closer to the truth.  -- C.P. Snow
From: Coby Beck
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <oDcUc.34513$fz2.23727@edtnps89>
"Pascal Costanza" <········@web.de> wrote in message
·················@newsreader2.netcologne.de...
>
> Coby Beck wrote:
>
> > All of your solutions are using the backquote as a short hand for list.
I
> > don't think that is a good idea as a general principle.  Unless you are
> > being very thoughtful about how and when you apply it (I did not try to
see
> > if there is really any problem) you can get yourself into "Undefined
> > Consequence" territory.
> >
> > Other opinions?
>
> He doesn't use the so constructed lists in destructive operations, so I
> don't see why this should be a problem. I am using backquoting in
> similar ways outside of macros because I think it makes the code easier
> to read (not always, of course).

Sorry, I should have been more clear.  When I said "other opinions" I meant
"other opinions that agree with me" ;)

> What worries me more is his use of "lst" instead of "list" as a variable
> name. ;)

A symptom of a deeper illness!

I guess I use the backquote in functions too, when not using it would mean a
whole mess of quotes on list elements.  But if everything inside has a comma
applied to it blah, blah...<my own bias>  Note I did qualify it with "as a
general principal"

If it is your default mode for constructing lists it can make bugs like
this:

> (defun total (lst)
>    (labels ((total-rec (sublst subtot)
>                        (if sublst
>                          (let* ((sub (car sublst))
>                                 (sum (* (cadr sub) (caddr sub))))
>                            (cons `(,(car sub) ,sum)
>                                (total-rec (cdr sublst) (+ subtot sum))))
==>                        `((total ,subtot)))))
>      (total-rec lst 0)))

harder to see!

And here is some destructive operation:

> (defun total (lst)
>    (let ((total 0)
>          (ret))
==>    (dolist (elt lst (nreverse (push `(total ,total) ret)))
>        (let ((subtotal (* (cadr elt) (caddr elt))))
==>        (push `(,(car elt) ,subtotal) ret)
>          (incf total subtotal)))))

I for one would be wasting time making sure my cuteness is not
dangerous...ok, true the backquoted material is not effected.  I just like
to keep my fingers farther from the ripsaw blade rather than checking for
blood every few minutes!

This one fails the easier to read test (for me): (reformatted to help out)

> (defun total (lst)
>    (append
>      (mapcar #'(lambda (e)
>                  (list (car e) (* (cadr e) (caddr e))))
>          lst)
>    `((total ,(apply #'+ (mapcar #'(lambda (elt)
>                                       (* (cadr elt) (caddr elt)))
>                              lst))))))

plus line 6 has the same 'total bug as before.

Sorry for being so critical, Martin!  (It's much easier to criticize than
post code...but you said you were a newbie so my advice is newbie oriented.)


-- 
Coby Beck
(remove #\Space "coby 101 @ big pond . com")
From: Coby Beck
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <TUeUc.19845$X12.17756@edtnps84>
"Coby Beck" <·····@mercury.bc.ca> wrote in message
··························@edtnps89...
> And here is some destructive operation:
>
> > (defun total (lst)
> >    (let ((total 0)
> >          (ret))
> ==>    (dolist (elt lst (nreverse (push `(total ,total) ret)))
> >        (let ((subtotal (* (cadr elt) (caddr elt))))
> ==>        (push `(,(car elt) ,subtotal) ret)
> >          (incf total subtotal)))))

sorry, only the first indication should be there
From: Pascal Costanza
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <cfsoa0$re7$1@newsreader2.netcologne.de>
Coby Beck wrote:

> Sorry, I should have been more clear.  When I said "other opinions" I meant
> "other opinions that agree with me" ;)

No way! ;)

> I for one would be wasting time making sure my cuteness is not
> dangerous...ok, true the backquoted material is not effected.  I just like
> to keep my fingers farther from the ripsaw blade rather than checking for
> blood every few minutes!

OK, I see.


Pascal

-- 
Tyler: "How's that working out for you?"
Jack: "Great."
Tyler: "Keep it up, then."
From: ·······@Yahoo.Com
Subject: Re: Lisp Function Problem
Date: 
Message-ID: <REM-2004aug15-004@Yahoo.Com>
> From: "nissan2000" <······@campus.ie>
> Write a Function 'total' that takes an orderd list ie.
> ((itemA quantityA costA)(itemB quantityB costB)....)
> but returns a list giving the total cost plus the overall cost.
> Eg:
> LISP>(total'((book 2 10)(pen 3 2)(notepad 1 12)))
> ((BOOK 20)(PEN 6)(NOTEPAD 12)(TOTAL 38))

First, your English leaves something to be desired. Total cost and
overall cost mean exactly the same thing. What you meant to say was
per-kind-of-item subtotal and overall total cost, right?

Second, either you're an absolute beginner who hasn't the foggiest idea
how to analyze a problem and attempt a solution, or you're a lazy bum
who expects people on the net to do your homework for you. I'll assume
the former, and try to give you kindergarden-level Socratic tutoring.

The first thing is to notice the problem breaks down into three parts.
Can you state clearly what the two parts are, and what the output from
the first part should be, and what input (possibly the same as output
from first part) is needed as input to the second part, and how the
output from the first two parts is combined by the third part to yield
the final answer? Once you've split the problem, we can work on one
part or another separately.

In parallel with that, you need to learn how to write simple program
loops. Do you know how to draw a simple flowchart showing an iteration
through a range of indexes or down a list until the end is reached?
If yes, please draw an ASCII-text flowchart to loop through the numbers
from 1, stepping by 3, until you reach 14, adding the numbers to a
running total which is initialized at 1000 before the loop starts.
Don't be fancy, just boxes like this:
+--------+
|        |
+--------+
with words inside them and ---- or vertical-bar lines connecting them,
and < or > or V or ^ as arrowheads. For example, see this:
http://www.rawbw.com/~rem/StandardPair.AsciiArt.txt
which is not a flowchart, it's a data-pointy-structure diagram, but
nearly the same visual technique can be used to draw flowcharts.

If you have no idea how to organize an algorithm into a loop, or to
draw a flowchart of such a loop, you can start with something easier:
Imagine you have a pocket calculator that can do only one thing at a
time. Imagine you are able to count just fine, even count by threes (or
maybe you write down the counting by threes on paper and key them back
into the calculator later), but you need the calculator to add these
3-stepping numbers into a total. Ignoring the details of the calculator
like the C key to clear and the ENTER key etc., what would be the
sequence of steps to perform? Write them in pseudo code. Here are those
numbers stepping by 3:  1, 4, 7, 10, 13, 16xxxDoneAlready, and here are
starter and ender for how to add them the way I defined above:
- Enter 1000.
- Add 1.
- Add 4.
..
- Add 13.
- DONE (don't add 16) (already-displayed total is the answer)
Now you fill in the missing steps, remember this is kindergarden level,
you need to learn how to crawl, without missing even one step, before
you can walk or ride a bike or drive a car or fly an airplane or pilot
a spacecraft or build a Dyson sphere.

After you finish that exercise, there are two directions go to:
Calculator/pseudocode to set up a counter instead of handcoding those
numbers that step by 3, or translating the sequence of steps into LISP
syntax. After the first of those is done, we can start thinking about
how to make a loop out of that algorithm.