source: liacs/cvp/scheme/exercise2.scm@ 288

Last change on this file since 288 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

  • Property svn:executable set to *
File size: 2.2 KB
RevLine 
[2]1#!/usr/bin/env guile --debug -ds
2!#
3
4; * Rick van der Zwet
5; * 0433373
6; * Scheme programming assigment 1a
7; * Licence: BSD
8; * $Id: exercise2.scm 378 2007-12-11 16:14:47Z rick $
9
10;infix order (<left-tree> <node> <right-tree>), in breadth first order
11;just seems to see the fact we are 'pushing' the node and then
12;traversing the tree
13
14(define treewalking
15 (lambda (tree)
16; (display "Debug : ")
17; (display tree)
18; (newline)
19; (display "Length : ")
20; (display (length tree))
21; (newline)
22 ;when nothing left on the stack bail out
23 (if (null? tree) tree
24 (cond
25 ;emtry branch, continue
26 ((null? (car tree))
27 (treewalking (cdr tree))
28 )
29 ;leaf, add and continue
30 ((symbol? (caar tree))
31 (cons
32 (caar tree)
33 (treewalking (cdr tree))
34 )
35 )
36 (else
37 ; fetch the node
38 ; check wether we still have an tail
39 (if (null? (cdr tree))
40 (cons
41 (cadar tree)
42 ; recurse with left, right
43 (treewalking (cons (caar tree)
44 (cddar tree)
45 )
46 )
47 )
48 (cons
49 (cadar tree)
50 ; recurse with tail, left, right
51 (treewalking (append (cdr tree)
52 (cons (caar tree)
53 (cddar tree)
54 )
55 )
56 )
57 )
58 )
59 )
60 )
61 )
62 )
63)
64
65(define treewalk
66 (lambda (tree)
67 (treewalking (list tree))
68 )
69)
70
71(display
72 (equal?
73 (treewalk
74 '( (b) a ( ((f) d (g)) c (() e (h)) ) )
75 )
76 '(a b c d e f g h)
77 )
78)
Note: See TracBrowser for help on using the repository browser.