Re: Re: [racket-users] Pattern Matching in Macros | Meaning of dot

2016-03-14 Thread Jens Axel Søgaard
Yes, lambda expression have an implicit begin in the body.

> (begin . (1 2 3))
3

> (begin (1 2 3))
application: not a procedure;
expected a procedure that can be applied to arguments
given: 1
 arguments...:

Here (begin . (1 2 3)) is the same as (begin 1 2 3).

The docs for lambda are here:

http://docs.racket-lang.org/reference/lambda.html?q=lambda#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29

Note the body ...+ which means 1 or more bodies are allowed.

/Jens Axel







2016-03-14 18:37 GMT+01:00 Pedro Caldeira :

> Does that mean that lambda expressions have an implicit (begin …) block
> in them?
>
> (begin ((displayln 1) (displayln 2) (displayln 3))) leads to an error
>
> (begin . ((displayln 1) (displayln 2) (displayln 3))) displays to 1 2 3
>
> Thank you for the detailed explanation I think I get it now.
>
> On 13 Mar 2016, at 19:48, Jens Axel Søgaard  wrote:
>
> If we use
>
> (define-syntax define/memoized
>   (syntax-rules ()
> ((_ (name . args) . body)
>  (define name (memoize (lambda args  body))
>
> and body is bound to((displayln x) (displayln y) (displayz))
> then
>
> (lambda args  body)
>
> will become
>
> (lambda  ((displayln x) (displayln y) (displayz)))
>
> And when this function is called you will get an error since
>
> ((displayln x) ...)
>
> means apply the result of (displayln x) to (displayln y) (displayln x).
>
> /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.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
-- 
Jens Axel Søgaard

-- 
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: Re: [racket-users] Pattern Matching in Macros | Meaning of dot

2016-03-14 Thread Pedro Caldeira
Does that mean that lambda expressions have an implicit (begin …) block in them?

(begin ((displayln 1) (displayln 2) (displayln 3))) leads to an error

(begin . ((displayln 1) (displayln 2) (displayln 3))) displays to 1 2 3

Thank you for the detailed explanation I think I get it now.

> On 13 Mar 2016, at 19:48, Jens Axel Søgaard  > wrote:
> 
> If we use
> 
> (define-syntax define/memoized
>   (syntax-rules ()
> ((_ (name . args) . body)
>  (define name (memoize (lambda args  body))
> 
> and body is bound to((displayln x) (displayln y) (displayz)) 
> then
> 
> (lambda args  body)
> 
> will become
> 
> (lambda  ((displayln x) (displayln y) (displayz)))
> 
> And when this function is called you will get an error since
> 
> ((displayln x) ...) 
> 
> means apply the result of (displayln x) to (displayln y) (displayln x).
> 
> /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.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Pattern Matching in Macros | Meaning of dot

2016-03-13 Thread Pedro Caldeira
>Imagine (_ (foo x y z) (displayln x) (displayln y) (displayln z)) as the 
>actual syntax. The .body will be bound to the sequence of three diaplaylns and 
>this sequence will become the body of the lambda in the expansion.

So in this case body will be bound to the list ((displayln x) (displayln y) 
(displayz)) right?

So why do we use the '.' in the lambda declaration, couldn't we just write:

(define-syntax define/memoized
  (syntax-rules ()
((_ (name . args) . body)
 (define name (memoize (lambda args  body))

In Scala the head and tail of a list can be matched in the following way: x :: 
xs with x being the head and xs the tail, is the dot notation similar in 
purpose? Is the expression (x . xs) in Racket used to match x with the head of 
the S-expression and xs with the rest of the S-expression?

>(define-syntax define/memoized 
>  (syntax-rules () 
>((_ (name arg ...) form ... expr) 
> (define name (memoize (lambda (arg ...) form ... expr))

I see thank you for the different format, so to what does the form Thank you 
for taking the time to answer my question.

>Imagine (_ (foo x y z) (displayln x) (displayln y) (displayln z)) as the 
>actual syntax. The .body will be bound to the sequence of three diaplaylns and 
>this sequence will become the body of the lambda in the expansion.

So in this case body will be bound to the list ((displayln x) (displayln y) 
(displayz)) right?

So why do we use the '.' in the lambda declaration, couldn't we just write:

(define-syntax define/memoized
  (syntax-rules ()
((_ (name . args) . body)
 (define name (memoize (lambda args  body))

In Scala the head and tail of a list can be matched in the following way: x :: 
xs with x being the head and xs the tail, is the dot notation similar in 
purpose? Is the expression (x . xs) in Racket used to match x with the head of 
the S-expression and xs with the rest of the S-expression?

>(define-syntax define/memoized 
>  (syntax-rules () 
>((_ (name arg ...) form ... expr) 
> (define name (memoize (lambda (arg ...) form ... expr))

I see thank you for the different forma
Thank you for taking the time to answer my question.

>Imagine (_ (foo x y z) (displayln x) (displayln y) (displayln z)) as the 
>actual syntax. The .body will be bound to the sequence of three diaplaylns and 
>this sequence will become the body of the lambda in the expansion.

So in this case body will be bound to the list ((displayln x) (displayln y) 
(displayz)) right?

So why do we use the '.' in the lambda declaration, couldn't we just write:

(define-syntax define/memoized
  (syntax-rules ()
((_ (name . args) . body)
 (define name (memoize (lambda args  body))

In Scala the head and tail of a list can be matched in the following way: x :: 
xs with x being the head and xs the tail, is the dot notation similar in 
purpose? Is the expression (x . xs) in Racket used to match x with the head of 
the S-expression and xs with the rest of the S-expression?

>(define-syntax define/memoized 
>  (syntax-rules () 
>((_ (name arg ...) form ... expr) 
> (define name (memoize (lambda (arg ...) form ... expr))

I see thank you for the different format, the notation with the tree dots is 
more intuitive. As a side note in the racket reference: 
https://docs.racket-lang.org/reference/stx-patterns.html is the pattern with 
the three dots described by (pattern ...) or by (pattern ellipsis)?

@Jens Alex
Thank you for the clarification, I think it might confirm what I wrote above (I 
was replying while you answered)

Again, thank you all for your time!

-- 
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] Pattern Matching in Macros | Meaning of dot

2016-03-13 Thread Jens Axel Søgaard
The cons cell constructed by (cons 1 2)
is normally printed as (1 . 2).

The list created by (list 1 2 3) could also be created
as (cons 1 (cons 2 (cons 3 '(. It could be printed as
(1 . (2 . (3 . (.

Normally lists are simply printed as (1 2 3) though.
Notice that (1 . (list 2 3)) is the same list since (cons 1 (list 2 3))
is the same as (list 1 2 3).

In a round about way we have arrived at the pattern (name . args).

Suppose we have a syntax object representing a list (plus 1 2 3)
which could be written as (plus . (1 2 3)).
If we match this against (name . args) we see that name matches plus
and that args now matches (1 2 3).

To practise the dot notation you can use the repl (since the reader
understands dot notation.

> ( 1 . (2 3 4))
...


/Jens Axel






2016-03-13 16:05 GMT+01:00 Pedro Caldeira :

> Hello everyone,
>
> Since I've discovered the concept of metaprogramming I've been quite
> interested in Racket and its syntax extension capabilities.
>
> While searching for a memoization syntax extension I found a macro whose
> pattern extension remained unclear.
>
> (define-syntax define/memoized
>   (syntax-rules ()
> ((_ (name . args) . body)
>  (define name (memoize (lambda args . body))
>
> With the memoization function being defined as such:
>
> (define (memoize f)
>   (local ([define table (make-hash)])
> (lambda args
>   (dict-ref! table args (lambda () (apply f args))
>
> What does the '.' mean in '(name . args)' and '. body'?
>
> In a call like (define/memoized (foo x y z) (displayln "bar"))) I would
> imagine that name would be matched to foo; (x y z) to args and body to
> (display ln "bar") but why do we use the '. body' in the lambda expression
> at the end?
>
> Thank you for your attention.
>
> --
> 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.
>



-- 
-- 
Jens Axel Søgaard

-- 
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] Pattern Matching in Macros | Meaning of dot

2016-03-13 Thread Jos Koot
Consider (define/memoized (a b c d) form0 form1 form2)
. body allows the body to consist of more than one form.
Without the dot, syntax define/memoized would accept bodies of one form
only,
that is (define/memoized (a b c d) form0) would match, but
(define/memoized (a b c d) form0 form1 form2) would not match.
With the dot (define/memoized (a b c d) form0 form1 form2) matches too.

Another way to achieve the same is:

(define-syntax define/memoized
  (syntax-rules ()
((_ (name . args) form ... expr)
 (define name (memoize (lambda args form ... expr))

or

(define-syntax define/memoized
  (syntax-rules ()
((_ (name arg ...) form ... expr)
 (define name (memoize (lambda (arg ...) form ... expr))

Hope this helps.
Jos

-Original Message-
From: racket-users@googlegroups.com [mailto:racket-users@googlegroups.com]
On Behalf Of Pedro Caldeira
Sent: domingo, 13 de marzo de 2016 16:05
To: Racket Users
Subject: [racket-users] Pattern Matching in Macros | Meaning of dot

Hello everyone,

Since I've discovered the concept of metaprogramming I've been quite
interested in Racket and its syntax extension capabilities.

While searching for a memoization syntax extension I found a macro whose
pattern extension remained unclear.

(define-syntax define/memoized
  (syntax-rules ()
((_ (name . args) . body)
 (define name (memoize (lambda args . body))

With the memoization function being defined as such:

(define (memoize f)
  (local ([define table (make-hash)])
(lambda args
  (dict-ref! table args (lambda () (apply f args))

What does the '.' mean in '(name . args)' and '. body'?

In a call like (define/memoized (foo x y z) (displayln "bar"))) I would
imagine that name would be matched to foo; (x y z) to args and body to
(display ln "bar") but why do we use the '. body' in the lambda expression
at the end?

Thank you for your attention.

-- 
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] Pattern Matching in Macros | Meaning of dot

2016-03-13 Thread Matthias Felleisen

> On Mar 13, 2016, at 11:05 AM, Pedro Caldeira  
> wrote:
> 
> Hello everyone,
> 
> Since I've discovered the concept of metaprogramming I've been quite 
> interested in Racket and its syntax extension capabilities.
> 
> While searching for a memoization syntax extension I found a macro whose 
> pattern extension remained unclear.
> 
> (define-syntax define/memoized
>  (syntax-rules ()
>((_ (name . args) . body)
> (define name (memoize (lambda args . body))
> 
> With the memoization function being defined as such:
> 
> (define (memoize f)
>  (local ([define table (make-hash)])
>(lambda args
>  (dict-ref! table args (lambda () (apply f args))
> 
> What does the '.' mean in '(name . args)' and '. body’?


The parameter spec "(name . args)" means at least one argument (name) followed 
by an arbitrary number of additional (regular) arguments. 

The ". body” in the syntax pattern denotes ‘the rest of the given Syntax 
expression (!= S-expression). 


> In a call like (define/memoized (foo x y z) (displayln "bar"))) I would 
> imagine that name would be matched to foo; (x y z) to args and body to 
> (display ln "bar") but why do we use the '. body' in the lambda expression at 
> the end?


Imagine (_ (foo x y z) (displayln x) (displayln y) (displayln z)) as the actual 
syntax. The .body will be bound to the sequence of three diaplaylns and this 
sequence will become the body of the lambda in the expansion. 

;; — 

You may want to read up on Racket’s syntax first, starting with the Guide 
documentation. Meta-programming is about manipulation syntax and generating 
syntax and making sure bindings work out. 

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


[racket-users] Pattern Matching in Macros | Meaning of dot

2016-03-13 Thread Pedro Caldeira
Hello everyone,

Since I've discovered the concept of metaprogramming I've been quite interested 
in Racket and its syntax extension capabilities.

While searching for a memoization syntax extension I found a macro whose 
pattern extension remained unclear.

(define-syntax define/memoized
  (syntax-rules ()
((_ (name . args) . body)
 (define name (memoize (lambda args . body))

With the memoization function being defined as such:

(define (memoize f)
  (local ([define table (make-hash)])
(lambda args
  (dict-ref! table args (lambda () (apply f args))

What does the '.' mean in '(name . args)' and '. body'?

In a call like (define/memoized (foo x y z) (displayln "bar"))) I would imagine 
that name would be matched to foo; (x y z) to args and body to (display ln 
"bar") but why do we use the '. body' in the lambda expression at the end?

Thank you for your attention.

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