[racket] number->string in the HtDP student languages

From: Alexis King (lexi.lambda at gmail.com)
Date: Wed Feb 18 03:32:50 EST 2015

This is interesting behavior. To explain why it happens, this has something to do with how the HtDP languages handle exact/inexact numbers. For example, in plain Racket, (exact? 0.75) is #f, but in BSL, it is #true. In BSL, numbers are exact unless otherwise specified. Note that explicitly using an exactness prefix in plain Racket, #e0.75, produces 3/4.

What’s odd is that the convention for printing numbers in BSL is different from the convention used by number->string. BSL, unlike Racket, prints exact numbers as decimals. I’d expect that this is to avoid confusion when students type in decimals only to have them printed in large fractions. Unfortunately, it would seem that number->string simply uses the plain Racket implementation, which lets the odd implementations details “leak through” the abstraction.

It does feel like poor behavior to me. I’d expect number->string to follow the same printing conventions BSL uses.

> On Feb 18, 2015, at 00:17, Breanndán Ó Nualláin <bon at science.uva.nl> wrote:
> 
> In BSL, I just got caught out by this behaviour:
> 
>> (number->string 0.75)
>  "3/4"
> 
> I expected the same behaviour as in the full Racket language:
> 
>> (number->string 0.75)
>  "0.75"
> 
> What's the thinking behind this? Wouldn't the decimal notation be more intuitive, particularly in cases like this?
> 
>> (number->string 0.7555555)
>  "1511111/2000000"
> 
> And especially in cases like this?
> 
>> (number->string (string->number (number->string pi)))
>  "3141592653589793/1000000000000000"
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20150218/6f5bc68b/attachment.html>

Posted on the users mailing list.