[racket] integers, exact and inexact in working with split-at-right

From: Neil Toronto (neil.toronto at gmail.com)
Date: Sat Mar 15 16:10:33 EDT 2014

This came up so often when I was initially working on the math library 
that I defined `exact-round' and friends in `racket/math'. They have 
nicer types in TR than e.g. composing `inexact->exact' and `floor'.

Neil ⊥

On 03/15/2014 01:30 PM, Laurent wrote:
> `inexact->exact` is more general than some `float->integer`:
>  > (inexact->exact .75)
> 3/4
>
> I agree it is generally cumbersome to write `(inexact->exact (round <exp
> that returns a float but you want an int>))`, but it's sufficiently easy
> to write a wrapper by yourself. Although I wouldn't be against
> predefined some function that does something like that, but there are
> many ways to do things like that, so I guess that's the problem.
>
> Laurent
>
>
> On Sat, Mar 15, 2014 at 8:23 PM, Tom Dean <tld at google.com
> <mailto:tld at google.com>> wrote:
>
>     I encountered the following error — imagine a function that returns
>     3.0 instead of just 3.0 as shown example below:
>
>      > (split-at-right (range 8) 3.0)
>     split-at-right: contract violation
>        expected: exact-nonnegative-integer?
>        given: 3.0
>        context...:
>         /Applications/Racket/collects/racket/list.rkt:191:0: split-at-right
>         /Applications/Racket/collects/racket/private/misc.rkt:87:7
>
>     The obvious  fixes didn't work ...
>
>      > (split-at-right (range 8) (round 3.0))
>     split-at-right: contract violation
>        expected: exact-nonnegative-integer?
>        given: 3.0
>        context...:
>         /Applications/Racket/collects/racket/list.rkt:191:0: split-at-right
>         /Applications/Racket/collects/racket/private/misc.rkt:87:7
>      >
>
>     ... and I couldn't find something like real->integer, but
>     inexact->exact came to the rescue:
>
>      > (split-at-right (range 8) (inexact->exact 3.0))
>     (split-at-right (range 8) (inexact->exact 3.0))
>     '(0 1 2 3 4)
>     '(5 6 7)
>      > (split-at-right (range 8) 3.0)
>     split-at-right: contract violation
>        expected: exact-nonnegative-integer?
>        given: 3.0
>        context...:
>         /Applications/Racket/collects/racket/list.rkt:191:0: split-at-right
>         /Applications/Racket/collects/racket/private/misc.rkt:87:7
>      >
>
>     Still, it doesn't seem particularly elegant.
>
>
>     ____________________
>        Racket Users list:
>     http://lists.racket-lang.org/users
>
>
>
>
> ____________________
>    Racket Users list:
>    http://lists.racket-lang.org/users
>


Posted on the users mailing list.