typing variable length argument lists

33 views
Skip to first unread message

Hendrik Boom

unread,
Apr 10, 2020, 7:49:14 PM4/10/20
to Racket Users
Trying to convert the following to typed Racket:

(define (unique list message . messageargs)
; return the only element of the list, or '() if there is none.
; Produce message if not just one.
(if (equal? 1 (length list)) (car list)
(begin
(apply fprintf (cons anomaly (cons message messageargs)))
(if (null? list) list (car list))
)
)
)

It's an error message function tat accepts a list to test.
If the test fails it uses fprintf to print a message with the
arguments for the ~s items.

I got so far, but I don't know what to do with the . messageargs :

(define (unique [list (Listof Any)] [message : String] . messageargs)
; return the only element of the list, or '() if there is none.
; Produce message if not just one.
(if (equal? 1 (length list)) (car list)
(begin
(apply fprintf (cons anomaly (cons message messageargs)))
(if (null? list) list (car list))
)
)
)

Jon Zeppieri

unread,
Apr 10, 2020, 8:18:53 PM4/10/20
to Racket Users
(define (unique [list : (Listof Any)] [message : String] .
[messageargs : Any *])
; return the only element of the list, or '() if there is none.
; Produce message if not just one.
(if (equal? 1 (length list)) (car list)
(begin
(apply fprintf anomaly message messageargs)
(if (null? list) list (car list)))))

> --
> 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.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/20200410234906.5wy5kgzl2qfwd2db%40topoi.pooq.com.

Hendrik Boom

unread,
Apr 11, 2020, 9:43:24 AM4/11/20
to Racket Users
On Fri, Apr 10, 2020 at 08:18:38PM -0400, Jon Zeppieri wrote:
> (define (unique [list : (Listof Any)] [message : String] .
> [messageargs : Any *])
> ; return the only element of the list, or '() if there is none.
> ; Produce message if not just one.
> (if (equal? 1 (length list)) (car list)
> (begin
> (apply fprintf anomaly message messageargs)
> (if (null? list) list (car list)))))

Thank you. That worked.
The change to the apply syntax surprised me.

-- hendrik

K H

unread,
Apr 13, 2020, 3:22:35 PM4/13/20
to Racket Users
If I understand correctly, the difference to getting the code to type check was:
>         (apply fprintf (cons anomaly (cons message messageargs)))

becomes:
>        (apply fprintf anomaly message messageargs)

Since the documentation explains that the arguments to fprintf are effectively gathered in a list*, could someone explain why the change makes a difference? Would it the answer depend on whether messagesargs was a list?.

Also, the first version with the cons's looks strange to me. Is there a reason why the statement was originally coded that way? Is it perhaps an artifact from attempting to force the result of list* to produce a list? regardless of what is the type of of the value passed in messageargs?

Thanks in advance,

Kieron


--
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.

Sam Tobin-Hochstadt

unread,
Apr 13, 2020, 3:32:33 PM4/13/20
to K H, Racket Users
The change makes a difference because Typed Racket's type inference
for `cons` is producing a less-accurate answer than it could. When it
sees arguments of type `String` and `(Listof Any)`, it decides that
the result is a `(Listof Any)` instead of a `(Pairof String (Listof
Any))`.

If you add (excessive) annotation like this:

(apply fprintf (ann (cons (current-output-port) (ann (cons
message messageargs) (Pairof String (Listof Any))))
(Pairof Output-Port (Pairof String (Listof Any)))))

then the expression typechecks as written.

However, I think the use of `apply` without the extra `cons` calls is
clearer anyway, in addition to being easier for the type checker.

Sam
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAEEP09AhmxLHeRdOpzpuG7qC9jOuPTrpog92G_3b6nJWwZDzbw%40mail.gmail.com.

K H

unread,
Apr 13, 2020, 4:31:14 PM4/13/20
to Sam Tobin-Hochstadt, Racket Users
Thanks Sam, for the explanation.

I was thinking that some information was lacking to the type inference system, and it helps to see the annotation incantations necessary to fill in the gap.

Cheers,

Kieron.
Reply all
Reply to author
Forward
0 new messages