Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"The only way for a reporter to look at a politician is down." -- H. L. Mencken


devel / comp.lang.lisp / .Re: Lisp Function Problem

SubjectAuthor
o .Re: Lisp Function ProblemRobert L.

1
.Re: Lisp Function Problem

<sv9sd7$cqd$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16931&group=comp.lang.lisp#16931

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!aioe.org!4LB8ASW99KTkgLVfDAzYZg.user.46.165.242.75.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (Robert L.)
Newsgroups: comp.lang.lisp
Subject: .Re: Lisp Function Problem
Date: Fri, 25 Feb 2022 06:19:20 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <sv9sd7$cqd$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Info: gioia.aioe.org; logging-data="13133"; posting-host="4LB8ASW99KTkgLVfDAzYZg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: XanaNews/1.18.1.6
X-Notice: Filtered by postfilter v. 0.9.2
 by: Robert L. - Fri, 25 Feb 2022 06:19 UTC

> > 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))))))

Let's make it shorter and clearer.

Gauche Scheme and Racket:

(use srfi-1) ;; unzip3 for Gauche
or
(require srfi/1) ;; unzip3 and fold for Racket
(require srfi/8) ;; receive for Racket

(define (total seq)
(receive (items q c) (unzip3 seq)
(define totals (map * q c))
(define grand (fold + 0 totals))
`(,@(map list items totals) (total ,grand))))

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


devel / comp.lang.lisp / .Re: Lisp Function Problem

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor