Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth


devel / comp.lang.lisp / .Re: Exercises please

SubjectAuthor
o .Re: Exercises pleaseRobert L.

1
.Re: Exercises please

<sv6id2$2d3$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!aioe.org!ml5Mrp15EVwugPZCEWuB5w.user.46.165.242.75.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (Robert L.)
Newsgroups: comp.lang.lisp
Subject: .Re: Exercises please
Date: Thu, 24 Feb 2022 00:10:12 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <sv6id2$2d3$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Info: gioia.aioe.org; logging-data="2467"; posting-host="ml5Mrp15EVwugPZCEWuB5w.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. - Thu, 24 Feb 2022 00:10 UTC

Pascal J. Bourguignon wrote:

> and my solutions (still incomplete):
> http://www.informatimago.com/develop/lisp/l99/index.html

Where we find:

(---------------------------------------------------------------
P13 (**) Run-length encoding of a list (direct solution).

Example:
* (encode-direct '(a a a a b c c a a d e e e e))
((4 A) B (2 C) (2 A) D (4 E))
"

;; Iterative solution, uses only O(r) space:

(defun encode-modified (list)
(let ((result '())
(count 0)
(last-item nil))
(labels ((collect-result ()
(push (if (= 1 count)
last-item
(list count last-item))
result))
(new-item (item)
(setf count 1
last-item item))
(same-item ()
(incf count))
(return-result ()
(when (plusp count)
(collect-result))
(nreverse result)))
(dolist (item list (return-result))
(cond
((zerop count) (new-item item))
((eql item last-item) (same-item))
(t (collect-result)
(new-item item)))))))
---------------------------------------------------------------)

Gauche Scheme or Racket:

(require srfi/1) ;; fold-right for Racket

(define (encode the-list)
(define (increment n item) (list (+ 1 n) item))
(define (update xs . accum) (cons (apply increment xs) accum))
(define (kons x accum)
(if (or (null? accum) (not (equal? x (cadar accum))))
(cons (list 1 x) accum)
(apply update accum)))
(map
(lambda (xs) (if (= 1 (car xs)) (cadr xs) xs))
(fold-right
kons
'()
the-list)))

(encode '(a a a a b c c a a d e e e e))
===>
((4 a) b (2 c) (2 a) d (4 e))


devel / comp.lang.lisp / .Re: Exercises please

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor