[plt-scheme] for comprehensions in typed-scheme

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Jun 8 10:18:06 EDT 2009

Noel, Sam is trying to imitate ML's bad type error messages and I am  
afraid he's succeeding all too well. Sorry.

In your case, you need to read the error message as

  max consumes at least one number and a possible empty sequence of  
additional numbers.

You,

  the programmer, is trying to make max compute the maximum of a list  
of numbers, via apply.
  But silly, don't you know that lists can be empty?!!

Here is another way of saying this:

> #lang typed-scheme
>
> (: f ((Listof Number) -> Number))
> (define (f y)
>   (define x (apply max (car y) (cdr y)))
>   x)

Of course note that (car y) does NOT signal a type error. The typed  
language has turned the potential error of computing the max from a  
compile time type error into a run-time check/error -- QUIETLY.  
Aren't typed languages beautiful?




On Jun 8, 2009, at 9:54 AM, Noel Welsh wrote:

> On Mon, Jun 8, 2009 at 1:45 PM, Sam TH<samth at ccs.neu.edu> wrote:
>> This works:
>
> Groovy.
>
> Next dumb question:
>
> (define-struct: Mark ())
> (define-struct: (Dot Mark) ([x : Number] [y : Number]))
>
> (: bounding-box ((Listof Dot) -> Box))
> (define (bounding-box marks)
>   ;; This only works for Dots. It should work for all Marks
>   (define: xs : (Listof Number) (map Dot-x marks))
>   (define: ys : (Listof Number) (map Dot-y marks))
>   (define: max-x : Number (#{apply @ Number Number} max xs))
>   (define: max-y : Number (apply max ys))
>   (define: min-x : Number (apply min xs))
>   (define: min-y : Number (apply min ys))
>   ...
> )
>
> All the calls to apply fail with
>
> typecheck: Bad arguments to function in apply:
> Domain: Number Number *
> Arguments: (Listof Number) *
>
> I don't get it! At the REPL I can do:
>
>   (apply max (list 1 2 3 4))
>
> and it works. My expressions above seem equivalent. What must I do to
> get this past the type checker?
>
> N.
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.