forked from fanlei/the-little-phper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_3.php
92 lines (80 loc) · 2.2 KB
/
chapter_3.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php declare(strict_types=1);
//require 'chapter_1.php';
/* Chapter 3 */
function rember
($s, array $l) : array
{return
is_nulll($l) ? []
: (is_eq(car($l), $s) ? cdr($l)
: cons(car($l), rember($s, cdr($l))));
}
function firsts
(array $l) : array
{return
is_nulll($l) ? []
: cons(car(car($l)), firsts(cdr($l)));
}
function insert_right
($new_s, $old_s, array $l) : array
{return
is_nulll($l) ? []
: (is_eq(car($l), $old_s) ? cons($old_s, cons($new_s, cdr($l)))
: cons(car($l), insert_right($new_s, $old_s, cdr($l))));
}
function insert_left
($new_s, $old_s, array $l) : array
{return
is_nulll($l) ? []
: (is_eq(car($l), $old_s) ? cons($new_s, $l)
: cons(car($l), insert_left($new_s, $old_s, cdr($l))));
}
function subst
($new_s, $old_s, array $l) : array
{return
is_nulll($l) ? []
: (is_eq(car($l), $old_s) ? cons($new_s, cdr($l))
: cons(car($l), subst($new_s, $old_s, cdr($l))));
}
function subst2
($new_s, $old_s1, $old_s2, array $l) : array
{return
is_nulll($l) ? []
: (is_eq(car($l), $old_s1) || is_eq(car($l), $old_s2) ?
cons($new_s, cdr($l))
: cons(car($l), subst2($new_s, $old_s1, $old_s2, cdr($l))));
}
function multirember
($s, array $l) : array
{return
is_nulll($l) ? []
: (is_eq(car($l), $s) ? multirember($s, cdr($l))
: cons(car($l), multirember($s, cdr($l))));
}
function multiinsert_right
($new_s, $old_s, array $l) : array
{return
is_nulll($l) ? []
: (is_eq(car($l), $old_s) ?
cons($old_s,
cons($new_s,
multiinsert_right($new_s, $old_s, cdr($l))))
: cons(car($l), multiinsert_right($new_s, $old_s, cdr($l))));
}
function multiinsert_left
($new_s, $old_s, array $l) : array
{return
is_nulll($l) ? []
: (is_eq(car($l), $old_s) ?
cons($new_s,
cons($old_s,
multiinsert_left($new_s, $old_s, cdr($l))))
: cons(car($l), multiinsert_left($new_s, $old_s, cdr($l))));
}
function multisubst
($new_s, $old_s, array $l) : array
{return
is_nulll($l) ? []
: (is_eq(car($l), $old_s) ?
cons($new_s, multisubst($new_s, $old_s, cdr($l)))
: cons(car($l), multisubst($new_s, $old_s, cdr($l))));
}