source: liacs/cvp/scheme/exercise1a.scm@ 354

Last change on this file since 354 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: 1.2 KB
Line 
1#!/usr/bin/env guile -s
2!#
3
4
5; * Rick van der Zwet
6; * 0433373
7; * Scheme programming assigment 1a
8; * Licence: BSD
9; * $Id: exercise1a.scm 374 2007-12-10 20:18:45Z rick $
10
11; replace find and replace
12(define sub_atom
13 (lambda (atom sub)
14 ;write down replacement else procede to next of the list
15 (cond
16 ((null? sub) atom)
17 ((eq? (caar sub) atom) (cadar sub))
18 (else (sub_atom atom (cdr sub)))
19 )
20 )
21)
22
23(define substitute
24 (lambda (lijst sub)
25 ;if element a return sub if found else element using adding pair (a
26 ; a) to replace sub
27 (if (list? lijst)
28 ;if list and both head and tail recurse both
29 ;else recurse single
30 (if (null? (cdr lijst))
31 (list (substitute (car lijst) sub))
32 (cons (substitute (car lijst) sub)
33 (substitute (cdr lijst) sub))
34 )
35 ; try to do the replacement
36 (sub_atom lijst sub)
37 )
38 )
39)
40
41
42
43
44(display
45 (equal?
46 (substitute
47 '((a (b c) d) a (f b))
48 '((a z) (b y))
49 )
50 '((z (y c) d) z (f y))
51 )
52)
Note: See TracBrowser for help on using the repository browser.