I've recently wanted a contract that lets me check the last element of a 
(potentially improper) list, ignoring the other elements. To do this in a 
general sense, I came up with this.

(define (listof* init/c last/c)
  (flat-named-contract
   `(listof* ,(contract-name init/c) ,(contract-name last/c))
   (flat-rec-contract listof* last/c (cons/c init/c listof*))))

That definition can then be used like this.

(define/contract num-tail
  (listof* any/c number?)
  '(a b c . 4))

The use-case I've found most helpful, though, is combining it with listof 
contracts to check sequences within lists or at the end of the lists.

(define/contract num-tail-proper
  (listof* any/c (list/c number?))
  '(a b c 4))

This works, but it only works with flat contracts. Would it be possible to 
generalize this to work with all contracts? Also, would this sort of combinator 
make sense in the standard library? The name I've used probably wouldn't be 
usable (since it could be confused with list*of), but I think the functionality 
is helpful.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to