Re: [racket-users] Jupyter Racket Kernel - iracket

2018-05-03 Thread Graham Dean
Pleasure Ryan. Thanks for developing iracket, it's really useful for me to 
demonstrate on-going work using Racket.

Graham

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] How much unsafety with unsafe-require/typed for polymorphic structs?

2018-05-03 Thread Sam Tobin-Hochstadt
In general, `unsafe-require/typed` is safe if the value you get from
untyped code really does have that type. In your second example, that
genuinely is true, so it's safe. But note that there are ways it can
be trickier -- if a field is mutable but you pretend it's immutable,
Typed Racket can go wrong. And if Typed Racket makes a mistake
predicting what type something has, that can always be used to turn
into a segfault.

Sam

On Thu, May 3, 2018 at 9:11 PM, Philip McGrath  wrote:
> I have some polymorphic struct types defined in untyped Racket that I'd like
> to be able to work with from Typed Racket. I see that struct type variables
> only work with `unsafe-require/typed`; I'm trying to understand just what
> unsafety I would be signing myself up for if I were to use that so I can
> decide whether to try it or whether I need to use these structs only from
> untyped Racket.
>
> (I can't really move the definitions of the structs into TR because they use
> properties and other things TR doesn't support.)
>
> With some experimentation, the only unsafety I've discovered is that Typed
> Racket doesn't protect me from badly-behaved constructors or accessors, e.g.
> this attempts to do `(add1 "This is bad.")`:
> #lang typed/racket #:no-optimize
>
> (module untyped racket
>   (provide (struct-out bad))
>   (struct bad (v)
> #:guard (λ (x y) "This is bad.")
> #:transparent))
>
> (require typed/racket/unsafe)
>
> (unsafe-require/typed
>  'untyped
>  [#:struct (A) bad ([v : A])])
>
> (add1 (bad-v (bad 12)))
>
> If that is indeed the only unsafety, I can pretty confidently make sure that
> my untyped struct definition isn't doing anything so bad as that. Everything
> else I've tried has worked as I would hope, e.g. this program raises a nice
> contract violation:
> #lang racket
>
> (module untyped racket
>   (provide (struct-out wraper))
>   (struct wraper (v)
> #:transparent))
>
> (module typed typed/racket
>   (require typed/racket/unsafe)
>   (provide add-wraped)
>   (unsafe-require/typed
>(submod ".." untyped)
>[#:struct (A) wraper ([v : A])])
>   (: add-wraped (-> (wraper Integer)
> (wraper Integer)
> (wraper Integer)))
>   (define (add-wraped a b)
> (wraper (+ (wraper-v a)
>(wraper-v b)
>
> (require 'untyped
>  'typed)
>
> (add-wraped (wraper 1) (wraper "Not a number"))
>
> However, I wanted to ask, because I'd rather not find out there's some
> unsafety I haven't anticipated by getting a mysterious segfault.
>
> -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.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Re: game help

2018-05-03 Thread Alex Harsanyi

I wonder if *just solved your homework...*

(define (same-value? vec)
  (define test-val (vector-ref vec 0))
  (if (zero? test-val)
  #f
  (let ((same? (for/and ((v (in-vector vec)))
 (eqv? test-val v
(and same? test-val

(define (winner-by-row row-num)
  (define row (vector-ref board row-num))
  (same-value? row))

(define (winner-by-col col-num)
  (define col (for/vector ((row (in-vector board)))
(vector-ref row col-num)))
  (same-value? col))

(define (winner-by-diag direction)
  (define diag (for/vector (((row index) (in-indexed board)))
 (define idx (if (eqv? direction 1)
 index
 (- (vector-length row) (add1 index
 (vector-ref row idx)))
  (same-value? diag))

(define winner (lambda ()
 ; return winner (1 or 2) or 0 if no winner

 (or
  (for/or ((row (in-range 3)))
(winner-by-row row))
  (for/or ((col (in-range 3)))
(winner-by-col col))
  (winner-by-diag 1)
  (winner-by-diag -1)
 
 0)))

Alex.

On Friday, May 4, 2018 at 2:12:17 AM UTC+8, ademko...@gmail.com wrote:
>
> hey guys, struggling with a project, have an incomplete template of a 
> noughts and crosses game but need to change the state and display the 
> winner once they have 3 in a row.
>
> #lang racket/gui
>
> ; o and x game
> ; with a GUI. Attempts to use an MVC-like approach.
>
> ;-
> ; Model: State of the game
> ; use vectors as they are mutable - we can change the state
> (define board (vector
>(vector 0 0 0)
>(vector 0 0 0)
>(vector 0 0 0) ))
>
> ; Size of game:
> (define rows 3)
> (define cols 3)
>
> ; current go: either 1 or 2
> (define currentPlayer 1)
>
> ;-
> ; functions to modify state
>
> (define updateState (lambda (rowNum colNum val)
>   ; update the state 
>   (vector-set! (vector-ref board rowNum) colNum val)
>   ))
>
> (define switchPlayer (lambda () (set! currentPlayer (- 3 currentPlayer
>
> (define winner (lambda ()
>  ; return winner (1 or 2) or 0 if no winner
>  
>  ; row
>  ; col
>  ; diagonal
>  
>  0))
>
> ;-
> ; View: create button matrix; 
>
> ; adds a button to container - callback does the actionFunc and calls 
> update to refresh display
> ; updateFunc added to the updator set
> (define addButton (lambda (container actionFunc updateFunc rowNum colNum)
> (let ((newBut
>(new button% [label ""] [parent container] 
> [callback
>
> (lambda (button event)
>  
> (actionFunc)
>  
> (update)
>  
> )])
>))
>   (addUpdator (lambda () (updateFunc newBut)))
>   )
> ))
>
>
> (define createButRow (lambda (rowContainer rowNum numberOfButtons)
>; create a row of buttons
>(cond
>  ((equal? numberOfButtons 0))
>  (else  (let ((buttonIndex (+ (* cols (- rowNum 
> 1)) numberOfButtons)))
>   
>   (addButton
>rowContainer
>(lambda () ; actionFunc: set data item 
> buttonIndex to currentPlayer; change currentPlayer
>  (updateState (- rowNum 1) (- 
> numberOfButtons 1) currentPlayer)
>  (switchPlayer)
>  )
>(lambda (button) (updateButton button 
> (- rowNum 1) (- numberOfButtons 1) )) ; updateFunc
>rowNum numberOfButtons)
>   
>   )
> (createButRow rowContainer rowNum (- 
> numberOfButtons 1)))
>  )))
>
>
> (define createButtonRows (lambda (vContainer rowCount colCount)
>; create a rowCount rows of buttons
>(cond
>  ((equal? rowCount 0) )
>  (else  

[racket-users] How much unsafety with unsafe-require/typed for polymorphic structs?

2018-05-03 Thread Philip McGrath
I have some polymorphic struct types defined in untyped Racket that I'd
like to be able to work with from Typed Racket. I see that struct type
variables only work with `unsafe-require/typed`; I'm trying to understand
just what unsafety I would be signing myself up for if I were to use that
so I can decide whether to try it or whether I need to use these structs
only from untyped Racket.

(I can't really move the definitions of the structs into TR because they
use properties and other things TR doesn't support.)

With some experimentation, the only unsafety I've discovered is that Typed
Racket doesn't protect me from badly-behaved constructors or accessors,
e.g. this attempts to do `(add1 "This is bad.")`:
#lang typed/racket #:no-optimize

(module untyped racket
  (provide (struct-out bad))
  (struct bad (v)
#:guard (λ (x y) "This is bad.")
#:transparent))

(require typed/racket/unsafe)

(unsafe-require/typed
 'untyped
 [#:struct (A) bad ([v : A])])

(add1 (bad-v (bad 12)))

If that is indeed the only unsafety, I can pretty confidently make sure
that my untyped struct definition isn't doing anything so bad as that.
Everything else I've tried has worked as I would hope, e.g. this program
raises a nice contract violation:
#lang racket

(module untyped racket
  (provide (struct-out wraper))
  (struct wraper (v)
#:transparent))

(module typed typed/racket
  (require typed/racket/unsafe)
  (provide add-wraped)
  (unsafe-require/typed
   (submod ".." untyped)
   [#:struct (A) wraper ([v : A])])
  (: add-wraped (-> (wraper Integer)
(wraper Integer)
(wraper Integer)))
  (define (add-wraped a b)
(wraper (+ (wraper-v a)
   (wraper-v b)

(require 'untyped
 'typed)

(add-wraped (wraper 1) (wraper "Not a number"))

However, I wanted to ask, because I'd rather not find out there's some
unsafety I haven't anticipated by getting a mysterious segfault.

-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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] char-utf-8-length signature is surprising

2018-05-03 Thread David Storrs
Interesting.  Thanks for the explanation; that was bugging me.

On Thu, May 3, 2018 at 3:37 PM, Shu-Hung You
 wrote:
> Looks like the implementation of char-utf-8-length returns values
> fitting the "FSS-UTF (1992) / UTF-8 (1993)" table in
> https://en.wikipedia.org/wiki/UTF-8#History. Not sure what's the
> standard UTF-8 encoding..
>
> /* racket/src/char.c */
> static Scheme_Object *char_utf8_length (int argc, Scheme_Object *argv[])
> {
>   mzchar wc;
>   if (!SCHEME_CHARP(argv[0]))
> scheme_wrong_contract("char-utf-8-length", "char?", 0, argc, argv);
>
>   wc = SCHEME_CHAR_VAL(argv[0]);
>   if (wc < 0x80) {
> return scheme_make_integer(1);
>   } else if (wc < 0x800) {
> return scheme_make_integer(2);
>   } else if (wc < 0x1) {
> return scheme_make_integer(3);
>   } else if (wc < 0x20) {
> return scheme_make_integer(4);
>   } else if (wc < 0x400) {
> return scheme_make_integer(5);
>   } else {
> return scheme_make_integer(6);
>   }
> }
>
>
> On Thu, May 3, 2018 at 2:12 PM, David Storrs  wrote:
>> I noticed this in the docs and it surprised me:
>>
>> (char-utf-8-length char) → (integer-in 1 6)
>>
>> UTF-8 characters are 1-4 bytes, so why isn't it (integer-in 1 4)?  I
>> feel like this is probably obvious but I'm not coming up with the
>> answer.
>>
>> --
>> 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.
>> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] char-utf-8-length signature is surprising

2018-05-03 Thread Shu-Hung You
Looks like the implementation of char-utf-8-length returns values
fitting the "FSS-UTF (1992) / UTF-8 (1993)" table in
https://en.wikipedia.org/wiki/UTF-8#History. Not sure what's the
standard UTF-8 encoding..

/* racket/src/char.c */
static Scheme_Object *char_utf8_length (int argc, Scheme_Object *argv[])
{
  mzchar wc;
  if (!SCHEME_CHARP(argv[0]))
scheme_wrong_contract("char-utf-8-length", "char?", 0, argc, argv);

  wc = SCHEME_CHAR_VAL(argv[0]);
  if (wc < 0x80) {
return scheme_make_integer(1);
  } else if (wc < 0x800) {
return scheme_make_integer(2);
  } else if (wc < 0x1) {
return scheme_make_integer(3);
  } else if (wc < 0x20) {
return scheme_make_integer(4);
  } else if (wc < 0x400) {
return scheme_make_integer(5);
  } else {
return scheme_make_integer(6);
  }
}


On Thu, May 3, 2018 at 2:12 PM, David Storrs  wrote:
> I noticed this in the docs and it surprised me:
>
> (char-utf-8-length char) → (integer-in 1 6)
>
> UTF-8 characters are 1-4 bytes, so why isn't it (integer-in 1 4)?  I
> feel like this is probably obvious but I'm not coming up with the
> answer.
>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] char-utf-8-length signature is surprising

2018-05-03 Thread David Storrs
I noticed this in the docs and it surprised me:

(char-utf-8-length char) → (integer-in 1 6)

UTF-8 characters are 1-4 bytes, so why isn't it (integer-in 1 4)?  I
feel like this is probably obvious but I'm not coming up with the
answer.

-- 
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.
For more options, visit https://groups.google.com/d/optout.


[racket-users] game help

2018-05-03 Thread ademkolliari
hey guys, struggling with a project, have an incomplete template of a 
noughts and crosses game but need to change the state and display the 
winner once they have 3 in a row.

#lang racket/gui

; o and x game
; with a GUI. Attempts to use an MVC-like approach.

;-
; Model: State of the game
; use vectors as they are mutable - we can change the state
(define board (vector
   (vector 0 0 0)
   (vector 0 0 0)
   (vector 0 0 0) ))

; Size of game:
(define rows 3)
(define cols 3)

; current go: either 1 or 2
(define currentPlayer 1)

;-
; functions to modify state

(define updateState (lambda (rowNum colNum val)
  ; update the state 
  (vector-set! (vector-ref board rowNum) colNum val)
  ))

(define switchPlayer (lambda () (set! currentPlayer (- 3 currentPlayer

(define winner (lambda ()
 ; return winner (1 or 2) or 0 if no winner
 
 ; row
 ; col
 ; diagonal
 
 0))

;-
; View: create button matrix; 

; adds a button to container - callback does the actionFunc and calls 
update to refresh display
; updateFunc added to the updator set
(define addButton (lambda (container actionFunc updateFunc rowNum colNum)
(let ((newBut
   (new button% [label ""] [parent container] 
[callback
   
(lambda (button event)
 
(actionFunc)
 
(update)
 )])
   ))
  (addUpdator (lambda () (updateFunc newBut)))
  )
))


(define createButRow (lambda (rowContainer rowNum numberOfButtons)
   ; create a row of buttons
   (cond
 ((equal? numberOfButtons 0))
 (else  (let ((buttonIndex (+ (* cols (- rowNum 1)) 
numberOfButtons)))
  
  (addButton
   rowContainer
   (lambda () ; actionFunc: set data item 
buttonIndex to currentPlayer; change currentPlayer
 (updateState (- rowNum 1) (- 
numberOfButtons 1) currentPlayer)
 (switchPlayer)
 )
   (lambda (button) (updateButton button (- 
rowNum 1) (- numberOfButtons 1) )) ; updateFunc
   rowNum numberOfButtons)
  
  )
(createButRow rowContainer rowNum (- 
numberOfButtons 1)))
 )))


(define createButtonRows (lambda (vContainer rowCount colCount)
   ; create a rowCount rows of buttons
   (cond
 ((equal? rowCount 0) )
 (else  
  (createButRow 
   (new horizontal-panel% [parent vContainer]
[alignment '(center center)])  rowCount 
colCount)
  (createButtonRows vContainer (- rowCount 1) 
colCount) 
  )
 )
   )
  )
(define createBoard (lambda (container rowCount colCount) 
  (createButtonRows 
   (new vertical-panel% [parent container]
[alignment '(center center)])
   rowCount colCount)
  )
  )

; add a message box at the bottom: this will be updated by do show whose 
turn it is
(define addMessageArea (lambda (container)
 (let  ((messageBox (new message% [label ""] 
[parent container] [ auto-resize #t])))
   (addUpdator (lambda ()
 (let ((w (winner)))
   (cond
 ((equal? w 0) (send messageBox 
set-label (format "Player ~a to play" currentPlayer)))
 (#t (send messageBox set-label 
(format "Winner is ~a" w)))
 )
   ))
   


;---

Re: [racket-users] Jupyter Racket Kernel - iracket

2018-05-03 Thread Ryan Culpepper

On 05/01/2018 09:33 PM, Graham Dean wrote:

PR submitted :)


PR merged. Thanks!

Ryan

--
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.
For more options, visit https://groups.google.com/d/optout.