Re: [racket-users] Built-in function for accessing fields of a composite type

2018-10-24 Thread Hassan Shahin
Many thanks Matthew!

I couldn't believe I missed that. These are given for free. Yes.
BTW, I am a big fan.

Best

On Thursday, October 25, 2018 at 5:12:13 AM UTC+3, Matthew Flatt wrote:
>
> Since `tag` is the constructor name and `children` is the field name, 
> `tag-children` is defined as the accessor function for the `children` 
> field. 
>
> I recommend using `type-case`, though. 
>
> At Wed, 24 Oct 2018 19:05:44 -0700 (PDT), Hassan Shahin wrote: 
> > using #lang Plait, I have defined these two types: 
> > 
> > (define-type Property 
> >   (property [name : Symbol] 
> > [value : (Boxof Symbol)])) 
> > 
> > (define-type Tag 
> >   (tag 
> >[name : String] 
> >[id : String] 
> >[properties : (Listof Property)] 
> >[value : Content-Type] 
> >[children : (Boxof (Listof Tag))])) 
> > 
> > I also defined an instances of types Property and Tag as follows: 
> > 
> > (define r (property 'color (box 'red))) 
> > (define b (property 'color (box 'blue))) 
> > (define g (property 'color (box 'green))) 
> > (define s (property 'size (box '50s))) 
> > 
> > (define t2 (tag "div" "2" 
> > (list r s) 
> > (text "This is a text") 
> > (box empty))) 
> > 
> > (define t3 (tag "div" "3" 
> > '() 
> > (text "This is a text") 
> > (box empty))) 
> > 
> > (define t1 (tag "div" "1" 
> > (list r s) 
> > (text "This is a text") 
> > (box (list t2 t3 
> > 
> > Now, I want to define a function (insert-tags-in-tag : Tag -> (Listof 
> Tag) 
> > ->Tag), that, say, inserts t2 and t3 in t1 as children. 
> > I can do that, if I know how to access children field of the Tag type. 
> > 
> > The signature of my function is: 
> > (define (insert-tags-in-tag (t1  (list t2 t3)) 
> >  (...)) 
> > 
> > I can't make the implementation of the (...), as I am trying to see if 
> > Plait provides an accessor function for the fields. Otherwise, I guess, 
> I 
> > will need to implement my own interp. 
> > I think I searched the documentation, but couldn't find mention for such 
> a 
> > case. 
> > 
> > Appreciate your help. 
> > 
> > -- 
> > 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...@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] Built-in function for accessing fields of a composite type

2018-10-24 Thread Matthew Flatt
Since `tag` is the constructor name and `children` is the field name,
`tag-children` is defined as the accessor function for the `children`
field.

I recommend using `type-case`, though.

At Wed, 24 Oct 2018 19:05:44 -0700 (PDT), Hassan Shahin wrote:
> using #lang Plait, I have defined these two types:
> 
> (define-type Property
>   (property [name : Symbol]
> [value : (Boxof Symbol)]))
> 
> (define-type Tag
>   (tag
>[name : String]
>[id : String]
>[properties : (Listof Property)]
>[value : Content-Type]
>[children : (Boxof (Listof Tag))]))
> 
> I also defined an instances of types Property and Tag as follows:
> 
> (define r (property 'color (box 'red)))
> (define b (property 'color (box 'blue)))
> (define g (property 'color (box 'green)))
> (define s (property 'size (box '50s)))
> 
> (define t2 (tag "div" "2"
> (list r s)
> (text "This is a text")
> (box empty)))
> 
> (define t3 (tag "div" "3"
> '()
> (text "This is a text")
> (box empty)))
> 
> (define t1 (tag "div" "1"
> (list r s)
> (text "This is a text")
> (box (list t2 t3
> 
> Now, I want to define a function (insert-tags-in-tag : Tag -> (Listof Tag) 
> ->Tag), that, say, inserts t2 and t3 in t1 as children.
> I can do that, if I know how to access children field of the Tag type.
> 
> The signature of my function is:
> (define (insert-tags-in-tag (t1  (list t2 t3))
>  (...))
> 
> I can't make the implementation of the (...), as I am trying to see if 
> Plait provides an accessor function for the fields. Otherwise, I guess, I 
> will need to implement my own interp. 
> I think I searched the documentation, but couldn't find mention for such a 
> case.
> 
> Appreciate your help.
> 
> -- 
> 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] Built-in function for accessing fields of a composite type

2018-10-24 Thread Hassan Shahin
using #lang Plait, I have defined these two types:

(define-type Property
  (property [name : Symbol]
[value : (Boxof Symbol)]))

(define-type Tag
  (tag
   [name : String]
   [id : String]
   [properties : (Listof Property)]
   [value : Content-Type]
   [children : (Boxof (Listof Tag))]))

I also defined an instances of types Property and Tag as follows:

(define r (property 'color (box 'red)))
(define b (property 'color (box 'blue)))
(define g (property 'color (box 'green)))
(define s (property 'size (box '50s)))

(define t2 (tag "div" "2"
(list r s)
(text "This is a text")
(box empty)))

(define t3 (tag "div" "3"
'()
(text "This is a text")
(box empty)))

(define t1 (tag "div" "1"
(list r s)
(text "This is a text")
(box (list t2 t3

Now, I want to define a function (insert-tags-in-tag : Tag -> (Listof Tag) 
->Tag), that, say, inserts t2 and t3 in t1 as children.
I can do that, if I know how to access children field of the Tag type.

The signature of my function is:
(define (insert-tags-in-tag (t1  (list t2 t3))
 (...))

I can't make the implementation of the (...), as I am trying to see if 
Plait provides an accessor function for the fields. Otherwise, I guess, I 
will need to implement my own interp. 
I think I searched the documentation, but couldn't find mention for such a 
case.

Appreciate your help.

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