Live Note

Remain optimistic

公式: $$P{Q}R\ ({P}Q{R})$$

P 和 R 都是一阶公式, 如果前提条件 P 在执行 Q 前成立, 则执行后得到满足条件 R 的状态
部分正确性断言: 如果 P 在 Q 执行前为真, 那么, 如果 Q 的执行终止,则终止在使 R 为真的某个状态
终止性断言:如果 P 在 Q 执行前为真, 那么 Q 将终止在使 R 为真的某个状态
赋值公理: $$\vdash P_0 {x:=f} P$$

推理规则的表示

$$\frac{premise -> f_0, f_1, …, f_n}{conclusion -> f_0}$$

推理规则

  • Rules of Consequence:
    $$\frac{P{Q}R,\ R\rightarrow S}{P{Q}S}\ \ \ \ \ \frac{P{Q}R,\ S\rightarrow P}{S{Q}R}$$
  • Rule of Composition:
    $$\frac{P{Q_1}R_1,\ R_1{Q_2}R}{P{Q_1, Q_2}R}$$
  • Rules of Iteration:
    $$\frac{P\ &\ B{S}P}{P\ {while\ B\ do\ S}\ \neg B \ &\ P}$$

等式公理

  • 代换: $[N/x]M$ 表示表示 M 中的自由变元 x 用 N 代换的结果, N 中的自由变元代换后不能成为约束变元
  • 约束变元改名: $\lambda x:\sigma .M = \lambda y:\sigma.[y/x]M$ 例如:$\lambda x:\sigma.x + y = \lambda z:\sigma .z+y$
  • 等价公理: 计算函数实际就是在函数中使用实在变元替换形式变元, $(\lambda x:\sigma.M)N = [N/x]M$
  • 同余性规则: 相等的函数作用于相等的变元产生相等的结果, $\frac{M_1=M_2,\ N_1=N_2}{M_1N_1=M_2N_2}$

状态模式

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
let ResultState = (() => {
let States = {
state0: function () {
console.log("state0")
},
state1: function () {
console.log("state1")
},
state2: function () {
console.log("state2")
},
state3: function () {
console.log("state3")
},
}

function show(result) {
States["state" + result] && States["state" + result]()
}

return {
show,
}
})()

// for test
ResultState.show(3)

策略模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let Price = (() => {
let obj = {
return30: (price) => +price + parseInt(price / 100) * 30,
return50: (price) => +price + parseInt(price / 100) * 50,
percent90: (price) => (price * 100 * 90) / 10000,
percent80: (price) => (price * 100 * 80) / 10000,
}
return (algorithm, price) => {
return obj[algorithm] && obj[algorithm](price)
}
})()

// for test
let price = Price("return50", 321.13)
console.log(price)

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
class Node {
constructor(data = -1, prev = null, next = null) {
this.data = data
this.prev = prev // 向前指针
this.next = next // 向后指针
}
}

class LinkList {
constructor() {
this.head = this.tail = null // 首、尾指针
this.size = 0 // 元素个数
}

addHead(elem) {
let elemNode = new Node(elem)
if (this.size == 0) {
this.head = this.tail = elemNode
this.size++
} else {
this.head.prev = elemNode
elemNode.next = this.head
this.head = elemNode
this.size++
}
return true
}

deleteHead() {
if (this.size != 0) {
let elem = this.head.data
this.head.prev = null
this.head = this.head.next
this.size--
return elem
} else {
console.log("list is empty")
return
}
}

addTail(elem) {
let elemNode = new Node(elem)
if (this.size == 0) {
this.head = this.tail = elemNode
this.size++
} else {
elemNode.prev = this.tail
this.tail.next = elemNode
this.tail = elemNode
this.size++
}
return true
}

deleteTail() {
if (this.size != 0) {
let elem = this.tail.data
this.tail.next = null
this.tail = this.tail.prev
this.size--
return elem
} else {
console.log("list is empty")
return
}
}

display() {
let current = this.head,
count = this.size,
str = ""
while (count > 0) {
str += current.data + " "
current = current.next
count--
}
console.log(str)
}
}
let linklist = new LinkList()
linklist.addHead(1)
linklist.addHead(2)
linklist.addHead(3)
linklist.deleteHead()
linklist.addTail(4)
linklist.display()

结果

2 1 4

简单的 Array 实现

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
class Array {
constructor(size = 10) {
this.arr = [] // 数组
this.size = size // 最大长度
this.elems = 0 // 成员数量
}

add(elem) {
if (this.arr.length == this.size) {
console.log("Array is full")
return
}
this.arr[this.elems++] = elem
return true
}

find(elem) {
for (let i = 0, len = this.elems; i < len; i++) {
if (this.arr[i] == elem) return i
}
return -1
}

delete(elem) {
let index = this.find(elem)
if (index == -1) {
console.log("Element not found")
return
}
for (let i = index, len = this.elems - 1; i < len; i++)
this.arr[i] = this.arr[i + 1]
this.elems--
return true
}

update(oldVal, newVal) {
// only use for unique element
let index = this.find(oldVal)
if (index == -1) {
console.log("Element not found")
return
}
this.arr[index] = newVal
return true
}

display() {
let srt = ""
for (let i = 0, len = this.elems; i < len; i++) {
srt += "" + this.arr[i] + " "
}
console.log(srt)
}

length() {
return this.elems
}
}
var arr = new Array()
arr.add(1)
arr.add(2)
arr.add(3)
arr.add(4)
arr.display()
console.log(arr.length())

结果

1
2
1 2 3 4
4

简单的 Queue 实现

数组实现

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
class Queue {
constructor(size = 10) {
this.size = size // 队列最大长度
this.top = 0 // 首位置
this.bottom = -1 // 尾位置
this.elems = 0 // 成员个数
this.arr = [] // 队列
}

add(elem) {
if (this.elems == this.size) {
console.log("Queue is full")
return
}
if (this.bottom == this.size - 1) {
// 循环队列
this.bottom = -1
}
this.arr[++this.bottom] = elem
this.elems++
return true
}

out() {
if (this.elems == 0) {
console.log("Queue is empty")
return
}
let elem = this.arr[this.top]
this.arr[this.top] = null
this.top++
if (this.top == this.size) {
this.top = 0
}
this.elems--
return elem
}
}
var queue = new Queue()
queue.add(3)
queue.add(2)
console.log(queue.out())
console.log(queue.out())
console.log(queue.out())
Read more »