Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

For courage mounteth with occasion. -- William Shakespeare, "King John"


comp / comp.lang.scheme / Re: Rosetta birthday problem

SubjectAuthor
* Rosetta birthday problemB. Pym
+- Re: Rosetta birthday problemKaz Kylheku
`- Re: Rosetta birthday problemB. Pym

1
Subject: Rosetta birthday problem
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 26 Jul 2024 21:26 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: Rosetta birthday problem
Date: Fri, 26 Jul 2024 21:26:41 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <v814ag$30n60$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Fri, 26 Jul 2024 23:26:42 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="747fa0f9e4f56b6369e69e3bde11dd7d";
logging-data="3169472"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+hUOEhEsmKKTfoDDX1pqNo"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:0Oa3HGm3ngtcJIOyL89b6OyKgCc=
View all headers

http://rosettacode.org/wiki/Cheryl%27s_birthday

> Cheryl's birthday
>
> Albert and Bernard just became friends with Cheryl, and they
> want to know when her birthday is.
>
> Cheryl gave them a list of ten possible dates:
>
> May 15, May 16, May 19
> June 17, June 18
> July 14, July 16
> August 14, August 15, August 17
>
> Cheryl then tells Albert the month of birth, and Bernard
> the day (of the month) of birth.
>
> 1) Albert: I don't know when Cheryl's birthday is, but I
> know that Bernard does not know, too.
>
> 2) Bernard: At first I didn't know when Cheryl's birthday is,
> but I know now.
>
> 3) Albert: Then I also know when Cheryl's birthday is.

Gauche Scheme

(use gauche.generator)
(use gauche.collection)

(define (remove-from xs key pred group?)
(let* ((keys (map key xs))
(bad
(filter
(lambda (k)
(let ((cnt (count (lambda(x) (equal? x k)) keys)))
(pred cnt)))
keys)))
(append-map
(lambda(g)
(if (any (lambda(x) (member (key x) bad)) g) '() g))
(if group?
(group-collection xs :key car :test equal?)
(map list xs)))))

(define (foo)
(define dates
(slices
(with-input-from-string
"May 15 May 16 May 19
June 17 June 18
July 14 July 16
August 14 August 15 August 17"
(cut generator->list read))
2))
(set! dates (remove-from dates cadr (^c (= c 1)) #t))
(print dates)
(set! dates (remove-from dates cadr (^c (> c 1)) #f))
(print dates)
(set! dates (remove-from dates car (^c (> c 1)) #t))
dates)

===>
((July 14) (July 16) (August 14) (August 15) (August 17))
((July 16) (August 15) (August 17))
((July 16))

Subject: Re: Rosetta birthday problem
From: Kaz Kylheku
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sat, 27 Jul 2024 07:43 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Rosetta birthday problem
Date: Sat, 27 Jul 2024 07:43:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 99
Message-ID: <20240726235406.930@kylheku.com>
References: <v814ag$30n60$1@dont-email.me>
Injection-Date: Sat, 27 Jul 2024 09:43:35 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7dc4349931df46a56d4f543b60e1cc03";
logging-data="3474308"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vJsYyQie+stK0YrethpoynO7+a9stu7s="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:72AvoAcx8wP6QhVWZVb4hVr/+JE=
View all headers

On 2024-07-26, B. Pym <Nobody447095@here-nor-there.org> wrote:
> http://rosettacode.org/wiki/Cheryl%27s_birthday
>
>> Cheryl's birthday
>>
>> Albert and Bernard just became friends with Cheryl, and they
>> want to know when her birthday is.
>>
>> Cheryl gave them a list of ten possible dates:
>>
>> May 15, May 16, May 19
>> June 17, June 18
>> July 14, July 16
>> August 14, August 15, August 17
>>
>> Cheryl then tells Albert the month of birth, and Bernard
>> the day (of the month) of birth.
>>
>> 1) Albert: I don't know when Cheryl's birthday is, but I
>> know that Bernard does not know, too.
>>
>> 2) Bernard: At first I didn't know when Cheryl's birthday is,
>> but I know now.
>>
>> 3) Albert: Then I also know when Cheryl's birthday is.
>
>
> Gauche Scheme
>
> (use gauche.generator)
> (use gauche.collection)
>
> (define (remove-from xs key pred group?)
> (let* ((keys (map key xs))
> (bad
> (filter
> (lambda (k)
> (let ((cnt (count (lambda(x) (equal? x k)) keys)))
> (pred cnt)))
> keys)))
> (append-map
> (lambda(g)
> (if (any (lambda(x) (member (key x) bad)) g) '() g))
> (if group?
> (group-collection xs :key car :test equal?)
> (map list xs)))))
>
> (define (foo)
> (define dates
> (slices
> (with-input-from-string
> "May 15 May 16 May 19
> June 17 June 18
> July 14 July 16
> August 14 August 15 August 17"
> (cut generator->list read))
> 2))
> (set! dates (remove-from dates cadr (^c (= c 1)) #t))
> (print dates)
> (set! dates (remove-from dates cadr (^c (> c 1)) #f))
> (print dates)
> (set! dates (remove-from dates car (^c (> c 1)) #t))
> dates)
>
> ===>
> ((July 14) (July 16) (August 14) (August 15) (August 17))
> ((July 16) (August 15) (August 17))
> ((July 16))

$ txr cheryls-birthday.tl
((July 14) (July 16) (August 14) (August 15) (August 17))
((July 16) (August 15) (August 17))
((July 16))

$ cat cheryls-birthday.tl
(defun munge (groupfn selfn keepfn filfn data)
(flow data
(group-by groupfn)
(mappend (do if-match (@nil @pair) @1 (list [selfn pair])))
(keepfn (opip filfn (member @1 @@1)) data)))

(flow "May 15, May 16, May 19\n \
June 17, June 18\n \
July 14, July 16\n \
August 14, August 15, August 17\n"
(remq #\,)
read-objects
(tuples 2)
(munge second first remove-if first)
prinl
(munge second second keep-if second)
prinl
(munge first second keep-if second)
prinl)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Subject: Re: Rosetta birthday problem
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sun, 4 Aug 2024 21:37 UTC
References: 1
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: Rosetta birthday problem
Date: Sun, 4 Aug 2024 21:37:34 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 115
Message-ID: <v8osao$8i81$1@dont-email.me>
References: <v814ag$30n60$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 04 Aug 2024 23:37:34 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e14708043080897b7486957644b6603d";
logging-data="280833"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+6BKWR4TcvMfSB8pLVeuVX"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:ANmOfOorcXdgjsf7zVcUJnpcnSU=
View all headers

B. Pym wrote:

> http://rosettacode.org/wiki/Cheryl%27s_birthday
>
> > Cheryl's birthday
> >
> > Albert and Bernard just became friends with Cheryl, and they
> > want to know when her birthday is.
> >
> > Cheryl gave them a list of ten possible dates:
> >
> > May 15, May 16, May 19
> > June 17, June 18
> > July 14, July 16
> > August 14, August 15, August 17
> >
> > Cheryl then tells Albert the month of birth, and Bernard
> > the day (of the month) of birth.
> >
> > 1) Albert: I don't know when Cheryl's birthday is, but I
> > know that Bernard does not know, too.
> >
> > 2) Bernard: At first I didn't know when Cheryl's birthday is,
> > but I know now.
> >
> > 3) Albert: Then I also know when Cheryl's birthday is.
>
>
> Gauche Scheme
>
> (use gauche.generator)
> (use gauche.collection)
>
> (define (remove-from xs key pred group?)
> (let* ((keys (map key xs))
> (bad
> (filter
> (lambda (k)
> (let ((cnt (count (lambda(x) (equal? x k)) keys)))
> (pred cnt)))
> keys)))
> (append-map
> (lambda(g)
> (if (any (lambda(x) (member (key x) bad)) g) '() g))
> (if group?
> (group-collection xs :key car :test equal?)
> (map list xs)))))
>
> (define (foo)
> (define dates
> (slices
> (with-input-from-string
> "May 15 May 16 May 19
> June 17 June 18
> July 14 July 16
> August 14 August 15 August 17"
> (cut generator->list read))
> 2))
> (set! dates (remove-from dates cadr (^c (= c 1)) #t))
> (print dates)
> (set! dates (remove-from dates cadr (^c (> c 1)) #f))
> (print dates)
> (set! dates (remove-from dates car (^c (> c 1)) #t))
> dates)
>
> ===>
> ((July 14) (July 16) (August 14) (August 15) (August 17))
> ((July 16) (August 15) (August 17))
> ((July 16))

newLISP

(define (get-month xs) (first xs))
(define (get-day xs) (nth 1 xs))
(define single? (curry = 1))
(define multiple? (curry < 1))
(define (count1 x xs) (first (count (list x) xs)))

(define (remove-from xs key pred delete-whole-month?)
(letn (keys (map key xs)
bad-keys '()
bad-months '())
(dolist (birthday xs)
(when (pred (count1 (key birthday) keys))
(push (get-month birthday) bad-months)
(push (key birthday) bad-keys)))
(if delete-whole-month?
(clean
(fn (birthday) (member (get-month birthday) bad-months))
xs)
(clean
(fn (birthday) (member (key birthday) bad-keys))
xs))))

(define (foo)
(let (dates (explode (parse
"May 15 May 16 May 19
June 17 June 18
July 14 July 16
August 14 August 15 August 17")
2))
(setq dates (remove-from dates get-day single? true))
(println dates)
(setq dates (remove-from dates get-day multiple? nil))
(println dates)
(setq dates (remove-from dates get-month multiple? true))))

(foo)

(("July" "14") ("July" "16") ("August" "14") ("August" "15")
("August" "17"))
(("July" "16") ("August" "15") ("August" "17"))
(("July" "16"))

1

rocksolid light 0.9.8
clearnet tor