damn tweaks6
[lambda.git] / damn3.rkt
1 #lang racket
2 (require racket/control)
3
4 (define damn0 (lambda ()
5                                 'id))
6
7 (define damn1 (lambda ()
8                                 (cons '("side effect" bad)
9                                           'id)))
10
11 (define damn2 (lambda () (shift k
12                                                                 (cons '("side effect" bad) 
13                                                                           (list (k 'id))))))
14
15 (define damn3 (lambda () (shift k
16                                                                 (list (k 'id)
17                                                                           '("side effect" bad)))))
18
19
20 ; Now if we use damn0, our compositional semantics will work OK but
21 ; we don't yet have any expressive contribution:
22
23 (list "main content" 'i (list 'like (list 'the (damn0) 'boy)))
24 ; '("main content" i (like (the id boy)))
25
26
27 ; If we use damn1, we've added in the expressive side-effect:
28
29 (list "main content" 'i (list 'like (list 'the (damn1) 'boy)))
30 ; '("main content" i (like (the (("side effect" bad) . id) boy)))
31
32 ; However, the context (list 'the ... 'boy) is now being asked to operate
33 ; on an element (("side effect" bad) . id), and it may complain it doesn't
34 ; know what that is. It knows how to use 'id to get (list 'the 'id 'boy),
35 ; and how to use 'bad to get (list 'the 'bad 'boy), but we're supposed to
36 ; have something different here.
37
38 ; To get what we want we need to use (delimited) continuations:
39 (reset (list "main content" 'i (list 'like (list 'the (damn2) 'boy))))
40 ; '(("side effect" bad) ("main content" i (like (the id boy))))
41
42 ; or to get the side effect at the end:
43
44 (reset (list "main content" 'i (list 'like (list 'the (damn3) 'boy))))
45 ; '(("main content" i (like (the id boy))) ("side effect" bad))
46
47 ; If you're working in the interactive interpreter, the outermost "reset" here
48 ; is already in its default position, so it doesn't need to be explicitly
49 ; specified:
50
51 (list "main content" 'i (list 'like (list 'the (damn2) 'boy)))
52 ; '(("side effect" bad) ("main content" i (like (the id boy))))
53
54 ; However, if you're executing this as a file, you would need to include explicit resets.
55
56
57
58 ; Instead of using reset/shift you could use an element like "print" in
59 ; building the side-effect, as we did in class. Here you wouldn't require an
60 ; explicit continuation, but as Chris said, that's because "print" already
61 ; represents an implicit continuation.
62
63 (define damn4 (lambda () (begin (print "bad") 'id)))
64 (list "main content" 'i (list 'like (list 'the (damn4) 'boy)))
65 ; "bad"'("main content" i (like (the id boy)))
66 ;
67