Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Caution: Keep out of reach of children.


comp / comp.lang.scheme / Re: Lisp Function Problem

SubjectAuthor
o Re: Lisp Function ProblemB. Pym

1
Subject: Re: Lisp Function Problem
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Thu, 26 Sep 2024 12:20 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Lisp Function Problem
Date: Thu, 26 Sep 2024 12:20:29 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <vd3jib$67df$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Thu, 26 Sep 2024 14:20:29 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="6cb849fef883bcf58fa0f863da8f36cf";
logging-data="204207"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18VhKnIK2VU919K6e6ohgzv"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:C2G02bdhHWIX755Ov/okNfFm6B0=
View all headers

> > 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 stuff)
(let* ((items (map car stuff))
(costs (map (~>> cdr (apply *)) stuff))
(total (fold + 0 costs)))
(append (map list items costs)
`((total ,total)))))

Given:

(define-syntax ->>
(syntax-rules ()
[(_ x) x]
[(_ x (y ...) z ...)
(->> (y ... x) z ...)]
[(_ x y z ...)
(->> (y x) z ...)]))

(define-syntax ~>>
(syntax-rules ()
[(_ (func0 a ...) func ...)
(lambda xs (->> (apply func0 a ... xs) func ...))]
[(_ func0 func ...)
(lambda xs (->> (apply func0 xs) func ...))]))

1

rocksolid light 0.9.8
clearnet tor