Re: [racket-users] What do you use macros for?

2016-04-11 Thread Matthias Felleisen

Late to the party, and to make life simple, I removed all quoted material. 

Asumu quoted me from the early 1990s. That's what I 
understood back then about macros and I got on the
wrong track with this idea for about five to eight 
years. I wanted macros to become complete language
definitions ... and that's what they are good for, 
but there is way more in all directions. So here are 
two thoughts. 

First credit to Kent Dybvig who bluntly said to me 
in '97 or so, I want to be able to define macros inside 
of some lexical scope. These days I define little macros
inside of loops or methods all the time. 

Here is an example: 

(define/public (run)
  (parameterize ([current-input-port in] [current-output-port out])
(send-message SIGN-UP)
(define ok (read-json in))
(define/fsm start
  (state: start json->start void --> choose)
  (state: choose json->choose (compose send-message values) --> 
feed-next)
  (state: feed-next json->state next->json --> feed-next || start

The above is a remote proxy that will receive three kinds of
messages on port 'in' and will respond via a call to a proper
class. Then it transitions to a different state where it will wait
for a different kind of input. The define/fsm macro makes this 
incredible easy to express. I bet everyone can read this 3-line
FSM and guess what each transition does: 

 -- in state 'feed-next', the method reads the message with json->state
 -- calls the method feed-next of the contained object 
 -- sends the result off to the 'out' port with next->json 
 -- and then transitions to either feed-next or start, 
depending on what kind of message arrives. 

The 10-line macro refers to pieces of the class and cannot be
defined globally. 

Second credit to Matthew (and Shriram and Robby) who kept me 
on the 'macros are good for designing complete languages' track, 
too. This one is important for different reasons. Often you want
to create a reasonably complete language but not invent everything
from scratch. In our words, you want 'linguistic inheritance'. For
example, you like Haskell's type system but you hate, loathe, despise
the stupid idea of universally lazy evaluation. Well, if you were
in Racket, you'd define a 10-line language that looks, feels, smells, 
tastes like the host but has a by-value evaluation mechanism. Of course, 
Racket is by-value, so you may convert it into a lazy language without
giving up the lovely parentheses and the beautiful Lisp syntax. Well, 
it's a 10-line language definition, based on Racket's generalization
of old, very old, stone-age old Lisp macro system. 

See the last chapter of Realm of Racket on how to teach Kindergarden 
kids to define this kind of by-need (actually by-name) Racket. 

In short, as Robby says 'macros' (what a stupid word) serve the whole
range of purposes, from little conveniences inside of a class to entire
language rewrites that keep the best of one language and add better
things on top 

-- Matthias







-- 
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] Racket change button% coordinates(X and Y axis)

2016-04-11 Thread Matthew Flatt
At Sun, 10 Apr 2016 08:18:50 -0700 (PDT), Theodor Berza wrote:
> The purpose of this project is to make a GUI Builder where the user drags 
> buttons on a frame and the position of the cursor changes in the code the X 
> and Y axis.

I case you haven't found it already, you might want to take a look at
this GUI builder:

 https://github.com/Metaxal/MrEd-Designer


To answer your question more directly, you could create a panel with a
custom layout function to directly position children:

http://docs.racket-lang.org/gui/windowing-overview.html#%28part._new-containers%29


I think MrEd Designer uses the pasteboard approach that Jens Axel
suggests, though.

-- 
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] Hidden a list

2016-04-11 Thread Héctor Mc

Hello guys,

There any way to hide the contents of a list as does the structure?

(define lst (list "abc" 123 (+ 12 32)))
lst ; --> '("abc" 123 44) something like #

(define-struct user (nom ape))
(define u (make-user "nom" "ape"))
user ; --> #
u ; --> #

-- 
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] Hidden a list

2016-04-11 Thread Vincent St-Amour
You could write your own printer, that recognizes lists specially and
prints them how you want, and set the `current-print` parameter.

Or you could use an opaque struct type and wrap your lists with those
structs.

Vincent


On Mon, 11 Apr 2016 10:35:55 -0500,
Héctor Mc wrote:
> 
> Hello guys,
> 
> There any way to hide the contents of a list as does the structure?
> 
> (define lst (list "abc" 123 (+ 12 32)))
> lst ; --> '("abc" 123 44) something like #
> 
> (define-struct user (nom ape))
> (define u (make-user "nom" "ape"))
> user ; --> #
> u ; --> #
> 
> -- 
> 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] Racket's UDP Behavior

