Re: [racket-users] how to match unbound identifier as literal in `syntax-parse`?

2018-04-03 Thread Matthew Butterick

> On Apr 3, 2018, at 4:37 AM, Ryan Culpepper  wrote:
> 
> Here's one way:
> 
>  (~and z:id
>   (~fail #:unless (free-identifier=? #'z #'zeta)
>  "expected the identifier `zeta`"))
> 
> Another way is to make a syntax class (either specifically for `zeta` or 
> parameterized by the identifier) that does the same check.



What about just using `~literal`? It seems to be more lenient than `#:literals` 
(because it accepts unbound identifiers) but also relies on the 
`free-identifier=?` predicate. [1] 


[1] 
https://docs.racket-lang.org/syntax/stxparse-patterns.html?q=~literal#%28form._%28%28lib._syntax%2Fparse..rkt%29._~7eliteral%29%29
 



-- 
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] let-syntax example?

2018-04-03 Thread Alex Knauth


> On Apr 3, 2018, at 2:31 PM, Kevin Forchione  wrote:
> 
> Hi Guys,
> Does anyone have an analogous example for let-syntax to something as simple 
> as this?
> 
>   (let ([a 3]) a)
> 
> Something like….
> 
>   (let-syntax ([a 3]) ….) 
> 
> At which point I’m stumped as to what expression in the body would return 3. 
> There are no examples in the Reference. 

Others have pointed out different things you could put on the right-hand-side 
of the let-binding, iow, where the 3 is.

However, your original question was about what body expression would make 
`(let-syntax ([a 3]) )` return 3. The function that lets you do that is 
`syntax-local-value`. It's used in macros to get the compile-time values of 
things defined with define-syntax or let-syntax.

