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

From: Laurent (laurent.orseau at gmail.com)
Date: Sat Mar 15 15:30:09 EDT 2014

`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> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140315/01485c01/attachment-0001.html>

Posted on the users mailing list.