2016-04-11 Thread Nick Gordon
I'm building reliable data transfer onto Racket's UDP suite for a term project, 
and I need to know what Racket does with corrupt datagrams. Since the Racket 
docs don't mention the word checksum for the UDP segment, I need to know some 
things:

* Does Racket just throw away corrupt datagrams?
** Does it then notify the program that's running (udp-receive!)?
* When it detects a corrupt datagram, does it do something as radical as 
contact the sender for retransmission? 

I can't build reliable data transfer until I can be sure I'm not losing 
datagrams in the ether, and I don't know how to accomplish this. Is there a 
preferred workaround for this (as in continued retransmission until the 
datagram successfully reaches its destination)?

Thanks,
Nick

-- 
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] Racket's UDP Behavior

2016-04-11 Thread Tony Garnock-Jones
On 04/11/2016 01:52 PM, Nick Gordon wrote:
> I'm building reliable data transfer onto Racket's UDP suite for a
> term project, and I need to know what Racket does with corrupt
> datagrams. Since the Racket docs don't mention the word checksum for
> the UDP segment, I need to know some things:

The kernel discards corrupt UDP datagrams, so Racket doesn't see them.

There is certainly *no* automatic retransmission involved.

> I can't build reliable data transfer until I can be sure I'm not
> losing datagrams in the ether, and I don't know how to accomplish
> this. Is there a preferred workaround for this (as in continued
> retransmission until the datagram successfully reaches its
> destination)?

Yes, effectively this. I recommend reading up on protocols for achieving
"reliable" delivery on unreliable networks. For example, look into how
TCP works. (Bear in mind that UDP is basically the same thing as IP.)

One of your first tasks will be to decide what "reliable" means in your
context. If you haven't seen it before, check out the
Fischer-Lynch-Patterson Impossibility Result:
http://the-paper-trail.org/blog/a-brief-tour-of-flp-impossibility/

Also, look into the Byzantine Generals problem:
https://en.wikipedia.org/wiki/Byzantine_fault_tolerance

Once you understand why the Byzantine Generals problem has no solution,
you can start to work toward making a decision about how your "reliable"
system should behave in corner cases.

Tony

-- 
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] Re: Problem with # and mutable lists

2016-04-11 Thread Benjamin Greenman
>
> but although I changed else expresion, Racket sometimes returns same
> warning(but now with #f instead of #).


After you call `select-room`, you'll want to check that the result is an
mlist (instead of #f or #) before calling mcdr.

-- 
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: What do you use macros for?

2016-04-11 Thread George Neuner
On Mon, 11 Apr 2016 10:25:46 -0400, Matthias Felleisen
 wrote:

>These days I define little macros inside of loops or methods all the time. 

Same here.  My most common uses are to handle database connections and
to embed free form SQL into Racket code. 

George

-- 
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] Re: What do you use macros for?

2016-04-11 Thread Daniel Prager
On Apr 12, 2016 7:53 AM, "George Neuner"  wrote:
>
> On Mon, 11 Apr 2016 10:25:46 -0400, Matthias Felleisen
>  wrote:
>
> >These days I define little macros inside of loops or methods all the
time.
>
> Same here.  My most common uses are to handle database connections and
> to embed free form SQL into Racket code.
>
> George
>

Care to post one of your examples, with a bit of commentary?

Dan

-- 
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] Questions about implementing a Lisp-2

2016-04-11 Thread Josh Tilles
Hello Racketeers,

I come seeking advice. I'm trying to implement a Lisp-2 in Racket, but I've 
been unsuccessful in my initial attempts.

My goal is to be able to do something like the following:

```
(defun example (sym)
  (symbol->string sym))

(defvar example 'an-arbitrary-symbol)

(example example)
;=> "an-arbitrary-symbol"
```

And my here was my first stab at a solution:

```
#lang racket

(define-for-syntax functions-ns (make-empty-namespace))
(define-for-syntax values-ns (make-empty-namespace))

(define-syntax (defun stx)
  (syntax-case stx ()
[(_ name (arg ...) body0 body ...)
 (parameterize ([current-namespace functions-ns])
   #'(define (name arg ...)
   body0
   body ...))]))

(define-syntax (defvar stx)
  (syntax-case stx ()
[(_ name value)
 (parameterize ([current-namespace values-ns])
   #'(define name value))]))

;; Let's see whether these work.
(defvar example 'an-arbitrary-symbol)
(defun example (sym) (symbol->string sym))
(example example)
;;=> symbol->string: contract violation
;;=>   expected: symbol?
;;=>   given: #
;;=>   context...:
;;=>/opt/homebrew-cask/Caskroom/racket/6.4/Racket 
v6.4/collects/racket/private/misc.rkt:87:7
```