(let-syntax ([a 3])
  (define-syntax m
(lambda (stx)
  #`(quote #,(syntax-local-value #'a
  (m))

While this compile-time value can be any normal value like 3, most of the time 
you want it to be a function. If it's a function, the macro expander will call 
that function when it sees that identifier in the code, to expand the macro.

However, if you want two macros to communicate a compile-time value between 
them, you can have one of them generate a `let-syntax` or `define-syntax` to 
define that value, and have the other use `syntax-local-value` to get it.

Alex Knauth

> Thanks!
> Kevin


-- 
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] let-syntax example?

2018-04-03 Thread David Storrs
On Tue, Apr 3, 2018 at 3:02 PM, Shu-Hung You
 wrote:
> Hi Keven,
>
> Here's an example:
>
> #lang racket
> (let-syntax ([a  (lambda (stx)
>(printf "expansion time: stx = ~a\n" stx)
>#'3) ])
> a)
>
> However, I would suggest to start at least from syntax objects and
> macro transformers in the guide and use define-syntax instead of
> let-syntax.

And, of course, http://www.greghendershott.com/fear-of-macros/

> https://docs.racket-lang.org/guide/stx-obj.html
> https://docs.racket-lang.org/guide/macro-transformers.html
>
> Best,
> Shu-Hung
>
> On Tue, Apr 3, 2018 at 1:31 PM, Kevin Forchione  wrote:
>> Hi Guys,
>> Does anyone have an analogous example for let-syntax to something as simple 
>> as this?
>>
>> (let ([a 3]) a)
>>
>> Something like….
>>
>> (let-syntax ([a 3]) ….)
>>
>> At which point I’m stumped as to what expression in the body would return 3. 
>> There are no examples in the Reference.
>>
>> Thanks!
>> Kevin
>>
>> --
>> 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.

-- 
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] let-syntax example?

2018-04-03 Thread Shu-Hung You
Hi Keven,

Here's an example:

#lang racket
(let-syntax ([a  (lambda (stx)
   (printf "expansion time: stx = ~a\n" stx)
   #'3) ])
a)

However, I would suggest to start at least from syntax objects and
macro transformers in the guide and use define-syntax instead of
let-syntax.
https://docs.racket-lang.org/guide/stx-obj.html
https://docs.racket-lang.org/guide/macro-transformers.html

Best,
Shu-Hung

On Tue, Apr 3, 2018 at 1:31 PM, Kevin Forchione  wrote:
> Hi Guys,
> Does anyone have an analogous example for let-syntax to something as simple 
> as this?
>
> (let ([a 3]) a)
>
> Something like….
>
> (let-syntax ([a 3]) ….)
>
> At which point I’m stumped as to what expression in the body would return 3. 
> There are no examples in the Reference.
>
> Thanks!
> Kevin
>
> --
> 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] let-syntax example?

2018-04-03 Thread Kevin Forchione
Hi Guys,
Does anyone have an analogous example for let-syntax to something as simple as 
this?

(let ([a 3]) a)

Something like….

(let-syntax ([a 3]) ….) 

At which point I’m stumped as to what expression in the body would return 3. 
There are no examples in the Reference. 

Thanks!
Kevin

-- 
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 to match unbound identifier as literal in `syntax-parse`?

2018-04-03 Thread Ryan Culpepper

Here's one way:

  (~and z:id
   (~fail #:unless (free-identifier=? #'z #'zeta)
  "expected the identifier `zeta`"))

Another way is to make a syntax class (either specifically for `zeta` or 
parameterized by the identifier) that does the same check.


Ryan


On 4/3/18 8:33 AM, Matthew Butterick wrote:

Consider the difference in results below. How would I persuade `syntax-parse` 
to match literal identifiers in the `free-identifier=?` sense that 
`syntax-case` does? (and thereby allow unbound identifiers to be compared also)

IIUC, `#:literals` is too strict, as it insists that every listed identifier 
have a binding; `#:datum-literals` is too lenient, as it does not compare 
bindings at all, but rather just the symbol.

;

#lang racket
(require math rackunit)

(module mod racket
  (require (for-syntax syntax/parse))
  (define-syntax (mac stx)
(syntax-parse stx
  #:literals () ;; <- can't put `zeta` here, produces an error
  #:datum-literals (zeta)
  [(_ zeta) #''match]
  [else #''nope]))
  (provide mac))
(require 'mod)

(check-equal? (mac zeta) 'match)


(module mod2 racket
  (require (for-syntax syntax/parse))
  (define-syntax (mac2 stx)
(syntax-case stx (zeta)
  [(_ zeta) #''match]
  [else #''nope]))
  (provide mac2))
(require 'mod2)

(check-equal? (mac2 zeta) 'nope)



--
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] Why is there a space in the path to the Racket application on MacOSX?

2018-04-03 Thread Philip McGrath
One way the student languages from HtDP are different from most Racket DSLs
and langs is that, in the service of regularity and nice error messages,
they take away many convenient and powerful features of the full Racket
language. There is not really an "escape hatch" into full Racket, other
than importing libraries.

In contrast, most Racket language extensions either simply add new language
constructs to racket/base—think of the contract system, or the class-based
object system—or provide forms very similar to the racket/base form, but
with some refinement: #lang web-server transforms modules to make
continuations serializable at interaction points, or Typed Racket adds
syntax for specifying types. So starting your readers with a DSL doesn't
have to mean that they have a heavily-restricted language overall (or you
could be really ambitious and provide both a (require railroad) library and
a #lang railroad/beginner).

My first experience with Racket was through HtDP, which I loved, but, while
I understand the good reasons for the teaching languages (especially the
great error messages for beginners), I also have mixed feelings about them.
For students like me who already had some (imperative, non-parenthesized)
coding experience, there was a lot of complaining about conveniences of
other languages that seemed to be missing from *SL (e.g. I believe BSL
doesn't even have a mechanism for giving temporary names to values). When I
tell other people who've been through HtDP that Racket is now my language
of choice, I often get reactions like "Why would you want to use that for a
real project?" and have to explain that Racket is not the language we
learned in class. I think the text of the second edition may do a better
job at making that distinction: we mostly used the first edition with
little bits of the second, and certainly it was more confusing when
everything was just called "Scheme."

-Philip

On Mon, Apr 2, 2018 at 1:30 AM, Stephen Smith 
wrote:

> Railroad-simulation language, absolutely! One of the key reasons that
> Racket is on the top of the list. But what I didn't think of was to have
> the reader use the DSL first. I was initially planning to develop the DSL
> as a later part of the book - doing it the hard way perhaps.
>
> That has always been the one thing I wasn't crazy about with htdp,
> starting with the Beginning Student language, but that's just me
> personally. I understand the reasons for the approach. I look at it as, "I
> don't want training wheels - let me take the real thing for a ride!", and
> deal with the consequences later - an 
> I-wanna-know-what's-under-the-hood-right-now
> type of guy. For that reason, I never considered something like that for my
> book, but the more I think of it now, it kind of makes a lot of sense for
> my audience? I've always looked at it from an experienced programmer's
> point of view - which doesn't necessarily fit best here.
>
> Thanks Matthias for giving me this suggestion to think about. I might have
> a lot more work to do to get started using that approach, but in the long
> run, as you say, it might get them more interested in the programming part.
> Much appreciated food for thought.
>
>
>
> On Sunday, April 1, 2018 at 8:23:32 PM UTC-4, Matthias Felleisen wrote:
>>
>>
>> On Apr 1, 2018, at 12:57 PM, Stephen Smith  wrote:
>>
>> my (book) project is for model railroad hobbyists (many if not most who
>> have never programmed before).
>>
>>
>>
>> Have you considered the development of a railroad-simulation language
>> within Racket that fits your domain? If you can provide people with a
>> language that fits their problem area, they might be more interested in
>> learning more about programming per se. Since embedded DSLs usually have a
>> natural backdoor, this might be an approach that works well.
>>
>> In my current “hack your own language” course, some kids have gone a step
>> further. They are interested in music theory. So they implemented a
>> language for specifying languages in which students can then create
>> compositions of choral music and the composition is statically checked
>> before they even turn it in. The teacher can create a language per weekly
>> homework and teh students get to see the progression. At the same time,
>> there are several ways to dive into Racket from each level.
>>
>> — 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.
>

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

[racket-users] how to match unbound identifier as literal in `syntax-parse`?

2018-04-03 Thread Matthew Butterick
Consider the difference in results below. How would I persuade `syntax-parse` 
to match literal identifiers in the `free-identifier=?` sense that 
`syntax-case` does? (and thereby allow unbound identifiers to be compared also)

IIUC, `#:literals` is too strict, as it insists that every listed identifier 
have a binding; `#:datum-literals` is too lenient, as it does not compare 
bindings at all, but rather just the symbol.

;

#lang racket
(require math rackunit)

(module mod racket
  (require (for-syntax syntax/parse))
  (define-syntax (mac stx)
(syntax-parse stx
  #:literals () ;; <- can't put `zeta` here, produces an error
  #:datum-literals (zeta)
  [(_ zeta) #''match]
  [else #''nope]))
  (provide mac))
(require 'mod)

(check-equal? (mac zeta) 'match) 


(module mod2 racket
  (require (for-syntax syntax/parse))
  (define-syntax (mac2 stx)
(syntax-case stx (zeta)
  [(_ zeta) #''match]
  [else #''nope]))
  (provide mac2))
(require 'mod2)

(check-equal? (mac2 zeta) 'nope)

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