Re: [racket-users] Pattern Matching

2020-10-06 Thread George Neuner


On 10/6/2020 10:41 AM, Beatriz Moreira wrote:

Hello,

Yes, my idea is to check the type of the parameters. I still have to 
add the types to my code, but I was trying to see if I could do it 
without them (just pattern matching), as the functions of type *f* are 
in the declaration of the contract.


Can you access this link? 
https://bitbucket.org/beatrizmoreira/msc/src/master/fwsollast.rkt 



Thank you for your answer :D


I thought you were working with match in Racket ... Redex is its own 
language, and I haven't done anything with it.


Sorry, I can't help.
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/caef9073-1d94-6799-cfc6-730058cc1c7d%40comcast.net.


Re: [racket-users] Pattern Matching

2020-10-06 Thread Beatriz Moreira
Hello,

Yes, my idea is to check the type of the parameters. I still have to add 
the types to my code, but I was trying to see if I could do it without them 
(just pattern matching), as the functions of type *f* are in the 
declaration of the contract.

Can you access this link? 
https://bitbucket.org/beatrizmoreira/msc/src/master/fwsollast.rkt

Thank you for your answer :D
A terça-feira, 29 de setembro de 2020 à(s) 17:20:58 UTC+1, 
wesley@gmail.com escreveu:

> Hello Beatriz,
>
> Is this something like contracts with parameters? That sounds neat, 
> actually.
>
> Anyway, this seems more like syntax analysis rather than string matching, 
> but I'm not entirely certain as to what your approach is. I can't access 
> the file you linked.
>
> If this _is_ syntax analysis, then maybe a macro using `syntax-case` or 
> `syntax-parse` would serve you better? Again, I'm not exactly sure what 
> your approach is, and I'm sorry for that.
> On Monday, September 28, 2020 at 3:31:11 PM UTC-4 gneuner2 wrote:
>
>>
>> On 9/28/2020 10:50 AM, Beatriz Moreira wrote:
>>
>> Hello, 
>> I would like to know how do I match multiple variables to a regular 
>> expression.
>> My idea is to match every *f* variables (f...) to an *f* in ((contract C 
>> ((T x) ...) ((T f)) ...) ... ).
>> I am trying to implement a core language for smart contracts in Racket, 
>> but I need to have as pre condition in my reduction rules that the *f* is 
>> a function in one of the contracts.
>> Thank you! :) 
>>
>>
>> 'regexp-match*' outputs a list of matched strings, and match has an apply 
>> clause: '(app *expr* pats ...)' which evaluates *expr* and tries to 
>> match the output.  My first thought would be to try something like:
>>
>>   match ( inp )
>> :
>> ((app (regexp-match* pattern inp) match1 match2 ... )
>>... )
>> :
>>
>> Caveat ... I've never tried to do this.  If match doesn't like this 
>> approach (or something close to it), then I would separately perform the 
>> regexp-match* and the pattern match on its output list.
>>
>> Hope this helps,
>> George
>>
>> https://docs.racket-lang.org/reference/regexp.html
>> https://docs.racket-lang.org/reference/match.html
>>
>

-- 
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/76283498-990f-4c7c-b445-0d40f10108can%40googlegroups.com.


Re: [racket-users] Pattern Matching

2020-10-06 Thread Beatriz Moreira
Hello,
I don't think this is what im looking for, but thank you very much :D 

A segunda-feira, 28 de setembro de 2020 à(s) 20:31:11 UTC+1, gneuner2 
escreveu:

>
> On 9/28/2020 10:50 AM, Beatriz Moreira wrote:
>
> Hello, 
> I would like to know how do I match multiple variables to a regular 
> expression.
> My idea is to match every *f* variables (f...) to an *f* in ((contract C 
> ((T x) ...) ((T f)) ...) ... ).
> I am trying to implement a core language for smart contracts in Racket, 
> but I need to have as pre condition in my reduction rules that the *f* is 
> a function in one of the contracts.
> Thank you! :) 
>
>
> 'regexp-match*' outputs a list of matched strings, and match has an apply 
> clause: '(app *expr* pats ...)' which evaluates *expr* and tries to match 
> the output.  My first thought would be to try something like:
>
>   match ( inp )
> :
> ((app (regexp-match* pattern inp) match1 match2 ... )
>... )
> :
>
> Caveat ... I've never tried to do this.  If match doesn't like this 
> approach (or something close to it), then I would separately perform the 
> regexp-match* and the pattern match on its output list.
>
> Hope this helps,
> George
>
> https://docs.racket-lang.org/reference/regexp.html
> https://docs.racket-lang.org/reference/match.html
>

