-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2023-10-11-exercises.rkt
52 lines (42 loc) · 1.05 KB
/
2023-10-11-exercises.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#lang racket
;; (1 2 ((2) 3) 4) => (1 2 3 4)
(define (list-flatten lst)
(cond [(null? lst) lst]
[(not (list? lst)) (list lst)]
[else (append (list-flatten (car lst)) (list-flatten (cdr lst)))]))
(list-flatten '(1 2 (2 (3)) (((4)))))
;; (multifun (f g) (x) ((+ x x x) (* x x)))
(define-syntax multifun
(syntax-rules ()
[(_ (f) (args ...) (body))
(define (f args ...) body)]
[(_ (f . fs) (args ...) (body . bodies))
(begin
(multifun (f) (args ...) (body))
(multifun fs (args ...) bodies))]
))
(multifun (f g h)
(x)
((+ x x x)
(* x x)
(- x x x)))
(f 2)
(g 2)
(h 2)
;; alternative solution
(define-syntax multifun2
(syntax-rules ()
[(_ (f) (args ...) (body))
(define (f args ...) body)]
[(_ (f fs ...) (args ...) (body bodies ...))
(begin
(multifun2 (f) (args ...) (body))
(multifun2 (fs ...) (args ...) (bodies ...)))]
))
(multifun2 (f1 g1 h1)
(x)
((+ x x x)
(* x x)
(- x x x)))
(f1 2)
(g1 2)