tag:blogger.com,1999:blog-22238331.post6949395633878196004..comments2024-02-04T10:25:17.591+00:00Comments on Vague vagaries: 99 lisp problems. Some are hard!Oisínhttp://www.blogger.com/profile/14940197011612073631noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-22238331.post-29329042728088002512010-02-02T02:57:10.121+00:002010-02-02T02:57:10.121+00:00oops, fixed the bug in this one.
(define (group-s...oops, fixed the bug in this one.<br /><br />(define (group-similar grouped ol) <br /> (cond <br /> ((null? ol) (list grouped)) ; change here <br /> ((= (car grouped) (car ol)) <br /> (group-similar <br /> (cons (car ol) grouped) <br /> (cdr ol))) <br /> (else <br /> (cons grouped <br /> (group-similar <br /> (cons (car ol) '()) <br /> (cdr ol)))))) <br /><br />(group-similar '(1) '(1 2 3 3 3 3 3 3 3))<br /><br />> ((1 1) (2) (3 3 3 3 3 3 3))cijuhttp://ciju.wordpress.comnoreply@blogger.comtag:blogger.com,1999:blog-22238331.post-30543475963134182812010-01-30T13:58:52.644+00:002010-01-30T13:58:52.644+00:00ciju:
Thanks for that - it's far neater than ...ciju:<br /><br />Thanks for that - it's far neater than my solution, but it does fall down on a lot of input:<br /><br />> (group-similar '(1) '(1 2 3 3 3 3 3 3 3))<br />((1 1) (2))<br /><br />I tried using <pre> but Blogger doesn't like it.. doh :/Oisínhttps://www.blogger.com/profile/14940197011612073631noreply@blogger.comtag:blogger.com,1999:blog-22238331.post-6604266123419239262010-01-30T07:34:51.439+00:002010-01-30T07:34:51.439+00:00(define (group-similar grouped ol)
(cond
((nu...(define (group-similar grouped ol)<br /> (cond<br /> ((null? ol) '())<br /> ((= (car grouped) (car ol))<br /> (group-similar<br /> (cons (car ol) grouped)<br /> (cdr ol)))<br /> (else<br /> (cons grouped<br /> (group-similar<br /> (cons (car ol) '())<br /> (cdr ol))))))<br /><br />(group-similar '(1) '(4 4 8 1 9 1 1 1 2 2 3))<br /><br />hopefully the code is easy to understand.<br /><br />as a side not, is there an easy way to preserve code formatting in blogger comments. i had to put nbsp, lots of it :(cijuhttp://ciju.wordpress.comnoreply@blogger.comtag:blogger.com,1999:blog-22238331.post-21293930260246678042008-08-28T01:12:00.000+01:002008-08-28T01:12:00.000+01:00Hi Ratboy, thanks for your comment. Although I'm e...Hi Ratboy, thanks for your comment. Although I'm even less experienced with Scheme than I am with Common Lisp, your solution is much cleaner than mine and nicely tail-recursive too.<BR/>I haven't run it yet, but I'll give it a shot in Gambit-C Scheme after some sleep. Good stuff!Oisínhttps://www.blogger.com/profile/14940197011612073631noreply@blogger.comtag:blogger.com,1999:blog-22238331.post-66006217544019884952008-08-25T03:46:00.000+01:002008-08-25T03:46:00.000+01:00Commenting for the solution...The function encode(...Commenting for the solution...<BR/><BR/>The function encode() creates the RLE list of lists encoding. The new encoding must be held somewhere, so we immediately call a helper function enc(). The first parameter to enc is the remainder to be done, and the second is the built of list-of-lists encoding.<BR/><BR/>enc() simply "cdr's" down the first list, merging the built up solution with the new first element, using the merge() function. As a bonus, enc() is then fully tail recursive.<BR/><BR/>The merge() function is the "heart" of the routine. It is passed the element, and the list-of-lists built to date. If the list-of-lists is empty, the return is trivially (list (list e)). Otherwise, we look at the first element of the first sublist of the list-of-lists. If it matches, build a sublist one longer, and return the cons of that new list and the remainder of the list-of-lists. The last case (no match) simply adds a new sublist to the front of the list-of-lists.<BR/><BR/>Unfortunately, the result is built in the "wrong" order; the encode() function then reverse()s the list-of-lists before returning it.<BR/><BR/>I finally had a peek at the questions -- this framework is easy to modify to accommodate. EG. A representation like<BR/>((5 A) B (2 C)) -- use pair?() to tell what it is, and cadr() to extract the element for testing.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-22238331.post-15128288685839343672008-08-23T02:46:00.000+01:002008-08-23T02:46:00.000+01:00Try this: It's probably not the best solution.(def...Try this: It's probably not the best solution.<BR/><BR/>(define (merge e l)<BR/> (cond ((null? l) (list (list e)))<BR/> ((= (caar l) e)<BR/> (cons (cons e (car l)) (cdr l)))<BR/> (else (cons (list e) l))))<BR/><BR/>(define (enc l1 l2)<BR/> (cond ((null? l1) l2)<BR/> (else (enc (cdr l1) (merge (car l1) l2)))))<BR/><BR/>(define (encode l)<BR/> (reverse (enc l '())))<BR/><BR/>(pp (encode '(1 4 4 8 1 9 1 1 1 2 2 3)))Anonymousnoreply@blogger.com