Re: [racket-users] confusion about real vs not real zero

2021-11-19 Thread Alain De Vos
Check if the distance from (0,0) is "small enough" ?
 
On Friday, November 19, 2021 at 9:32:23 PM UTC+1 Stephen Chang wrote:

> I dont want to fully support complex numbers. I just want to do the
> minimum so that programs that dont use them are not blocked by the
> lack of support
>
> On Fri, Nov 19, 2021 at 3:30 PM Sorawee Porncharoenwase
>  wrote:
> >
> > Yeah, I was about to reply that I don't think there's a workaround, too.
> >
> > What is your goal, though? Do you intend to support complex numbers 
> properly right now? In particular, that problematic code is random 
> generation from contracts, which is rarely invoked anyway. Intuitively, 
> there's no reason why the complex number feature is required to get the 
> code running.
> >
> > So one potential solution is to not support complex numbers right now, 
> and compile all complex number literals to a JS expression that throws an 
> exception at runtime.
> >
> >
> >
> > On Fri, Nov 19, 2021 at 12:20 PM Sam Tobin-Hochstadt  
> wrote:
> >>
> >> On Fri, Nov 19, 2021 at 3:13 PM Stephen Chang  
> wrote:
> >> >
> >> > Lol I read that page and still didn't get it.
> >> >
> >> > Any opinion for a potential workaround?
> >>
> >> It depends what you mean by "workaround". The distinction between
> >> exact and inexact numbers is pretty deeply built-in to how Racket
> >> numbers work, so there's not going to be a simple workaround that
> >> fixes this issue.
> >>
> >> For RacketScript I think the choices are (a) use floats for everything
> >> and have semantics that diverge substantially from Racket or (b) have
> >> a separate implementation of integers that's not JS numbers (maybe JS
> >> bigints would work).
> >>
> >> Sam
> >>
> >> >
> >> > On Fri, Nov 19, 2021 at 3:08 PM Sorawee Porncharoenwase
> >> >  wrote:
> >> > >
> >> > > I had this exact same question when I looked at the RacketScript 
> issue lol.
> >> > >
> >> > > The answer is https://docs.racket-lang.org/reference/numbers.html:
> >> > >
> >> > > a complex number with an exact zero imaginary part is a real number.
> >> > >
> >> > > Since 0.0 is not exact, 0.0i is not a real number.
> >> > >
> >> > > On Fri, Nov 19, 2021 at 11:59 AM Stephen Chang  
> wrote:
> >> > >>
> >> > >> In the following, why is the first considered a real number but the
> >> > >> second considered not real
> >> > >>
> >> > >> > (real? 0.0+0i)
> >> > >> #t
> >> > >> > (real? 0.0+0.0i)
> >> > >> #f
> >> > >>
> >> > >> --
> >> > >> 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/CAFfiA1%2BYygGrLH2rtwby8AWg7Edyvq-tzmANTNypq5Rqd-eXFw%40mail.gmail.com
> .
> >> >
> >> > --
> >> > 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/CAFfiA1%2B3yRzzh%3DKhcOddt0geMezNsxQGHqzwGTYbZjLekUQ8kQ%40mail.gmail.com
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/81f38508-f1cf-4e86-a1a9-a37924074553n%40googlegroups.com.


[racket-users] Re: Questions on a very simple class

2020-02-15 Thread Alain De Vos
For the record, this worked:
#lang typed/racket
(require typed/racket/class)
(define aninteger%
  (class object%
(super-new)
(init-field [x : Integer 0])
(: getxinternal Integer)
   (define getxinternal x)
(: getx (-> Integer))
   (define/public (getx) getxinternal)))
(print (send (new aninteger% [x 2]) getx))




