# Ruby Quick Start (3) -- Module, Mix-in

《Ruby语言入门教程v1.0》笔记（3）

## 第六章 模块、命名空间、Mix-in

### 1. Ruby中的模块

puts Math.sqrt(2)    # 1.4142135623731
puts Math::PI    # 3.14159265358979

• 模块不可以有实例对象；
• 模块不可以有子类。

# Notes of Scala course on Coursera -- Week 3

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

## 1. Class Hierarchies

### 1.1 Abstract Classes

Consider the task of writing a class for sets of integers with the following operations.

abstract class IntSet {
def incl(x: Int): IntSet
def contains(x: Int): Boolean
}

IntSet is an abstract class.

Abstract classes can contain members which are missing an implementation (in our case, incl and contains ).

Consequently, no instances of an abstract class can be created with the operator new.

### 1.2 Class Extensions

Let’s consider implementing sets as binary trees.

There are two types of possible trees: a tree for the empty set, and a tree consisting of an integer and two sub-trees.

Here are their implementations:

class Empty extends IntSet {
def contains(x: Int): Boolean = false
def incl(x: Int): IntSet = new NonEmpty(x, new Empty, new Empty)
}

class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet {
def contains(x: Int): Boolean =
if (x < elem) left contains x
else if (x > elem) right contains x
else true
def incl(x: Int): IntSet =
if (x < elem) new NonEmpty(elem, left incl x, right)
else if (x > elem) new NonEmpty(elem, left, right incl x)
else this
}

Empty and NonEmpty both extend the class IntSet .

This implies that the types Empty and NonEmpty conform to the type IntSet

• an object of type Empty or NonEmpty can be used wherever an object of type IntSet is required.

# Ruby Quick Start (2) -- Objects in Ruby

《Ruby语言入门教程v1.0》笔记（2）

## 第四章 Ruby面向对象初探

### 1. 封装

class Person
def initialize(name, age = 18)
@name = name
@age = age
@motherland = "China"
end  # 初始化方法结束
def talk
puts "my name is " + @name + ", age is " + @age.to_s
if @motherland == "China"
puts "I am a Chinese."
else
puts "I am a foreigner."
end
end  # talk方法结束
attr_writer :motherland  # @motherland的setter
end

p1 = Person.new("Yilin", 23)
p1.talk

p2 = Person.new("Ben")
p2.motherland = "ABC"
p2.talk

initialize 是初始化方法，相当于构造函数。

# Ruby Quick Start (1) -- A Glimpse of Syntax

(1) 我为什么要学习Ruby？

(2) 为什么选择Ruby，而不是Python，PHP？

• 假如你想帮他尽快找个活儿， 赚到钱， 推荐 PHP
• 假如你想让他成为一个高效工程师， 推荐 Python
• 假如你想让他爱上他的工作， 推荐 Ruby

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

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

## 第二部分 编码习惯和规范篇

### 建议82：积极使用const为函数保驾护航

const的真正威力体现在几个方面：

1. 修饰函数形式的参数：const只能修饰输入参数，对于内置数据类型的输入参数，不要将“值传递”的方式改为“const 引用传递”；
2. 修饰函数返回值；
3. 修饰成员函数：用const修饰成员函数的目的是提高程序的健壮性。const成员函数不允许对数据成员进行任何修改。

1. const对象只能访问const成员函数，而非const对象可以访问任意的成员函数；
2. const对象的成员是不可修改的，然而const对象通过指针维护的对象却是可以修改的；
3. const成员函数不可以修改对象的数据，不管对象是否具有const性质。

### 建议85：了解指针参数传递内存中的玄机

// 指针型变量在函数体中需要改变的写法
void f(int *&x)    // 使用指针变量的引用
{
++x;
}

# 《编写高质量代码：改善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
(2 * 3.14159) * 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.