check-duplicates

470 views
Skip to first unread message

Jos Koot

unread,
May 26, 2016, 4:35:51 PM5/26/16
to Racket Users, Jos Koot
Hi,
 
(member #f '(#f)) -> (#f)
(member #f '(#t)) -> #f
 
Nice and clear, but:
 
(check-duplicates '(#f #f)) -> #f
(check-duplicates '(#f #t)) -> #f
 
check-duplicates gives no clear answer when #f is duplicated.
 
I propose to modify check-duplicates such as to make a distinction between
(check-duplicates '(#f #f)) and (check-duplicates '(#f #t)).
 
May be cumbersome when many people already use check-duplicates, though,
for I see no way to repair this with backward compatibility.
 
Thanks, Jos
 

Matthias Felleisen

unread,
May 26, 2016, 7:23:32 PM5/26/16
to Jos Koot, Racket Users

You can add a keyword argument that changes the not-found result for such a case:

(define special (gensym))
(check-duplicates+ '(#f #f) #:not-found special)

Its signature would have to look like this then:

;; check-duplicates : (listof X)
;; [(K K -> bool)]
;; #:key (X -> K)
;; -> X or #f
(define (check-duplicates items
[same? equal?]
#:not-found [default #f]
#:key [key values])
…)

This way the existing interface works but someone who needs it for a list of Booleans (or something else I can’t think of right now) can get a distinct answer.


[[ Racket’s universal type lacks true option types, and we tend to approximate it with (U #f X). ]]

— Matthias
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Jos Koot

unread,
May 27, 2016, 1:00:01 PM5/27/16
to Matthias Felleisen, Racket Users, Jos Koot
Hi Matthias,
Thanks.
Very nice backward compatible suggestion.
I'll look into the source code of check-duplicates.
I should be able to modify it according to your suggestion.
I'll let you know as soon as I have it.
Thanks again, Jos.

-----Original Message-----
From: Matthias Felleisen [mailto:matt...@ccs.neu.edu]
Sent: viernes, 27 de mayo de 2016 1:24
To: Jos Koot
Cc: Racket Users
Subject: Re: [racket-users] check-duplicates


You can add a keyword argument that changes the not-found result for such a
case:

(define special (gensym))
(check-duplicates+ '(#f #f) #:not-found special)

Its signature would have to look like this then:

;; check-duplicates : (listof X)
;; [(K K -> bool)]
;; #:key (X -> K)
;; -> X or #f
(define (check-duplicates items
[same? equal?]
#:not-found [default #f]
#:key [key values])
.)

This way the existing interface works but someone who needs it for a list of
Booleans (or something else I can't think of right now) can get a distinct
answer.


[[ Racket's universal type lacks true option types, and we tend to
approximate it with (U #f X). ]]

- Matthias



> On May 26, 2016, at 4:35 PM, Jos Koot <jos....@gmail.com> wrote:
>
snip

Matthias Felleisen

unread,
May 27, 2016, 1:41:43 PM5/27/16
to Jos Koot, Racket Users

Open drracket, type in check-duplicates, and follow the right-click menu to find the source. Then submit a pull request on github. — Matthias

Jos Koot

unread,
May 27, 2016, 2:35:22 PM5/27/16
to Matthias Felleisen, Racket Users, Jos Koot

Hi Matthias,
It appeared to be easy to adapt check-duplicates according to your
suggestion:

(define (check-duplicates items
[same? equal?]
#:key [key values]
#:not-found [not-found #f])
original code,
but calling check-duplicates/t and check-duplicates/list
with extra argument not-found)

(define (check-duplicates/t items key table default)
(let loop ([items items])
(cond
((null? items)
default)
(else
(define key-item (key (car items)))
(cond
((hash-ref table key-item #f)
(car items))
(else
(hash-set! table key-item #t)
(loop (cdr items))))))))

(define (check-duplicates/list items key same? not-found)
body modified like in check-duplicates/t)

(define not-found (gensym))
(check-duplicates '(#f #f) #:not-found not-found) -> #f
(check-duplicates '(#f #t) #:not-found not-found) -> g14494
(check-duplicates '(#f #f) ) -> #f
(check-duplicates '(#f #t) ) -> #f

Another option can be to require (or allow) a thunk for argument not-found
(like in hash-ref),
to call this thunk when appropriate and to return the value(s) returned by
the thunk.

In addition an optional key-word-argument #:found [found values] can be
added
in order to return (found duplicate) when a duplicate is found.

Many more options are open.
One of the most difficult things when designing a program (or procedure or
whatever)
is to make decisions which options the design should offer
always keeping in mind what the users may or may not want (MHO)

Thanks again, Jos


-----Original Message-----
From: Matthias Felleisen [mailto:matt...@ccs.neu.edu]
Sent: viernes, 27 de mayo de 2016 1:24
To: Jos Koot
Cc: Racket Users
Subject: Re: [racket-users] check-duplicates


You can add a keyword argument that changes the not-found result for such a
case:

(define special (gensym))
(check-duplicates+ '(#f #f) #:not-found special)

Its signature would have to look like this then:

;; check-duplicates : (listof X)
;; [(K K -> bool)]
;; #:key (X -> K)
;; -> X or #f
(define (check-duplicates items
[same? equal?]
#:not-found [default #f]
#:key [key values])
.)

This way the existing interface works but someone who needs it for a list of
Booleans (or something else I can't think of right now) can get a distinct
answer.


[[ Racket's universal type lacks true option types, and we tend to
approximate it with (U #f X). ]]

- Matthias



> On May 26, 2016, at 4:35 PM, Jos Koot <jos....@gmail.com> wrote:
>
snip

Jos Koot

unread,
May 27, 2016, 2:47:51 PM5/27/16
to Matthias Felleisen, Racket Users
Thanks Matthias,
I found the source by means of 'open defining file'.
Brought me instantly to the definition.
I did not need a pull request on github.
Thanks,
Jos

-----Original Message-----
From: Matthias Felleisen [mailto:matt...@ccs.neu.edu]
Sent: viernes, 27 de mayo de 2016 19:42
To: Jos Koot
Cc: Racket Users
Subject: Re: [racket-users] check-duplicates


Open drracket, type in check-duplicates, and follow the right-click menu to
find the source. Then submit a pull request on github. - Matthias


snip

Matthias Felleisen

unread,
May 28, 2016, 8:56:21 AM5/28/16
to Jos Koot, Racket Users

On May 27, 2016, at 2:35 PM, Jos Koot <jos....@gmail.com> wrote:

Many more options are open.


Here is one you didn’t mention, going back to Julia Lawall's notion of “leakage”: 

pass in a success (sk) and failure (fk) continuation so that the “outside” 
does not have to recoup the decision that check-duplicates already made. 

— Mattihas

Jos Koot

unread,
May 28, 2016, 10:59:53 AM5/28/16
to Matthias Felleisen, Racket Users, Jos Koot
No, I did not mention that option, but it has crossed my mind.
At this moment I am reading Julia Lawall's notion of “leakage”.
Thanks, Jos.


From: Matthias Felleisen [mailto:matt...@ccs.neu.edu]
Sent: sábado, 28 de mayo de 2016 14:56

To: Jos Koot
Cc: Racket Users
Subject: Re: [racket-users] check-duplicates
Reply all
Reply to author
Forward
0 new messages