On Sunday, February 16, 2020 at 12:18:53 AM UTC+1, Alain De Vos wrote:
>
>
> Following code makes an "integerclass" with an "add" method :
>
> #lang racket
> (define (integerclass x)
>   (define (getx)  x)
>   (define (setx! x_new) (set! x x_new))
>   (define (add y)(integerclass (+ x (y 'getx
>   (lambda (message . args)
> (case message
> ((getx)  (apply getxargs))
> ((setx!) (apply setx!   args))
> ((add)   (apply add args))
>   (else (error "POINT: Unknown message ->" message)
>
> (define (myprint x) (print x)(newline))
> (define p1 (integerclass 1))
> (myprint (p1 'getx))
> (define p2 (integerclass 2))
> (myprint (p2 'getx))
> (define p3 (p1 'add p2))
> (myprint (p3 'getx))
>
> Question 1 : can this code be inproved, are there "better patterns" one 
> can you.
> Question 2 : can i modify this code to use typed/racket. I tried but 
> failed on the "apply" method in the code.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/3c99b587-b85a-49e5-9f7c-792a991077b4%40googlegroups.com.


Re: [racket-users] Questions on a very simple class

2020-02-15 Thread Alain De Vos
When i try typed/racket :
#lang typed/racket
(define (integerclass x)
  (define (getx) x)
  (define (setx! [x_new : Integer]) (set! x x_new))
  (define (add [y : integerclass]) : integerclass (integerclass (+ 1 (y 
'getx
  (lambda (message . args)
(case message
((getx)  (apply getxargs))
((setx!) (apply setx!   args))
((add)   (apply add args))
  (else (error "POINT: Unknown message ->" message)

(define (myprint x) (print x)(newline))
(define p1 (integerclass 1))
(myprint (p1 'getx))
(define p2 (integerclass 2))
(myprint (p2 'getx))
(define p3 (p1 'add p2))
(myprint (p3 'getx))

I receive the errors,
On the line:
(define (add [y : integerclass]) : integerclass (integerclass (+ x (y 
'getx
4 errors :
Type Checker: parse error in type; type name `integerclass' is unbound in: 
integerclass
Type Checker: parse error in type; type name `integerclass' is unbound in: 
integerclass
Type Checker: missing type for identifier;consider adding a type annotation 
with `:' identifier: integerclass in: integerclass
Type Checker: type mismatch expected: Number given: Any in: x

On the line : 
((getx)  (apply getxargs))
Error :Type Checker: Bad arguments to function in `apply': Domain:  
Arguments:  (Listof Any)
Other apply lines produce same errors.

On Sunday, February 16, 2020 at 12:25:41 AM UTC+1, Sage Gerard wrote:
>
> 1. If the intention is to create a class, then I'd use the class form.
>
>
> https://docs.racket-lang.org/reference/createclass.html#%28form._%28%28lib._racket%2Fprivate%2Fclass-internal..rkt%29._class%2A%29%29
>
> Not that there's anything overtly wrong with using a closure, but common 
> validation tasks re: inheritance, overriding, etc. all have to be done by 
> hand here.
>
> 2. What specific error message did you get?
>
> *~slg*
>
>
> ‐‐‐ Original Message ‐‐‐
> On Saturday, February 15, 2020 6:18 PM, Alain De Vos  > wrote:
>
>
> Following code makes an "integerclass" with an "add" method :
>
> #lang racket
> (define (integerclass x)
>   (define (getx)  x)
>   (define (setx! x_new) (set! x x_new))
>   (define (add y)(integerclass (+ x (y 'getx
>   (lambda (message . args)
> (case message
> ((getx)  (apply getxargs))
> ((setx!) (apply setx!   args))
> ((add)   (apply add args))
>   (else (error "POINT: Unknown message ->" message)
>
> (define (myprint x) (print x)(newline))
> (define p1 (integerclass 1))
> (myprint (p1 'getx))
> (define p2 (integerclass 2))
> (myprint (p2 'getx))
> (define p3 (p1 'add p2))
> (myprint (p3 'getx))
>
> Question 1 : can this code be inproved, are there "better patterns" one 
> can you.
> Question 2 : can i modify this code to use typed/racket. I tried but 
> failed on the "apply" method in the code.
>
>
> --
> 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...@googlegroups.com .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/racket-users/c28892b8-e49b-4257-9ebe-4560943e87a7%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/racket-users/c28892b8-e49b-4257-9ebe-4560943e87a7%40googlegroups.com?utm_medium=email_source=footer>
> .
>
>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/644612e1-ee15-4acb-b2b9-ed8c94f85f86%40googlegroups.com.


[racket-users] Questions on a very simple class

2020-02-15 Thread Alain De Vos

Following code makes an "integerclass" with an "add" method :

#lang racket
(define (integerclass x)
  (define (getx)  x)
  (define (setx! x_new) (set! x x_new))
  (define (add y)(integerclass (+ x (y 'getx
  (lambda (message . args)
(case message
((getx)  (apply getxargs))
((setx!) (apply setx!   args))
((add)   (apply add args))
  (else (error "POINT: Unknown message ->" message)

(define (myprint x) (print x)(newline))
(define p1 (integerclass 1))
(myprint (p1 'getx))
(define p2 (integerclass 2))
(myprint (p2 'getx))
(define p3 (p1 'add p2))
(myprint (p3 'getx))

Question 1 : can this code be inproved, are there "better patterns" one can 
you.
Question 2 : can i modify this code to use typed/racket. I tried but failed 
on the "apply" method in the code.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/c28892b8-e49b-4257-9ebe-4560943e87a7%40googlegroups.com.


[racket-users] Re: How to convert String to Integer

2020-02-12 Thread Alain De Vos
I came to the following result as conversion function :

#lang typed/racket
(: string2value (-> String Integer))
(define (string2value astring)
  (define (fun [val : Char] [res : Integer])
(+ (* 10 res) (- (char->integer val) 48)))
  (foldl fun 0 (string->list astring))
)
(print (string2value "12345"))



On Tuesday, February 11, 2020 at 11:34:16 AM UTC+1, Alain De Vos wrote:
>
> I tried the following function to conver a String to an Integer.
>
> #lang typed/racket
> (: myconversion (-> String Integer))
> (define (myconversion str)
>   (string->number str))
>
> The error given is :
> Type Checker: type mismatch
>   expected: Integer
>   given: (U Complex False) in: (string->number str)
>
> I guess this is because a number is not an Integer.
>
> How to proceed?
>
> I found the following code on internet , but this looks like a real 
> overkill for this simple problem ,
>
> (: numerical-char->integer (-> Char
>Integer))(define (numerical-char->integer char)
>   (let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
> (if
>  (or (< num 0) (> num 9))
>  (raise 'non-numerical-char #t)
>  num)))
> (: string->integer (-> String
>Integer))(define (string->integer str)
>   (let ([char-list (string->list str)])
> (if (null? char-list)
> (raise 'empty-string #t)
> (foldl
>  (λ([x : Integer] [y : Integer])
>(+ (* y 10) x))
>  0
>  (map numerical-char->integer char-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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/3e58a927-c05a-4688-984c-1750fb014624%40googlegroups.com.


[racket-users] Re: How to convert String to Integer

2020-02-11 Thread Alain De Vos
In C I would would do some very simple pointer arithmetic, but racket 
leaves me into the blue.
Documentation, which is fine, compared to other lisps, fyi chez, leaves me 
into the blue.
0) Given a string, 
1) convert to  a list of characters, 
2) allow me to iterate, 
3) convert a character to an int , 
4) subtract corresponding value of zero, 
5) allow me to some positional stuff and addition.
But first i need a list of characters. :)
Does the language has a conversion operator ?

On Tuesday, February 11, 2020 at 8:50:33 PM UTC+1, Alain De Vos wrote:
>
> Very basic question, first step first, how do i convert a string astring 
> to a list of characters,
> #lang typed/racket
> (require typed/racket/gui)
> (: astring String)
> (define astring "1234567890")
>
>
> On Tuesday, February 11, 2020 at 11:34:16 AM UTC+1, Alain De Vos wrote:
>>
>> I tried the following function to conver a String to an Integer.
>>
>> #lang typed/racket
>> (: myconversion (-> String Integer))
>> (define (myconversion str)
>>   (string->number str))
>>
>> The error given is :
>> Type Checker: type mismatch
>>   expected: Integer
>>   given: (U Complex False) in: (string->number str)
>>
>> I guess this is because a number is not an Integer.
>>
>> How to proceed?
>>
>> I found the following code on internet , but this looks like a real 
>> overkill for this simple problem ,
>>
>> (: numerical-char->integer (-> Char
>>Integer))(define (numerical-char->integer 
>> char)
>>   (let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
>> (if
>>  (or (< num 0) (> num 9))
>>  (raise 'non-numerical-char #t)
>>  num)))
>> (: string->integer (-> String
>>Integer))(define (string->integer str)
>>   (let ([char-list (string->list str)])
>> (if (null? char-list)
>> (raise 'empty-string #t)
>> (foldl
>>  (λ([x : Integer] [y : Integer])
>>(+ (* y 10) x))
>>  0
>>  (map numerical-char->integer char-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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/54b2884c-6d93-44ad-b34a-d68c110b73ec%40googlegroups.com.


[racket-users] Re: How to convert String to Integer

2020-02-11 Thread Alain De Vos
Very basic question, first step first, how do i convert a string astring to 
a list of characters,
#lang typed/racket
(require typed/racket/gui)
(: astring String)
(define astring "1234567890")


On Tuesday, February 11, 2020 at 11:34:16 AM UTC+1, Alain De Vos wrote:
>
> I tried the following function to conver a String to an Integer.
>
> #lang typed/racket
> (: myconversion (-> String Integer))
> (define (myconversion str)
>   (string->number str))
>
> The error given is :
> Type Checker: type mismatch
>   expected: Integer
>   given: (U Complex False) in: (string->number str)
>
> I guess this is because a number is not an Integer.
>
> How to proceed?
>
> I found the following code on internet , but this looks like a real 
> overkill for this simple problem ,
>
> (: numerical-char->integer (-> Char
>Integer))(define (numerical-char->integer char)
>   (let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
> (if
>  (or (< num 0) (> num 9))
>  (raise 'non-numerical-char #t)
>  num)))
> (: string->integer (-> String
>Integer))(define (string->integer str)
>   (let ([char-list (string->list str)])
> (if (null? char-list)
> (raise 'empty-string #t)
> (foldl
>  (λ([x : Integer] [y : Integer])
>(+ (* y 10) x))
>  0
>  (map numerical-char->integer char-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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/2e17ed2c-e1f1-4b56-985c-4202665db250%40googlegroups.com.


Re: [racket-users] How to convert String to Integer

2020-02-11 Thread Alain De Vos
Philip,
For the last part,
I will make my work through,
convert a string to collection/list of characters,
this to a collection/list of numbers with a value 48 subtracted i think,
this to the correct end value base10.
I have no complex numbers to deal with.
But at each step I should raise , this is not ok.
Otherwise the GUI just keeps eating memory ...

On Tuesday, February 11, 2020 at 8:09:31 PM UTC+1, Alain De Vos wrote:
>
>
>
> On Tuesday, February 11, 2020 at 5:33:37 PM UTC+1, Philip McGrath wrote:
>>
>> Others have tried to be more Socratic in pointing you this way, but 
>> here's an attempt at being more explicit.
>>
>> As you note, the result of `string->number` has the type `(U Complex 
>> False)`. If we try to think about about this in a version of the HtDP 
>> design recipe, we have a few cases:
>>
>>1. `string->number` produces an `Integer` when given an argument like 
>>`"42"`.
>>2. `string->number` can also produce a value of any of the various 
>>`Number` types that aren't integers, as with `"3.2+6.0i"` or `"2/3"`.
>>3. If the given string can't be parsed as any kind of number, like 
>>`"apple"`, `string->number` returns `#false`.
>>
>> If you want to write a function with the type `(-> String Integer)`, you 
>> are going to have to handle all of those cases, whether you use 
>> `string->number` or not! What behavior makes sense in cases 2 and 3 (or 
>> maybe it makes sense to break 2 into smaller cases) is going to depend on 
>> your particular use case. Maybe it makes sense to return a default value. 
>> Maybe you just want to raise an exception.
>>
>> If we write a version of your `myconversion` function with a placeholder 
>> (again in the spirit of HtDP), we might do something like this:
>> #lang typed/racket
>> (: myconversion (-> String Integer))
>> (define (myconversion str)
>>   (define rslt (string->number str))
>>   (cond
>> [(exact-integer? rslt)
>>  rslt]
>> [else
>>  ;; TODO
>>  ...]))
>>
>> This is where Ben's suggestion of `exact-integer?` is useful: thanks to 
>> Typed Racket's occurrence typing 
>> <https://docs.racket-lang.org/ts-guide/occurrence-typing.html>, the type 
>> system knows that, in the "then" branch of the conditional, `rslt` must 
>> have the type `Integer` because it satisfied the `exact-integer?` test. 
>> (Note that the `Integer` type corresponds to `exact-integer?` 
>> <https://docs.racket-lang.org/ts-reference/type-ref.html#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types..rkt%29._.Integer%29%29>,
>>  
>> not `integer?`.)
>>
>> We still need to fill in the "else" branch to handle the case that the 
>> string didn't represent an integer. In your latter example, you raised an 
>> exception, which is a sensible choice. Here's an idiomatic way to do that:
>> #lang typed/racket
>> (: myconversion (-> String Integer))
>> (define (myconversion str)
>>   (define rslt (string->number str))
>>   (cond
>> [(exact-integer? rslt)
>>  rslt]
>> [else
>>  (raise-argument-error 'myconversion
>>"a string representing an integer"
>>str)]))
>>
>> One thing to observe is that this is exactly the way you would have 
>> implemented such a function in untyped `#lang racket`: only the single type 
>> annotation is different. Much of the reason is that Typed Racket works hard 
>> through features like occurrence typing to be able to typecheck the kinds 
>> of programs you would idiomatically write in untyped Racket. In this 
>> particular case, though, it also reflects the fact that there isn't a type 
>> for "strings that can be parsed as integers." There's a potential for 
>> dynamic failure here that the static type system may help you to manage, 
>> but ultimately can't escape. (Ignoring for the moment fancier type systems 
>> that are mostly still research projects.)
>>
>> Your problem with `sweet-exp` is a different one. I'm not familiar with 
>> `sweet-exp`, so I can't really help you, but it looks like either a bug or 
>> a known limitation in the concrete syntax `sweet-exp` supports. I will note 
>> that the lambda function you use in that example would be a syntax error in 
>> `#lang racket`.
>>
>> -Philip
>>
>>
>> On Tue, Feb 11, 2020 at 10:04 AM Alain De Vos  
>> wrote:
>>
>>> No exact-integer is a check it is

Re: [racket-users] How to convert String to Integer

2020-02-11 Thread Alain De Vos


On Tuesday, February 11, 2020 at 5:33:37 PM UTC+1, Philip McGrath wrote:
>
> Others have tried to be more Socratic in pointing you this way, but here's 
> an attempt at being more explicit.
>
> As you note, the result of `string->number` has the type `(U Complex 
> False)`. If we try to think about about this in a version of the HtDP 
> design recipe, we have a few cases:
>
>1. `string->number` produces an `Integer` when given an argument like 
>`"42"`.
>2. `string->number` can also produce a value of any of the various 
>`Number` types that aren't integers, as with `"3.2+6.0i"` or `"2/3"`.
>3. If the given string can't be parsed as any kind of number, like 
>`"apple"`, `string->number` returns `#false`.
>
> If you want to write a function with the type `(-> String Integer)`, you 
> are going to have to handle all of those cases, whether you use 
> `string->number` or not! What behavior makes sense in cases 2 and 3 (or 
> maybe it makes sense to break 2 into smaller cases) is going to depend on 
> your particular use case. Maybe it makes sense to return a default value. 
> Maybe you just want to raise an exception.
>
> If we write a version of your `myconversion` function with a placeholder 
> (again in the spirit of HtDP), we might do something like this:
> #lang typed/racket
> (: myconversion (-> String Integer))
> (define (myconversion str)
>   (define rslt (string->number str))
>   (cond
> [(exact-integer? rslt)
>  rslt]
> [else
>  ;; TODO
>  ...]))
>
> This is where Ben's suggestion of `exact-integer?` is useful: thanks to 
> Typed Racket's occurrence typing 
> <https://docs.racket-lang.org/ts-guide/occurrence-typing.html>, the type 
> system knows that, in the "then" branch of the conditional, `rslt` must 
> have the type `Integer` because it satisfied the `exact-integer?` test. 
> (Note that the `Integer` type corresponds to `exact-integer?` 
> <https://docs.racket-lang.org/ts-reference/type-ref.html#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types..rkt%29._.Integer%29%29>,
>  
> not `integer?`.)
>
> We still need to fill in the "else" branch to handle the case that the 
> string didn't represent an integer. In your latter example, you raised an 
> exception, which is a sensible choice. Here's an idiomatic way to do that:
> #lang typed/racket
> (: myconversion (-> String Integer))
> (define (myconversion str)
>   (define rslt (string->number str))
>   (cond
> [(exact-integer? rslt)
>  rslt]
> [else
>  (raise-argument-error 'myconversion
>"a string representing an integer"
>str)]))
>
> One thing to observe is that this is exactly the way you would have 
> implemented such a function in untyped `#lang racket`: only the single type 
> annotation is different. Much of the reason is that Typed Racket works hard 
> through features like occurrence typing to be able to typecheck the kinds 
> of programs you would idiomatically write in untyped Racket. In this 
> particular case, though, it also reflects the fact that there isn't a type 
> for "strings that can be parsed as integers." There's a potential for 
> dynamic failure here that the static type system may help you to manage, 
> but ultimately can't escape. (Ignoring for the moment fancier type systems 
> that are mostly still research projects.)
>
> Your problem with `sweet-exp` is a different one. I'm not familiar with 
> `sweet-exp`, so I can't really help you, but it looks like either a bug or 
> a known limitation in the concrete syntax `sweet-exp` supports. I will note 
> that the lambda function you use in that example would be a syntax error in 
> `#lang racket`.
>
> -Philip
>
>
> On Tue, Feb 11, 2020 at 10:04 AM Alain De Vos  > wrote:
>
>> No exact-integer is a check it is not a type.
>> The error described above remains
>>
>> On Tuesday, February 11, 2020 at 3:50:27 PM UTC+1, Alain De Vos wrote:
>>>
>>>
>>>
>>> On Tuesday, February 11, 2020 at 3:25:42 PM UTC+1, Ben Greenman wrote:
>>>>
>>>> You may want `exact-integer?` 
>>>> True , i should use exact-integer.
>>>>
>>> -- 
>> 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...@googlegroups.com .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/racket-users/89

Re: [racket-users] How to convert String to Integer

2020-02-11 Thread Alain De Vos
No exact-integer is a check it is not a type.
The error described above remains

On Tuesday, February 11, 2020 at 3:50:27 PM UTC+1, Alain De Vos wrote:
>
>
>
> On Tuesday, February 11, 2020 at 3:25:42 PM UTC+1, Ben Greenman wrote:
>>
>> You may want `exact-integer?` 
>> True , i should use exact-integer.
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/89f338be-f574-44b5-82d4-23f833ec14ac%40googlegroups.com.


Re: [racket-users] How to convert String to Integer

2020-02-11 Thread Alain De Vos


On Tuesday, February 11, 2020 at 3:25:42 PM UTC+1, Ben Greenman wrote:
>
> You may want `exact-integer?` 
> True , i should use exact-integer.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/75efb7ac-ab10-4b29-ba9a-c546d7cb7b47%40googlegroups.com.


Re: [racket-users] How to convert String to Integer

2020-02-11 Thread Alain De Vos
Indeed good question, but the following code works,

#lang typed/racket
(require typed/racket/gui)

(: fib (-> Integer Integer))
(define (fib n)
  (if (< 2 n)
  n
  (+ (fib (- n 1))
 (fib (- n 2)

(: numericalchar2integer (-> Char Integer))
(define (numericalchar2integer char)
  (let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
(if
 (or (< num 0) (> num 9))
 (raise 'non-numerical-char #t)
 num)))

(: string2integer (-> String Integer))
(define (string2integer str)
  (let ([char-list (string->list str)])
(if (null? char-list)
(raise 'empty-string #t)
(foldl
 (λ([x : Integer] [y : Integer])
   (+ (* y 10) x))
 0
 (map numericalchar2integer char-list)

And the following code fails , just one the first line difference,
#lang 
<https://docs.racket-lang.org/guide/Module_Syntax.html#%28part._hash-lang%29>
 sweet-exp <https://docs.racket-lang.org/sweet/index.html> typed/racket
(: fib (-> Integer Integer))
(define (fib n)
  (if (< 2 n)
  n
  (+ (fib (- n 1))
 (fib (- n 2)

(: numericalchar2integer (-> Char Integer))
(define (numericalchar2integer char)
  (let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
(if
 (or (< num 0) (> num 9))
 (raise 'non-numerical-char #t)
 num)))

(: string2integer (-> String Integer))
(define (string2integer str)
  (let ([char-list (string->list str)])
(if (null? char-list)
(raise 'empty-string #t)
(foldl
 (λ([x : Integer] [y : Integer])
   (+ (* y 10) x))
 0
 (map numericalchar2integer char-list)

Error, unbound identifier y.
Has sweet-exp a problem with lambda functions ?




On Tuesday, February 11, 2020 at 12:02:46 PM UTC+1, Ryan Culpepper wrote:
>
> What should `(myconversion "apple")` return?
> What should `(myconversion "12.3")` return?
> What does `string->number` do in each of those cases?
>
> Ryan
>
>
> On Tue, Feb 11, 2020 at 11:34 AM Alain De Vos  > wrote:
>
>> I tried the following function to conver a String to an Integer.
>>
>> #lang typed/racket
>> (: myconversion (-> String Integer))
>> (define (myconversion str)
>>   (string->number str))
>>
>> The error given is :
>> Type Checker: type mismatch
>>   expected: Integer
>>   given: (U Complex False) in: (string->number str)
>>
>> I guess this is because a number is not an Integer.
>>
>> How to proceed?
>>
>> I found the following code on internet , but this looks like a real 
>> overkill for this simple problem ,
>>
>> (: numerical-char->integer (-> Char
>>Integer))(define (numerical-char->integer 
>> char)
>>   (let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
>> (if
>>  (or (< num 0) (> num 9))
>>  (raise 'non-numerical-char #t)
>>  num)))
>> (: string->integer (-> String
>>Integer))(define (string->integer str)
>>   (let ([char-list (string->list str)])
>> (if (null? char-list)
>> (raise 'empty-string #t)
>> (foldl
>>  (λ([x : Integer] [y : Integer])
>>(+ (* y 10) x))
>>  0
>>  (map numerical-char->integer char-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...@googlegroups.com .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/racket-users/4a13bb2e-3107-4c5f-aee6-6d29e493c040%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/racket-users/4a13bb2e-3107-4c5f-aee6-6d29e493c040%40googlegroups.com?utm_medium=email_source=footer>
>> .
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/97f234f9-8fc1-4b22-9581-8707a6b006d5%40googlegroups.com.


[racket-users] Re: How to convert String to Integer

2020-02-11 Thread Alain De Vos
Or is it they idea to write own conversion functions to learn the language.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/fb73a1b4-3aa1-419a-8167-588ac4473316%40googlegroups.com.


[racket-users] How to convert String to Integer

2020-02-11 Thread Alain De Vos
I tried the following function to conver a String to an Integer.

#lang typed/racket
(: myconversion (-> String Integer))
(define (myconversion str)
  (string->number str))

The error given is :
Type Checker: type mismatch
  expected: Integer
  given: (U Complex False) in: (string->number str)

I guess this is because a number is not an Integer.

How to proceed?

I found the following code on internet , but this looks like a real 
overkill for this simple problem ,

(: numerical-char->integer (-> Char
   Integer))(define (numerical-char->integer char)
  (let ([num (- (char->integer char) 48)]) ; 48 = (char->integer #\0)
(if
 (or (< num 0) (> num 9))
 (raise 'non-numerical-char #t)
 num)))
(: string->integer (-> String
   Integer))(define (string->integer str)
  (let ([char-list (string->list str)])
(if (null? char-list)
(raise 'empty-string #t)
(foldl
 (λ([x : Integer] [y : Integer])
   (+ (* y 10) x))
 0
 (map numerical-char->integer char-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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/4a13bb2e-3107-4c5f-aee6-6d29e493c040%40googlegroups.com.


[racket-users] Mixing different #lang in one application

2020-02-10 Thread Alain De Vos
I want to create a gui app.
But then I need:
#lang racket/gui
But when i click a button i want type control in the functions so i need :
#lang typed/racket
Are there solutions ?

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/4b579c9d-b9d0-4d8b-9d60-5be31d6449fd%40googlegroups.com.