[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
>  
> 
> .
>
>
>

-- 
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.


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

2020-02-15 Thread Sage Gerard
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-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](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/gUC9HzrnJWNGbe-kgcoyov8KOUFY7sKw4KNu2CfqFABrsW0dsGpLsPfuKAHmcoXMBFECT4Bln1wQMYhpOFhL_y4WjYY-RWnveapTNESXHoc%3D%40sagegerard.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.


Re: [racket-users] cast on mutable hash table...

2020-02-15 Thread 'John Clements' via users-redirect
Yes, I’ll add this. It slightly increases the pain density, especially since I 
think students likely to mistakenly write

(list (? symbol? #{s : Symbol}) …)

instead of the correct

(list (? symbol? #{s : (Listof Symbol)}) …)

… but it’s probably better than having to cast.

Thanks!

John


> On Feb 15, 2020, at 10:33, Sam Tobin-Hochstadt  wrote:
> 
> You can annotate variables like that in match patterns using #{s : Symbol}, 
> which ought to work in common cases. 
> 
> On Sat, Feb 15, 2020, 1:23 PM 'John Clements' via users-redirect 
>  wrote:
> Yes, absolutely. One reason that students in my class wind up using cast 
> quite frequently in their parsers is that they use patterns like (list (? 
> symbol s) …) which (as I recall) expand into unannotated lambda’s, and always 
> require a cast. I write that up here:
> 
> https://www.brinckerhoff.org/clements/2202-csc430/Assignments/tr-notes.html#%28part._.Predicate_.Patterns_with_.Dot-dot-dot%29
> 
> Beyond that, though, I tell them never to use cast:
> 
> https://www.brinckerhoff.org/clements/2202-csc430/Assignments/tr-notes.html#%28part._.Other_.Casting%29
> 
> The text I came up with for mutable hash tables is here:
> 
> https://www.brinckerhoff.org/clements/2202-csc430/Assignments/tr-notes.html#%28part._.Mutable_.Hash_.Table_.Construction%29
> 
> Many thanks to all of you!
> 
> John
> 
> > On Feb 14, 2020, at 12:32, Sam Tobin-Hochstadt  wrote:
> > 
> > The advice I would really give is to give a name and a type annotation to 
> > any mutable data structure you create. 
> > 
> > Also, cast is almost never what you want. I see lots of people (maybe 
> > including this student) using it as a substitute for type annotation but it 
> > really isn't. 
> > 
> > Sam
> > 
> > On Fri, Feb 14, 2020, 2:41 PM 'John Clements' via users-redirect 
> >  wrote:
> > I think I may understand what’s going on here, but a student and I worked 
> > on this for quite a while today before I found the problem.
> > 
> > Here’s a program:
> > 
> > #lang typed/racket
> > 
> > (define-type Store (Mutable-HashTable Integer Value))
> > (define-type Value (U Real Boolean String))
> > 
> > (define top-store (cast (make-hash (list
> > (cons -1 14)
> > (cons 1 #t)
> > (cons 2 #f)))
> > Store))
> > 
> > (hash-set! top-store 5 1234)
> > 
> > It fails with this error:
> > 
> > contract violation
> >   expected: (or/c (and/c byte? positive?) #t #f)
> >   given: 1234
> >   in: the values of
> >   the 3rd conjunct of
> >   (and/c
> >hash?
> >hash-mutable?
> >(hash/c
> > exact-integer?
> > (or/c (and/c byte? positive?) #t #f)
> > #:immutable
> > #f))
> >   contract from: typed-world
> >   blaming: cast
> >(assuming the contract is correct)
> >   at: unsaved-editor:6.18
> > 
> > If I understand what’s going on here, the basic issue is that the mutable 
> > hash table’s type is being inferred as (Immutable-HashTable Exact-Integer 
> > (U Boolean Positive-Byte)), and then as part of the cast, a contract is 
> > being inserted, which checks that all added values match the expected value 
> > type. The outer cast allows type checking to proceed, but then at runtime 
> > it fails because the given value doesn’t match the inferred value type.
> > 
> > This error doesn’t occur with immutable hash tables, because it’s fine to 
> > extend an immutable hash table to a larger one that contains it; the 
> > original one’s contract isn’t violated.
> > 
> > In this case, one easy error is to change the ‘cast’ into an ‘ann’, which 
> > works fine.
> > 
> > This is the first time I’ve encouraged my students to use a mutable hash, 
> > which is presumably why I haven’t encountered this before.
> > 
> > I’m trying to formulate a solution that I can put in a Hints for TR file, 
> > and I think the answer is probably this:
> > 
> > - When you use “make-hash” in TR, you should always specify the types 
> > explicitly using an “inst”.
> > or maybe
> > - When you call “make-hash” in TR, the call should be immediately wrapped 
> > with an “ann” type annotation.
> > 
> > In a perfect world, I think I would ask for a warning when casting a 
> > mutable hash table. It could go in that nice “warnings” box. Oh, wait…
> > 
> > … joking aside, actually I just did turn on the “log” window and I don’t 
> > see any warning about this, which is not too surprising. Oh, wait, I see 
> > another bug…
> > 
> > Thanks for reading, let me know if there’s any more obvious solution.
> > 
> > John
> > 
> > 
> > 
> > 
> > 
> > 
> > -- 
> > 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 
> > 

Re: [racket-users] cast on mutable hash table...

2020-02-15 Thread Philip McGrath
On Sat, Feb 15, 2020 at 1:23 PM 'John Clements' via users-redirect <
us...@plt-scheme.org> wrote:

> Yes, absolutely. One reason that students in my class wind up using cast
> quite frequently in their parsers is that they use patterns like (list (?
> symbol s) …) which (as I recall) expand into unannotated lambda’s, and
> always require a cast.


I like `assert` for those situations: it fails immediately if anything goes
wrong, and it uses a predicate, so it corresponds well to the `?` pattern.
(It can also perform better.)

-Philip

> --
-Philip

-- 
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/CAH3z3gbMiTHvBBRsRFuNzTJNy0sOMMZ3DGNbe312%2Bmjf7atdDg%40mail.gmail.com.


Re: [racket-users] Web server + authentication

2020-02-15 Thread 'John Clements' via Racket Users
Wait, we *all* have postmark libraries? 

Sigh.

John

> On Jan 23, 2020, at 16:29, Jens Axel Søgaard  wrote:
> 
> Den tor. 23. jan. 2020 kl. 01.47 skrev Matthew Butterick :
> I concur on Postmark. For 2+ yrs I've used it with the Racket web server for 
> mbtype.com. I pass the server settings to `smtp-send-message` from `net/smtp`.
> 
>> On 22 Jan 20, at 3:00 AM, Bogdan Popa  wrote:
>> 
>> I like using Postmark[0] for this.  Their free plan lets you send up to
>> 100 e-mails a month, their paid plans come at a reasonable price and
>> they have helpful docs and validators to help you set up SPF, DMARC and
>> DKIM.
> Two recommendations!
> 
> I took a second look at Postmark and was impressed by their service,
> so I went ahead and added password recovery emails via Postmark.
> 
> Some nice features:
> - don't need to setup and maintain a mail server
> - weekly report on the "spam status" of your domain
> - email templates (html mails are hard)
> - an ios app to track mails
> - 100 mails free pr month (which for Racket Stories is plenty)
> 
> Thanks for the push.
> 
> 
> Stephen, I think, password resets were what you originally asked for (see 
> latest commit).
> 
>  https://github.com/soegaard/racket-stories
> 
> The code hasn't been deployed yet - it'll happen sometime tomorrow.
> 
> Philip: I'll keep your solution in mind, if I for some reason need to change 
> it.
> 
> /Jens Axel
> 
> 
> 
> 
> 
> -- 
> 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/CABefVgzNd2ZF1fpChjPKu9uYP1JambVJNzONUqKeTquPR-v2Lg%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/5614dc15-473c-4ff6-81a2-5c951cd8f919%40mtasv.net.


Re: [racket-users] resources for learning JS / React?

2020-02-15 Thread 'John Clements' via Racket Users
Belatedly: awesome, many thanks!

John

> On Jan 27, 2020, at 02:39, Sean Kemplay  wrote:
> 
> 
> This is a good (free) course that takes you the lates best practices of JS 
> (getting more functional), react and then react native.
> 
> https://www.edx.org/course/cs50s-mobile-app-development-with-react-native
> 
> 
> On Saturday, January 25, 2020 at 6:56:57 PM UTC, 'John Clements' via 
> users-redirect wrote:
> I have a graduate student that wants a self-guided introduction to JS and 
> React. The problem here, to some degree, is that there are so *many* 
> introductions. Does anyone here have specific references that might be 
> helpful? (Say, e.g., if Gregor Kiczales did a JS course on coursera… that 
> would be pretty much perfect.) 
> 
> John 
> 
> 
> -- 
> 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/c433373a-3135-426d-90f3-f5d79032d38c%40googlegroups.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/7b5f53bb-0401-4fc0-81e1-78dd375e88f4%40mtasv.net.


Re: [racket-users] Rolling dice game

2020-02-15 Thread 'John Clements' via Racket Users
Have you taken a look at How To Design Programs? At the end of section one, you 
should have what you need to build these games and others like them:

https://htdp.org/2019-02-24/part_one.html

John Clements

> On Feb 3, 2020, at 03:31, Wilzoo  wrote:
> 
> Hi guys, so I am working on rolling dice game in GUI, the game rules will be 
> something like rolling 3 dices and then giving out points to 2 players based 
> on the rolled values. 
> 
> Im now stuck on the showing value in GUI. Basically what I need is to show 
> rolled value somewhere in frame. 
> 
> This is my rolling dice code:
> 
> (define (nrandom n)
> (if (= n 0 ) '() (cons (random 1 7) (nrandom (- n 1)
> 
> this is my callback function which i know is wrong:
> 
> (new button% [parent gamew]
>  [label "roll dice"]
>  [min-width 150]
>  ; Callback procedure for a button click:
>  [callback (lambda (b e) (send (nrandom 3) show #t))])
> 
> So by using this im getting my 3 values in IDE window. Where should I look 
> for information how to get them in frame in some kind of box?
> 
> I just started my programming journey and racket documentation is not easy 
> for me to get any information from there tbh. Just figuring out how to make 
> frame and buttons + callback functions took me good few hours.
> 
> Thanks for help
> 
> 
> -- 
> 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/da23d01b-5422-4fe9-b40d-0ca9a904fed9%40googlegroups.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/9c3d2a9a-a79d-47d6-b874-31a8dbb0d296%40mtasv.net.


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

2020-02-15 Thread 'John Clements' via Racket Users
Did anyone suggest this code to you? Apologies if I’m re-treading an old 
conversation.

#lang typed/racket
(: string2value (-> String Integer))
(define (string2value str)
  (define maybe-integer (string->number str))
  (cond [(exact-integer? maybe-integer) maybe-integer]
[else (error 'string2value
 "expected a string convertible to an integer, got: ~e"
 str)]))


Best,

John Clements

> On Feb 15, 2020, at 10:37, 'John Clements' via Racket Users 
>  wrote:
> 
> ??
> 
>> (string2value "-1234")
> - : Integer
> -28766
>> (string2value "abcd")
> - : Integer
> 54562
>> 
> 
> Is this your desired behavior?
> 
>> On Feb 12, 2020, at 16:43, Alain De Vos  wrote:
>> 
>> 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.
> 
> 
> 
> -- 
> 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/f6d3c6ef-8779-4e91-956f-d82eb11b%40mtasv.net.



-- 
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/450ccb56-b2ff-42c7-9df3-292f07151fd9%40mtasv.net.


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

2020-02-15 Thread 'John Clements' via Racket Users
??

> (string2value "-1234")
- : Integer
-28766
> (string2value "abcd")
- : Integer
54562
> 

Is this your desired behavior?

> On Feb 12, 2020, at 16:43, Alain De Vos  wrote:
> 
> 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.



-- 
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/f6d3c6ef-8779-4e91-956f-d82eb11b%40mtasv.net.


Re: [racket-users] cast on mutable hash table...

2020-02-15 Thread 'John Clements' via users-redirect
Yes, absolutely. One reason that students in my class wind up using cast quite 
frequently in their parsers is that they use patterns like (list (? symbol s) 
…) which (as I recall) expand into unannotated lambda’s, and always require a 
cast. I write that up here:

https://www.brinckerhoff.org/clements/2202-csc430/Assignments/tr-notes.html#%28part._.Predicate_.Patterns_with_.Dot-dot-dot%29

Beyond that, though, I tell them never to use cast:

https://www.brinckerhoff.org/clements/2202-csc430/Assignments/tr-notes.html#%28part._.Other_.Casting%29

The text I came up with for mutable hash tables is here:

https://www.brinckerhoff.org/clements/2202-csc430/Assignments/tr-notes.html#%28part._.Mutable_.Hash_.Table_.Construction%29

Many thanks to all of you!

John

> On Feb 14, 2020, at 12:32, Sam Tobin-Hochstadt  wrote:
> 
> The advice I would really give is to give a name and a type annotation to any 
> mutable data structure you create. 
> 
> Also, cast is almost never what you want. I see lots of people (maybe 
> including this student) using it as a substitute for type annotation but it 
> really isn't. 
> 
> Sam
> 
> On Fri, Feb 14, 2020, 2:41 PM 'John Clements' via users-redirect 
>  wrote:
> I think I may understand what’s going on here, but a student and I worked on 
> this for quite a while today before I found the problem.
> 
> Here’s a program:
> 
> #lang typed/racket
> 
> (define-type Store (Mutable-HashTable Integer Value))
> (define-type Value (U Real Boolean String))
> 
> (define top-store (cast (make-hash (list
> (cons -1 14)
> (cons 1 #t)
> (cons 2 #f)))
> Store))
> 
> (hash-set! top-store 5 1234)
> 
> It fails with this error:
> 
> contract violation
>   expected: (or/c (and/c byte? positive?) #t #f)
>   given: 1234
>   in: the values of
>   the 3rd conjunct of
>   (and/c
>hash?
>hash-mutable?
>(hash/c
> exact-integer?
> (or/c (and/c byte? positive?) #t #f)
> #:immutable
> #f))
>   contract from: typed-world
>   blaming: cast
>(assuming the contract is correct)
>   at: unsaved-editor:6.18
> 
> If I understand what’s going on here, the basic issue is that the mutable 
> hash table’s type is being inferred as (Immutable-HashTable Exact-Integer (U 
> Boolean Positive-Byte)), and then as part of the cast, a contract is being 
> inserted, which checks that all added values match the expected value type. 
> The outer cast allows type checking to proceed, but then at runtime it fails 
> because the given value doesn’t match the inferred value type.
> 
> This error doesn’t occur with immutable hash tables, because it’s fine to 
> extend an immutable hash table to a larger one that contains it; the original 
> one’s contract isn’t violated.
> 
> In this case, one easy error is to change the ‘cast’ into an ‘ann’, which 
> works fine.
> 
> This is the first time I’ve encouraged my students to use a mutable hash, 
> which is presumably why I haven’t encountered this before.
> 
> I’m trying to formulate a solution that I can put in a Hints for TR file, and 
> I think the answer is probably this:
> 
> - When you use “make-hash” in TR, you should always specify the types 
> explicitly using an “inst”.
> or maybe
> - When you call “make-hash” in TR, the call should be immediately wrapped 
> with an “ann” type annotation.
> 
> In a perfect world, I think I would ask for a warning when casting a mutable 
> hash table. It could go in that nice “warnings” box. Oh, wait…
> 
> … joking aside, actually I just did turn on the “log” window and I don’t see 
> any warning about this, which is not too surprising. Oh, wait, I see another 
> bug…
> 
> Thanks for reading, let me know if there’s any more obvious solution.
> 
> John
> 
> 
> 
> 
> 
> 
> -- 
> 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/f24851ca-32aa-40e1-9db4-691da815ebe7%40mtasv.net.



-- 
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/f1cdb734-cb79-4a1a-8bb8-3bf3d4a425b4%40mtasv.net.


Re: [racket-users] Help with vector-sort

2020-02-15 Thread Jens Axel Søgaard
Den lør. 15. feb. 2020 kl. 13.44 skrev greadey :

> I have written a programme to compute a bootstrapped mean etc.  I
> successfully wrote it using lists both untyped and in typed/racket.
> I am interested in optimising the code and having seen that typed racket
> performs faster (for lists) I am interested in seeing if I get a
> performance increase using vectors in both typed and untyped code.
> In order to compute the median value as well as certain percentiles it is
> necessary to sort the samples.  My specific problem is using vector-sort.
> I have got round it by using vector->list and list->vector functions and
> using the list sort function.
> If I use vector-sort typed racket keeps telling me to use
> "require/typed".  I got a function definition to compile  (:sort-myvector :
> (Vectorof Flonum) (Flonum Flonum -> Boolean) -> (Vectorof Flonum))
> I'm not sure if that is exactly right but it was something similar,
> however it did not work.  I did add a "require/typed racket/vector" to the
> expression.
> So, how can I use vector-sort in typed racket code, and can anyone tell me
> if translating vectors to lists and back again actually introduces a
> performance hit?
>

Here is an example of how to use `vector-sort` from a typed/racket program:

#lang typed/racket
(require/typed racket/vector
  [vector-sort (-> (Vectorof Any) (-> Any Any Boolean) (Vectorof Any))])

(define (compare x y)
  (if (and (real? x) (real? y))
  (< x y)
  (error 'compare "expected two real numbers, got: ~a ~a" x y)))

(vector-sort (vector 3 1 5 2) compare)

/Jens Axel

-- 
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/CABefVgzOSTZJs%2BaZADFxe8oPJ%2BNx4ihvjHjJ19BcqhF2oKkayQ%40mail.gmail.com.


[racket-users] Help with vector-sort

2020-02-15 Thread greadey
Hi there,

I have written a programme to compute a bootstrapped mean etc.  I 
successfully wrote it using lists both untyped and in typed/racket.
I am interested in optimising the code and having seen that typed racket 
performs faster (for lists) I am interested in seeing if I get a 
performance increase using vectors in both typed and untyped code.
In order to compute the median value as well as certain percentiles it is 
necessary to sort the samples.  My specific problem is using vector-sort.  
I have got round it by using vector->list and list->vector functions and 
using the list sort function.
If I use vector-sort typed racket keeps telling me to use "require/typed".  
I got a function definition to compile  (:sort-myvector : (Vectorof Flonum) 
(Flonum Flonum -> Boolean) -> (Vectorof Flonum))
I'm not sure if that is exactly right but it was something similar, however 
it did not work.  I did add a "require/typed racket/vector" to the 
expression.
So, how can I use vector-sort in typed racket code, and can anyone tell me 
if translating vectors to lists and back again actually introduces a 
performance hit? 
Here is my code for reference
/
#lang typed/racket

(require math/flonum)
(require racket/vector)
(require math/statistics)
;(require "net/basic-defs.rkt")


;;This struct to hold the means and SDs for each bootstrap list
;;We don't need these structs for this
;;(struct sig-mean (mean-val sd-val) #:transparent)
;;(struct sig-median (median-val sd-val) #:transparent)
;;What we need is this!
(struct metrics ([mean-val : Real] [median-val : Real]) #:transparent)


(: orig (Vectorof Real))
(define orig (vector  8.9518e-02
  9.9626e-02
  5.1029e-01
  2.2797e-01
  7.1273e-02
  7.4641e-01
  6.4869e-01
  8.1512e-01
  6.4230e-01
  4.2182e-03
  5.1573e-01))

;;This function picks a random item from a list of data
(: idx : (Vectorof Real) -> Integer)
(define (idx v)
  (random (vector-length v)))

;;This is because random yields a non-negative fixnum but we need to pass
;;an Integer to list-ref.
(: pick-item : (Vectorof Real) -> Real)
(define (pick-item v)
  (vector-ref v (idx v)))

;;This function takes a bootstrap size and a list of data and produces a
;;bootstrap data set of length size.
(: booted : Integer (Vectorof Real) -> (Vectorof (Vectorof Real)))
(define (booted size v)
  (for/vector : (Vectorof (Vectorof Real))
  ([i (in-range size)])
(for/vector : (Vectorof Real)
([j (in-range (vector-length v))])
  (pick-item v



;;This function was nicked from Rosetta Code.

(: my-median : (Vectorof Real) -> Real)
;(: my-median : (Vectorof Real) -> Real)
(define (my-median numbers)
  (define sorted-list (list->vector (sort (vector->list numbers) <)))
  (define count (vector-length numbers))
  (if (zero? count)
  0.0
  (/ (+ (vector-ref sorted-list (sub1 (floor (/ (add1 count) 2
(vector-ref sorted-list (sub1 (ceiling (/ (add1 count) 2)
 2)))


;;This function extracts the means and the stddevs for each sample.
;;lol is a list of list(s) of samples, returns a list of structs

(: get-metrics : (Vectorof Real) -> metrics)
(define (get-metrics v)
  (metrics (mean v) (my-median v)))

(: get-all-metrics : (Vectorof (Vectorof Real)) -> (Vectorof metrics))
(define (get-all-metrics vov)
  (vector-map get-metrics vov))

;;Takes a list of structs and a field and returns a list of the values
;;of the struct's field.  So if we want the means,
;;we call (split-metrics list-of-metrics (metrics-mean-val))
#;(: split-metrics : (Vectorof metrics) (metrics -> Real) -> (Vectorof 
Real))
#;(define (split-metrics lofStruct field)
  (vector-map field lofStruct))


;;So now we need to get the 2.5 and the 97.5 percentiles of the means and 
the medians.
;;To get the error range we need to get the 2.5th and 97.5th percentiles
;;of the means and the medians.
(: percentile : (Vectorof Real) Real -> Integer)
(define (percentile lofMet percen)
  ;(exact-round (+ 1 (* (/ percen 100) (sub1 (vector-length lofMet))
  (exact-round (* (/ percen 100) (sub1 (vector-length lofMet)

;;This function computes the 95 percent confidence limits
(: 95-limits : (Vectorof Real) -> (Listof Real))
(define (95-limits vofMeans)
  (let
  ([lower (vector-ref vofMeans (percentile vofMeans 2.5))]
   [upper (vector-ref vofMeans (percentile vofMeans 97.5))])
(list lower upper)))


(time
;;This function yields a vector of metrics structures
(define mets (get-all-metrics (booted 10 orig)))
;We get all the means with this one
(define my-means (vector-map metrics-mean-val mets))
;Samr for medians
(define my-medians (vector-map metrics-median-val mets))
;Next two get the SDs for standard error
(define std-err-mean (stddev my-means))
(define std-err-median (stddev my-medians))
;The next two