《编写高质量代码：改善C++程序的150个建议》读书笔记(1)

第一部分 语法篇

建议1：区分0的4种面孔

1. 整型0，32位（4个字节）；
2. 空指针NULL，指针与int类型所占空间是一样的，都是32位；
3. 字符串结束标志’\0’，8位，一个字节，与’0’有区别；
4. 逻辑FALSE/falseFALSE/TRUEint类型，而false/truebool类型。

建议5：不要忘记指针变量的初始化

1. 可以将其初始化为空指针0(NULL)；
2. 对于全局变量来说，在声明的同时，编译器会悄悄完成对变量的初始化。

建议6：明晰逗号分隔表达式的奇怪之处

1. 在使用逗号分隔表达式时，C++会确保每个表达式都被执行，而整个表达式的值则是最右边表达式的结果
2. 在C++中，逗号分隔表达式既可以用作左值，也可以用作右值

建议9：防止重复包含头文件

#ifndef _PROJECT_PATH_FILE_H
#define _PROJECT_PATH_FILE_H
// ...
#endif

Notes of Scala course on Coursera -- Week 2 (3)

Here are my notes for the week 2’s lecture of Functional Programming Principles in Scala on Coursera.

1. Functions and Data

1.1 Classes

In Scala, a class ia defined like this:

class Rational(x: Int, y: Int) {
def numer = x
def denom = y
}

This definition introduces two entities:

• A new type, named Rational .
• A constructor Rational to create elements of this type.

Notes of Scala course on Coursera -- Week 2 (2)

Here are my notes for the week 2’s lecture of Functional Programming Principles in Scala on Coursera.

4. Example: Finding Fixed Points

4.1 Finding a fixed point of a function

A number x is called a fixed point of a function f if

$f(x)=x$

For some functions f we can locate the fixed points by starting with an initial estimate and then by applying f in a repetitive way.

$x,f(x),f(f(x)),f(f(f(x))),...$

until the value does not vary anymore (or the change is sufficiently small).

Notes of Scala course on Coursera -- Week 2 (1)

Here are my notes for the week 2’s lecture of Functional Programming Principles in Scala on Coursera.

1. Recursion and Tail Recursion

1.1 Examples of Recursion

/* Euclid's Algorithm */
def gcd(a: Int, b: Int): Int =
if (b == 0) a else gcd(b, a % b)

gcd(14, 21) is evaluated as follows:

gcd(14, 21)
--> if (21 == 0) 14 else gcd(21, 14 % 21)
--> if (false) 14 else gcd(21, 14 % 21)
--> gcd(21, 14 % 21)
--> gcd(21, 14)
--> if (14 == 0) 21 else gcd(14, 21 % 14)
-->> gcd(14, 7)
-->> gcd(7, 0)
--> if (0 == 0) 7 else gcd(0, 7 % 0)
--> 7

Another example:

def factorial(n: Int): Int =
if (n == 0) 1 else n * factorial(n - 1)

factorial(4) is evaluated as follows:

factorial(4)
--> if (4 == 0) 1 else 4 * factorial(4 - 1)
-->> 4 * factorial(3)
-->> 4 * (3 * factorial(2))
-->> 4 * (3 * (2 * factorial(1)))
-->> 4 * (3 * (2 * (1 * factorial(0))))
-->> 4 * (3 * (2 * (1 * 1)))

1.2 Tail Recursion

Implementation Consideration:

If a function calls itself as its last action, the function’s stack frame can be reused. This is called tail recursion.

Tail recursive functions are iterative processes.

In general, if the last action of a function consists of calling a function (which may be the same), one stack frame would be sufficient for both functions. Such calls are called tail-calls.

Notes of Scala course on Coursera -- Week 1

Here are my notes for the week 1’s lecture of Functional Programming Principles in Scala on Coursera.

Evaluation of Programming

Every non-trivial programming language provides:

• primitive expressions representing the simplest elements
• ways to combine expressions
• ways to abstract expressions, which introduces a name for an expression by which it can be referred to

A non-primitive expression is evaluated as follows:

1. Take the leftmost operator
2. Evaluate its operands (left before right)
3. Apply the operator to the operands

A name is evaluated by replacing it with the right hand side of its definition.

Example:

(2 * pi) * radius
6.28318 * 10

Definition can have parameters:

def square(x: Double) = x * x

Parameter and Return Types:

def power(x: Double, y: Int): Double = ...

Evaluation of Function Applications

1. Evaluate all function arguments, from left to right.
2. Replace the function application by the function’s right-hand side, and, at the same time,
3. Replace the formal parameters of the function by the actual arguments.

Example:

sumOfSquares(3, 2+2)
sumOfSquares(3, 4)
square(3) + square(4)
3 * 3 + square(4)
9 + square(4)
9 + 4 * 4
9 + 16
25

The substitution model

The scheme of expression evaluation is called the substitution model.

The idea is that all evaluation dose is reduce an expression to a value.

It can be applied to all expressions, as long as they have no side effects.

The substitution model is formalized in the λ-calculus, which gives a
foundation for functional programming.

Start Learning Scala

---

Wikipedia上指出：

• imperative programming(命令式编程)
• functional programming(函数式编程)
• logic programming(逻辑式编程)
• object-oriented programming(面向对象编程)