Home > Archive > Scheme > September 2006 > print a structure
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| markww 2006-09-28, 4:02 am |
| Hi,
I cannot figure out how to print a 'returned' structure, my mind has
been corrupted by c/c++. I have a scheme function like:
(print-struct #t)
(define-struct tree (l r m) (make-inspector)) ; l = left, r = right, m
= myself, whatever symbol I am.
(define-struct leaf (info) (make-inspector)) ; info = whatever symbol
I am.
(define (parse s)
(cond
[(symbol? s) (make-leaf s)]
[(symbol=? (first s) '+) (make-tree (parse (second s))
(parse (third
s))]
)
)
which just keeps building elements of a tree. Once parse() 'returns'
how the heck do I traverse my returned tree!? Alright so I tried
wrapping the call to parse() in a printTree() function:
(printTree (parse s))
yes it goes into the function passing the tree I suppose.
(define (printTree tree)
(cond
[(tree? tree)
(display "I thought I should recursively call print on the
children here..\n")
(printTree tree-l)
(printTree tree-r)]
[(leaf? tree)
(display "I just want to print my tree...\n")
)
)
but yeah #1 even though I know my app built one parent node with two
leaves, the print statement only ever shows that I have one top tree
node, no children leaves.
Second - even if that were working, how could I print the value of the
leave!? I tried:
(display tree-info) ; when it's a leaf
but I just get an error at runtime about it. My ness is
unparalleled. If anyone can give me a hint that would be absolutely
wonderful as I've been googling for about 3 hours and can't find any
info on how to 'print' a structure in this manner.
Thanks
| |
| Jens Axel Søgaard 2006-09-28, 4:02 am |
| markww skrev:
> Hi,
>
> I cannot figure out how to print a 'returned' structure, my mind has
> been corrupted by c/c++. I have a scheme function like:
>
> (print-struct #t)
> (define-struct tree (l r m) (make-inspector)) ; l = left, r = right, m
> = myself, whatever symbol I am.
> (define-struct leaf (info) (make-inspector)) ; info = whatever symbol
> I am.
(print-struct #t)
(define-struct tree (l r m) (make-inspector)) ; l = left, r = right, m
(define-struct leaf (info) (make-inspector)) ; info = whatever symbol
(define a-tree (make-tree (make-tree (make-leaf 1) (make-leaf 2) 'a)
(make-tree (make-leaf 3) (make-leaf 4) 'b)
'c))
(display a-tree)
(newline)
> (define (parse s)
>
> (cond
> [(symbol? s) (make-leaf s)]
> [(symbol=? (first s) '+) (make-tree (parse (second s))
> (parse (third
> s))]
> )
> )
(define (parse s)
(cond
[(pair? s) (make-tree (parse (second s))
(parse (third s))
(first s))]
[else (make-leaf s)]))
(parse '(c (a 1 2) (b 3 4)))
> which just keeps building elements of a tree. Once parse() 'returns'
> how the heck do I traverse my returned tree!? Alright so I tried
> wrapping the call to parse() in a printTree() function:
>
> (printTree (parse s))
>
> yes it goes into the function passing the tree I suppose.
>
> (define (printTree tree)
>
> (cond
> [(tree? tree)
> (display "I thought I should recursively call print on the
> children here..\n")
> (printTree tree-l)
> (printTree tree-r)]
The function tree-l takes a tree as input and returns the field named l.
That is, to get the left tree use (tree-l tree). Printing the
left then becomes (printTree (tree-l tree)).
> [(leaf? tree)
> (display "I just want to print my tree...\n")
> )
> )
> but yeah #1 even though I know my app built one parent node with two
> leaves, the print statement only ever shows that I have one top tree
> node, no children leaves.
>
> Second - even if that were working, how could I print the value of the
> leave!? I tried:
>
> (display tree-info) ; when it's a leaf
>
> but I just get an error at runtime about it.
Show us exactly what you did. Display never gives runtime errors.
--
Jens Axel Søgaard
Planet Scheme - Scheme Blogs: <http://www.scheme.dk/planet/>
Everything Scheme: <http://www.scheme.dk/blog/>
|
|
|
|
|