-- 
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/3d0e5945-a81e-4424-99c5-96f6d0b7b198n%40googlegroups.com.


Re: [racket-users] Pattern Matching

2020-09-29 Thread Wesley Bitomski
Hello Beatriz,

Is this something like contracts with parameters? That sounds neat, 
actually.

Anyway, this seems more like syntax analysis rather than string matching, 
but I'm not entirely certain as to what your approach is. I can't access 
the file you linked.

If this _is_ syntax analysis, then maybe a macro using `syntax-case` or 
`syntax-parse` would serve you better? Again, I'm not exactly sure what 
your approach is, and I'm sorry for that.
On Monday, September 28, 2020 at 3:31:11 PM UTC-4 gneuner2 wrote:

>
> On 9/28/2020 10:50 AM, Beatriz Moreira wrote:
>
> Hello, 
> I would like to know how do I match multiple variables to a regular 
> expression.
> My idea is to match every *f* variables (f...) to an *f* in ((contract C 
> ((T x) ...) ((T f)) ...) ... ).
> I am trying to implement a core language for smart contracts in Racket, 
> but I need to have as pre condition in my reduction rules that the *f* is 
> a function in one of the contracts.
> Thank you! :) 
>
>
> 'regexp-match*' outputs a list of matched strings, and match has an apply 
> clause: '(app *expr* pats ...)' which evaluates *expr* and tries to match 
> the output.  My first thought would be to try something like:
>
>   match ( inp )
> :
> ((app (regexp-match* pattern inp) match1 match2 ... )
>... )
> :
>
> Caveat ... I've never tried to do this.  If match doesn't like this 
> approach (or something close to it), then I would separately perform the 
> regexp-match* and the pattern match on its output list.
>
> Hope this helps,
> George
>
> https://docs.racket-lang.org/reference/regexp.html
> https://docs.racket-lang.org/reference/match.html
>

-- 
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/236fecf4-e123-472e-9c86-dc9c88dc71c5n%40googlegroups.com.


Re: [racket-users] Pattern Matching

2020-09-28 Thread George Neuner


On 9/28/2020 10:50 AM, Beatriz Moreira wrote:

Hello,
I would like to know how do I match multiple variables to a regular 
expression.
My idea is to match every *f* variables (f...) to an *f* in ((contract 
C ((T x) ...) ((T f)) ...) ... ).
I am trying to implement a core language for smart contracts in 
Racket, but I need to have as pre condition in my reduction rules that 
the *f* is a function in one of the contracts.

Thank you! :)


'regexp-match*' outputs a list of matched strings, and match has an 
apply clause: '(app /expr/ pats ...)' which evaluates /expr/ and tries 
to match the output.  My first thought would be to try something like:


  match ( inp )
    :
    ((app (regexp-match* pattern inp) match1 match2 ... )
   ... )
    :

Caveat ... I've never tried to do this.  If match doesn't like this 
approach (or something close to it), then I would separately perform the 
regexp-match* and the pattern match on its output list.


Hope this helps,
George

https://docs.racket-lang.org/reference/regexp.html
https://docs.racket-lang.org/reference/match.html

--
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/22de3af2-b64f-73c0-fe12-a832dccf6086%40comcast.net.


[racket-users] Pattern Matching

2020-09-28 Thread Beatriz Moreira
Hello,
I would like to know how do I match multiple variables to a regular 
expression.
My idea is to match every *f* variables (f...) to an *f* in ((contract C 
((T x) ...) ((T f)) ...) ... ).
I am trying to implement a core language for smart contracts in Racket, but 
I need to have as pre condition in my reduction rules that the *f* is a 
function in one of the contracts.
Thank you! :) 


https://bitbucket.org/beatrizmoreira/msc/src/master/fwsollast.rkt

-- 
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/e4bc2e79-c375-4dad-8d49-2f308ad53c19n%40googlegroups.com.