I'll be deeply grateful for help you can provide.

Cheers,
Josh

-- 
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] Questions about implementing a Lisp-2

2016-04-11 Thread Neil Van Dyke

Josh Tilles wrote on 04/11/2016 07:51 PM:

I'm trying to implement a Lisp-2 in Racket,


I would first decide whether and how I want functions and variables 
provided by modules in this language, to be usable from modules in other 
`#lang`s.  That narrows down the options of how to do it.


If you're writing an Emacs Lisp implementation, I would look at what 
Guile has done.  And also consider whether the dynamically-scoped and 
buffer-local variables affect how you solve your Lisp-2 namespaces problem.


Neil V.

--
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] Questions about implementing a Lisp-2

2016-04-11 Thread Matthew Butterick
Messing with `current-namespace` seems like overkill, though maybe this 
approach is too naive for your needs, which just involves hiding the `defun` 
identifiers in the current namespace with a `defun:` prefix, then using `#%app` 
to pull them out again.


#lang racket
(require rackunit (for-syntax racket/syntax) (prefix-in rb: (only-in 
racket/base #%app)))

(define-syntax (defun stx)
  (syntax-case stx ()
[(_ name (arg ...) body0 body ...)
 (with-syntax ([defun:name (format-id stx "defun:~a" #'name)])
   #'(define (defun:name arg ...)
   body0
   body ...))]))

(define-syntax-rule (defvar name value)
  (define name value))

(define-syntax (#%app stx)
  (syntax-case stx ()
[(_ proc-name args ...)
 (with-syntax ([defun:proc-name (format-id stx "defun:~a" #'proc-name)])
   (if (identifier-binding #'defun:proc-name)
   #'(rb:#%app defun:proc-name args ...)
   #'(rb:#%app proc-name args ...)))]))


;; Let's see whether these work.
(defvar example 'an-arbitrary-symbol)
(defun example (sym) (symbol->string sym))
(check-equal? (example example) "an-arbitrary-symbol")
(check-equal? (symbol->string example) "an-arbitrary-symbol")





On Apr 11, 2016, at 4:51 PM, Josh Tilles  wrote:

> Hello Racketeers,
> 
> I come seeking advice. I'm trying to implement a Lisp-2 in Racket, but I've 
> been unsuccessful in my initial attempts.
> 
> My goal is to be able to do something like the following:
> 
> ```
> (defun example (sym)
>  (symbol->string sym))
> 
> (defvar example 'an-arbitrary-symbol)
> 
> (example example)
> ;=> "an-arbitrary-symbol"
> ```
> 
> And my here was my first stab at a solution:
> 
> ```
> #lang racket
> 
> (define-for-syntax functions-ns (make-empty-namespace))
> (define-for-syntax values-ns (make-empty-namespace))
> 
> (define-syntax (defun stx)
>  (syntax-case stx ()
>[(_ name (arg ...) body0 body ...)
> (parameterize ([current-namespace functions-ns])
>   #'(define (name arg ...)
>   body0
>   body ...))]))
> 
> (define-syntax (defvar stx)
>  (syntax-case stx ()
>[(_ name value)
> (parameterize ([current-namespace values-ns])
>   #'(define name value))]))
> 
> ;; Let's see whether these work.
> (defvar example 'an-arbitrary-symbol)
> (defun example (sym) (symbol->string sym))
> (example example)
> ;;=> symbol->string: contract violation
> ;;=>   expected: symbol?
> ;;=>   given: #
> ;;=>   context...:
> ;;=>/opt/homebrew-cask/Caskroom/racket/6.4/Racket 
> v6.4/collects/racket/private/misc.rkt:87:7
> ```
> 
> I'll be deeply grateful for help you can provide.
> 
> Cheers,
> Josh
> 
> -- 
> 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] Re: What do you use macros for?

2016-04-11 Thread Benjamin Greenman
Today I decided to convert all structure definitions in a Typed Racket file
to lists. For instance,

(struct foo ([x : Integer]))


would become a tagged list:

(define-type foo (Pairof 'foo (List Integer)))

along with a constructor, predicate, and accessors.

Using a macro, I was able to write what I wanted to do once and use it for
multiple structures.
Here's how it looked in one file: all I had to do was write the macro, use
"struct2" instead of "struct", and remove uses of "struct-out". Other files
in the project worked with the new data definition, no problems.

#lang typed/racket
(require (for-syntax racket/base syntax/parse racket/syntax))

(define-syntax (struct2 stx)
  (syntax-parse stx #:datum-literals (:)
   [(_ name:id ([f*:id : t*] ...))
#:with ((name-f* i*) ...)
  (for/list ([f (in-list (syntax-e #'(f* ...)))]
 [i (in-naturals 1)])
(list (format-id stx "~a-~a" (syntax-e #'name) (syntax-e f)) i))
#:with Name (format-id stx "~a" (string-titlecase (symbol->string
(syntax-e #'name
#:with name? (format-id stx "~a?" (syntax-e #'name))
(syntax/loc stx (begin
  (define-type Name (Pairof 'name (Listof Any)))
  (provide Name)
  (define (name (f* : t*) ...) : Name
(list 'name f* ...))
  (provide name)
  (define (name-f* (p : Name)) : t*
(cast (list-ref p 'i*) t*))
  ...
  (provide name-f* ...)
  (define (name? (v : Any)) : Boolean
(and (list? v) (not (null? v)) (eq? 'name (car v  ))]))

(struct2 posn ([x : Real]
   [y : Real]))
(struct2 block ([x : Real]
[y : Real]
[color : Symbol]))
(struct2 tetra ([center : Posn]
[blocks : (Listof Block)]))
(struct2 world ([tetra : Tetra]
[blocks : (Listof Block)]))

(: posn=? (-> Posn Posn Boolean))
(define (posn=? p1 p2)
  (and (= (posn-x p1) (posn-x p2))
   (= (posn-y p1) (posn-y p2

(provide posn=?)


(Nevermind why I did this, or why I used "Any" and "cast". Hopefully the
macro is interesting.)

-- 
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] DrRacket 6.4 error message + a misleading message

2016-04-11 Thread Elena Machkasova
Hi All, 

we have discovered that in 6.4 DrRacket (Advanced Student) seems to have a bug 
generating error messages for foldl/foldr:

(foldl + (list 2 3 4) 0) 

gives an error message "foldl: 2th argument must be a list, given 0" 
(in addition to the incorrect "th" for "2nd", the 0 is actually the third 
argument, not second). 

There is also a somewhat misleading error (for beginners) in 6.4 and earlier 
versions, shown with a somewhat artificial example: 

(map (lambda (x y) (+ x y)) (list 2 3 4))

gives and error:

 the given procedure's expected number of arguments does not match
 the given number of lists
  given procedure: #
  expected: 2
  given: 1 

This is technically correct, but not understandable to a student since most 
likely they mistakenly used a two-argument function instead of a one-argument 
one. In fact, students are never shown a map with multiple lists. A better 
message would say that a one-argument function was expected, but a two-argument 
function was given. 

The second error is somewhat of a design choice, but the first one (with foldl) 
is a bug. If there is a way to patch it in 6.4, that would be greatly 
appreciated. 

Cheers,
Elena
 

-- 
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] DrRacket 6.4 error message + a misleading message

2016-04-11 Thread Matthias Felleisen

Thanks for the bug report. I can confirm the error, and I will check into it 
tomorrow morning. 




> On Apr 11, 2016, at 9:25 PM, Elena Machkasova  wrote:
> 
> Hi All, 
> 
> we have discovered that in 6.4 DrRacket (Advanced Student) seems to have a 
> bug generating error messages for foldl/foldr:
> 
> (foldl + (list 2 3 4) 0) 
> 
> gives an error message "foldl: 2th argument must be a list, given 0" 
> (in addition to the incorrect "th" for "2nd", the 0 is actually the third 
> argument, not second). 
> 
> There is also a somewhat misleading error (for beginners) in 6.4 and earlier 
> versions, shown with a somewhat artificial example: 
> 
> (map (lambda (x y) (+ x y)) (list 2 3 4))
> 
> gives and error:
> 
> the given procedure's expected number of arguments does not match
> the given number of lists
>  given procedure: #
>  expected: 2
>  given: 1 
> 
> This is technically correct, but not understandable to a student since most 
> likely they mistakenly used a two-argument function instead of a one-argument 
> one. In fact, students are never shown a map with multiple lists. A better 
> message would say that a one-argument function was expected, but a 
> two-argument function was given. 
> 
> The second error is somewhat of a design choice, but the first one (with 
> foldl) is a bug. If there is a way to patch it in 6.4, that would be greatly 
> appreciated. 
> 
> Cheers,
> Elena
> 
> 
> -- 
> 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.