Groups | Search | Server Info | Login | Register


Groups > comp.lang.scheme > #6541

Re: Lisp Function Problem

From "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups comp.lang.lisp, comp.lang.scheme
Subject Re: Lisp Function Problem
Date 2025-08-07 23:30 +0000
Organization A noiseless patient Spider
Message-ID <1073cu3$be2l$1@dont-email.me> (permalink)
References <vd3jib$67df$1@dont-email.me>

Cross-posted to 2 groups.

Show all headers | View raw


B. Pym 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))))))

Gauche Scheme

(define (proc purchases)
  (define grand-total 0)
  (define (calc item quan price)
    (let1 total (* quan price)
      (inc! grand-total total)
      (list item total)))
  (append
    (map: calc purchases)
    (list (list 'total grand-total))))

(proc '((book 2 10) (pen 3 2) (notepad 1 12)))
  ===>
((book 20) (pen 6) (notepad 12) (total 38))


Given:

(define (map: func seq)
  (map (lambda(x) (apply func x)) seq))


-- 
[T]he problem is that lispniks are as cultish as any other devout group and
basically fall down frothing at the mouth if they see [heterodoxy].
  --- Kenny Tilton
The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham

Back to comp.lang.scheme | Previous | NextPrevious in thread | Find similar


Thread

Re: Lisp Function Problem "B. Pym" <Nobody447095@here-nor-there.org> - 2024-09-26 12:20 +0000
  Re: Lisp Function Problem "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-07 23:30 +0000

csiph-web