Re: [racket-users] Pattern matching as computation

2019-06-27 Thread David Storrs
Oh, neat.  Thank you, Sorawee.

On Thu, Jun 27, 2019 at 7:26 PM Sorawee Porncharoenwase <
sorawee.pw...@gmail.com> wrote:

> If you want only one match, it seems what you really want is ~seq-like
> form from syntax/parse. E.g.,
>
> #lang racket
>
> (require syntax/parse)
>
> (define ->value syntax->datum)
>
> (define (apply-lift op args)
>   (datum->syntax #f (apply op (->value args
>
> (syntax-parse (vector 1 2 3 4)
>   [#(a {~and {~seq {~between b 2 2} ...}
>  {~bind [x (apply-lift + #'(b ...))]}} c)
>(->value #'(a (b ...) c x))])
> ;; => '(1 (2 3) 4 5)
>
> (struct person (name age) #:prefab)
> (struct horse (color owner) #:prefab)
>
> (syntax-parse (vector 'brown 'bob 23)
>   [#(the-color {~and {~seq args ...}
>  {~bind [the-owner (apply-lift person #'(args ...))]}})
>(->value (apply-lift horse #'(the-color the-owner)))])
> ;; => '#s(horse brown #s(person bob 23))
>
>
>
>
>
> On Thu, Jun 27, 2019 at 3:31 PM David Storrs 
> wrote:
>
>>
>>
>> On Thu, Jun 27, 2019 at 5:13 PM Eric Griffis  wrote:
>>
>>> On Thu, Jun 27, 2019 at 11:56 AM David Storrs 
>>> wrote:
>>> >
>>> > Suppose instead I wanted to have a pattern like so (this does not
>>> work):
>>> >
>>> > (match (vector 1 2 3 4)
>>> >   [(vector a (app + b ..2 x) c) (list a b c x)]
>>> > ; => '(1 (2 3) 4 5))  ;  NB:  does not work
>>>
>>> We have a few problems here.
>>>
>>> The pattern (vector a ? c) does not match the shape of the val-expr
>>> (vector 1 2 3 4).
>>>
>>> In the second sub-pattern (app + b ..2 x), Racket complains `..2' is an
>>> "incorrect use of ... in pattern" because every sub-pattern of `app' must
>>> independently match the result of (+ 2).
>>>
>>> To get the expected result '(1 (2 3) 4 5) from the actual result (list
>>> a b
>>> c x), we'd need the following bindings:
>>>
>>> a --> 1
>>> b --> '(2 3)
>>> c --> 4
>>> x --> 5
>>>
>>> At this point, I'm not sure what you're trying to do. Maybe you're trying
>>> to bind `b' to '(2 3), then `x' with the sum (+ 2 3), then `c' with 4.
>>>
>>> Here's one way to do it from within a single pattern:
>>>
>>> (match (vector 1 2 3 4)
>>>   [(and (vector a _ _ c)
>>> (app (λ (vec)
>>>(match vec
>>>  [(vector _ p q _)
>>>   (list (list p q) (+ p q))]))
>>>  (list b x)))
>>>(list a b c x)])
>>>
>>> > Less trivially, I'd like to be able to do something like this:
>>> >
>>> > #lang racket
>>> > (struct person (name age) #:prefab)
>>> > (struct horse (color owner) #:prefab)
>>> >
>>> > ; The following does not work
>>> > (match (vector 'brown 'bob 23)
>>> >   [(vector the-color (app (curry apply person) args ... the-owner))
>>> >(horse the-color the-owner)])
>>>
>>> Here's an adaptation of the above technique that works:
>>>
>>> (match (vector 'brown 'bob 23)
>>>   [(and (vector the-color _ _)
>>> (app (λ (vec)
>>>(match vec
>>>  [(vector _ name age)
>>>   (person name age)]))
>>>  the-owner))
>>>(horse the-color the-owner)])
>>>
>>
>> Makes sense -- it's two matches,  but still within one sexp.  Thanks.
>>
>>
>>> Incidentally, Algebraic Racket is designed for this:
>>>
>>> #lang algebraic/racket/base
>>>
>>> (require (prefix-in algebraic- algebraic/racket/base/forms))
>>>
>>> (algebraic-case (vector 1 2 3 4)
>>>   [#(a p q c)
>>>#:with b (list p q)
>>>#:with x (+ p q)
>>>(list a b c x)])
>>>
>>> (algebraic-case (vector 'brown 'bob 23)
>>>   [#(the-color name age)
>>>#:with the-owner (person name age)
>>>(horse the-color the-owner)])
>>>
>>> If a #:with directive fails, the overall match fails:
>>>
>>> (algebraic-case (vector 'brown 'bob 23)
>>>   [#(the-color name age)
>>>#:with the-owner #f
>>>#:with (person _ _) the-owner
>>>(horse the-color the-owner)])
>>>
>>>
>> That is drop-dead sexy.  Thanks, Eric.
>>
>>
>> Eric
>>>
>>> --
>> 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/CAE8gKocxGYu0vKvUzW%3Dfc1X7acXmZjCmz6mPKErDWvNmjKLmZw%40mail.gmail.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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAE8gKocj-t%2B8WUVyZq0d6OsjtUk5XxL170RDf_xvJp%2BYhO2i8w%40mail.gmail.com.
For more options, visit 

Re: [racket-users] Pattern matching as computation

2019-06-27 Thread Sorawee Porncharoenwase
If you want only one match, it seems what you really want is ~seq-like form
from syntax/parse. E.g.,

#lang racket

(require syntax/parse)

(define ->value syntax->datum)

(define (apply-lift op args)
  (datum->syntax #f (apply op (->value args

(syntax-parse (vector 1 2 3 4)
  [#(a {~and {~seq {~between b 2 2} ...}
 {~bind [x (apply-lift + #'(b ...))]}} c)
   (->value #'(a (b ...) c x))])
;; => '(1 (2 3) 4 5)

(struct person (name age) #:prefab)
(struct horse (color owner) #:prefab)

(syntax-parse (vector 'brown 'bob 23)
  [#(the-color {~and {~seq args ...}
 {~bind [the-owner (apply-lift person #'(args ...))]}})
   (->value (apply-lift horse #'(the-color the-owner)))])
;; => '#s(horse brown #s(person bob 23))





On Thu, Jun 27, 2019 at 3:31 PM David Storrs  wrote:

>
>
> On Thu, Jun 27, 2019 at 5:13 PM Eric Griffis  wrote:
>
>> On Thu, Jun 27, 2019 at 11:56 AM David Storrs 
>> wrote:
>> >
>> > Suppose instead I wanted to have a pattern like so (this does not work):
>> >
>> > (match (vector 1 2 3 4)
>> >   [(vector a (app + b ..2 x) c) (list a b c x)]
>> > ; => '(1 (2 3) 4 5))  ;  NB:  does not work
>>
>> We have a few problems here.
>>
>> The pattern (vector a ? c) does not match the shape of the val-expr
>> (vector 1 2 3 4).
>>
>> In the second sub-pattern (app + b ..2 x), Racket complains `..2' is an
>> "incorrect use of ... in pattern" because every sub-pattern of `app' must
>> independently match the result of (+ 2).
>>
>> To get the expected result '(1 (2 3) 4 5) from the actual result (list a
>> b
>> c x), we'd need the following bindings:
>>
>> a --> 1
>> b --> '(2 3)
>> c --> 4
>> x --> 5
>>
>> At this point, I'm not sure what you're trying to do. Maybe you're trying
>> to bind `b' to '(2 3), then `x' with the sum (+ 2 3), then `c' with 4.
>>
>> Here's one way to do it from within a single pattern:
>>
>> (match (vector 1 2 3 4)
>>   [(and (vector a _ _ c)
>> (app (λ (vec)
>>(match vec
>>  [(vector _ p q _)
>>   (list (list p q) (+ p q))]))
>>  (list b x)))
>>(list a b c x)])
>>
>> > Less trivially, I'd like to be able to do something like this:
>> >
>> > #lang racket
>> > (struct person (name age) #:prefab)
>> > (struct horse (color owner) #:prefab)
>> >
>> > ; The following does not work
>> > (match (vector 'brown 'bob 23)
>> >   [(vector the-color (app (curry apply person) args ... the-owner))
>> >(horse the-color the-owner)])
>>
>> Here's an adaptation of the above technique that works:
>>
>> (match (vector 'brown 'bob 23)
>>   [(and (vector the-color _ _)
>> (app (λ (vec)
>>(match vec
>>  [(vector _ name age)
>>   (person name age)]))
>>  the-owner))
>>(horse the-color the-owner)])
>>
>
> Makes sense -- it's two matches,  but still within one sexp.  Thanks.
>
>
>> Incidentally, Algebraic Racket is designed for this:
>>
>> #lang algebraic/racket/base
>>
>> (require (prefix-in algebraic- algebraic/racket/base/forms))
>>
>> (algebraic-case (vector 1 2 3 4)
>>   [#(a p q c)
>>#:with b (list p q)
>>#:with x (+ p q)
>>(list a b c x)])
>>
>> (algebraic-case (vector 'brown 'bob 23)
>>   [#(the-color name age)
>>#:with the-owner (person name age)
>>(horse the-color the-owner)])
>>
>> If a #:with directive fails, the overall match fails:
>>
>> (algebraic-case (vector 'brown 'bob 23)
>>   [#(the-color name age)
>>#:with the-owner #f
>>#:with (person _ _) the-owner
>>(horse the-color the-owner)])
>>
>>
> That is drop-dead sexy.  Thanks, Eric.
>
>
> Eric
>>
>> --
> 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/CAE8gKocxGYu0vKvUzW%3Dfc1X7acXmZjCmz6mPKErDWvNmjKLmZw%40mail.gmail.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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CADcuegtXfWvz9n42Sa5KdWLGy5-ZDDQP7NM6gcT0ShGwKVcK5g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Pattern matching as computation

2019-06-27 Thread David Storrs
On Thu, Jun 27, 2019 at 5:13 PM Eric Griffis  wrote:

> On Thu, Jun 27, 2019 at 11:56 AM David Storrs 
> wrote:
> >
> > Suppose instead I wanted to have a pattern like so (this does not work):
> >
> > (match (vector 1 2 3 4)
> >   [(vector a (app + b ..2 x) c) (list a b c x)]
> > ; => '(1 (2 3) 4 5))  ;  NB:  does not work
>
> We have a few problems here.
>
> The pattern (vector a ? c) does not match the shape of the val-expr
> (vector 1 2 3 4).
>
> In the second sub-pattern (app + b ..2 x), Racket complains `..2' is an
> "incorrect use of ... in pattern" because every sub-pattern of `app' must
> independently match the result of (+ 2).
>
> To get the expected result '(1 (2 3) 4 5) from the actual result (list a b
> c x), we'd need the following bindings:
>
> a --> 1
> b --> '(2 3)
> c --> 4
> x --> 5
>
> At this point, I'm not sure what you're trying to do. Maybe you're trying
> to bind `b' to '(2 3), then `x' with the sum (+ 2 3), then `c' with 4.
>
> Here's one way to do it from within a single pattern:
>
> (match (vector 1 2 3 4)
>   [(and (vector a _ _ c)
> (app (λ (vec)
>(match vec
>  [(vector _ p q _)
>   (list (list p q) (+ p q))]))
>  (list b x)))
>(list a b c x)])
>
> > Less trivially, I'd like to be able to do something like this:
> >
> > #lang racket
> > (struct person (name age) #:prefab)
> > (struct horse (color owner) #:prefab)
> >
> > ; The following does not work
> > (match (vector 'brown 'bob 23)
> >   [(vector the-color (app (curry apply person) args ... the-owner))
> >(horse the-color the-owner)])
>
> Here's an adaptation of the above technique that works:
>
> (match (vector 'brown 'bob 23)
>   [(and (vector the-color _ _)
> (app (λ (vec)
>(match vec
>  [(vector _ name age)
>   (person name age)]))
>  the-owner))
>(horse the-color the-owner)])
>

Makes sense -- it's two matches,  but still within one sexp.  Thanks.


> Incidentally, Algebraic Racket is designed for this:
>
> #lang algebraic/racket/base
>
> (require (prefix-in algebraic- algebraic/racket/base/forms))
>
> (algebraic-case (vector 1 2 3 4)
>   [#(a p q c)
>#:with b (list p q)
>#:with x (+ p q)
>(list a b c x)])
>
> (algebraic-case (vector 'brown 'bob 23)
>   [#(the-color name age)
>#:with the-owner (person name age)
>(horse the-color the-owner)])
>
> If a #:with directive fails, the overall match fails:
>
> (algebraic-case (vector 'brown 'bob 23)
>   [#(the-color name age)
>#:with the-owner #f
>#:with (person _ _) the-owner
>(horse the-color the-owner)])
>
>
That is drop-dead sexy.  Thanks, Eric.


Eric
>
>

-- 
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/CAE8gKocxGYu0vKvUzW%3Dfc1X7acXmZjCmz6mPKErDWvNmjKLmZw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Pattern matching as computation

2019-06-27 Thread Eric Griffis
On Thu, Jun 27, 2019 at 11:56 AM David Storrs 
wrote:
>
> Suppose instead I wanted to have a pattern like so (this does not work):
>
> (match (vector 1 2 3 4)
>   [(vector a (app + b ..2 x) c) (list a b c x)]
> ; => '(1 (2 3) 4 5))  ;  NB:  does not work

We have a few problems here.

The pattern (vector a ? c) does not match the shape of the val-expr
(vector 1 2 3 4).

In the second sub-pattern (app + b ..2 x), Racket complains `..2' is an
"incorrect use of ... in pattern" because every sub-pattern of `app' must
independently match the result of (+ 2).

To get the expected result '(1 (2 3) 4 5) from the actual result (list a b
c x), we'd need the following bindings:

a --> 1
b --> '(2 3)
c --> 4
x --> 5

At this point, I'm not sure what you're trying to do. Maybe you're trying
to bind `b' to '(2 3), then `x' with the sum (+ 2 3), then `c' with 4.

Here's one way to do it from within a single pattern:

(match (vector 1 2 3 4)
  [(and (vector a _ _ c)
(app (λ (vec)
   (match vec
 [(vector _ p q _)
  (list (list p q) (+ p q))]))
 (list b x)))
   (list a b c x)])

> Less trivially, I'd like to be able to do something like this:
>
> #lang racket
> (struct person (name age) #:prefab)
> (struct horse (color owner) #:prefab)
>
> ; The following does not work
> (match (vector 'brown 'bob 23)
>   [(vector the-color (app (curry apply person) args ... the-owner))
>(horse the-color the-owner)])

Here's an adaptation of the above technique that works:

(match (vector 'brown 'bob 23)
  [(and (vector the-color _ _)
(app (λ (vec)
   (match vec
 [(vector _ name age)
  (person name age)]))
 the-owner))
   (horse the-color the-owner)])

Incidentally, Algebraic Racket is designed for this:

#lang algebraic/racket/base

(require (prefix-in algebraic- algebraic/racket/base/forms))

(algebraic-case (vector 1 2 3 4)
  [#(a p q c)
   #:with b (list p q)
   #:with x (+ p q)
   (list a b c x)])

(algebraic-case (vector 'brown 'bob 23)
  [#(the-color name age)
   #:with the-owner (person name age)
   (horse the-color the-owner)])

If a #:with directive fails, the overall match fails:

(algebraic-case (vector 'brown 'bob 23)
  [#(the-color name age)
   #:with the-owner #f
   #:with (person _ _) the-owner
   (horse the-color the-owner)])

Eric

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


[racket-users] Pattern matching as computation

2019-06-27 Thread David Storrs
This works:

(match (vector 1 2 3 4)
 [(vector a b (app add1 c) d)
  (list a b c d)])
; => '(1 2 4 4)

Suppose instead I wanted to have a pattern like so (this does not work):

(match (vector 1 2 3 4)
  [(vector a (app + b ..2 x) c) (list a b c x)]
; => '(1 (2 3) 4 5))  ;  NB:  does not work

Is there a way to do this?


Less trivially, I'd like to be able to do something like this:

#lang racket
(struct person (name age) #:prefab)
(struct horse (color owner) #:prefab)

; The following does not work
(match (vector 'brown 'bob 23)
  [(vector the-color (app (curry apply person) args ... the-owner))
   (horse the-color the-owner)])

I've been through the pattern matching docs in some detail and tried
various things, but my brain-fu is weak.

-- 
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/CAE8gKoeJL-YzSQ_SZ9te9OQ0HgmKiXW19624E8H6fScfFB2zpA%40mail.gmail.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 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.