# 我要学的最后一门编程语言：LISP (Clojure)

• 当你想开办一个公司时，先从顾问做起。(看，他已经知道我入魔了。)从你的客户那里学习业务和开发，获得报酬的同时，利用他们的专家和资金，最终将其全部转化到你的产品里。
• 学习LISP。

“编程语言不仅仅是一种技术，它更是一种思维习惯。” – http://www.paulgraham.com/avg.html

“50多年前，著名的语言学家Roman Jakobson用一句简单的名言指明了语言之间最关键的不同之处：‘语言之间的不同本质在于它们能够传达的信息，而不是它们不能传达的信息。’这句名言告诉了我们解放我们母语潜能的关键所在：不同的语言用不同的方式影响我们的大脑思维，这并不是因为这种语言可以让我们这样思考问题，而是它在强迫你这样思考。”

“当你的语言日常的强迫你去使用某些类型的信息进行说明，这会迫使你去留意那些使用其它种语言的人平时不会注意的某些细节和体验。因为这种说法的习惯是从小养成的，这种习惯已经成为大脑思维的习惯，超越了语言本身，直接影响了你的言行，感知，联想，感觉，记忆，以及世界观。”

# Yield in Ruby

Yield 是 Ruby 里面一个很独特的关键字，这篇文章可以帮助你很好的理解她。

## TUESDAY, JUNE 5, 2007

### Ruby Yield

Yield is one of the most powerful concepts implemented in the Ruby programming language. Yield lets you branch from within a method and execute some external code, then return to the original code in the first method.

What's so special about yield in Ruby? You could get the same branch and return flow by just executing a method call in java, or a function call in C.

Heck, COBOL's PERFORM verb implements branch and return behavior. So what's up with yield?

Well, the behavior of what you branch to is set in stone with all those other techniques, but is undetermined until coded in RubyYield is kind of like a place holder that says "I'm going to branch off and do something here, but I don't know what I'm going to do yet".

Maybe some examples will help make it more clear. Here is a Ruby method with some yields coded in it, then the code to invoke the method and the block of code to invoke when the yield is encountered:

def block1
puts "Start of block1..."
yield
yield
puts "End of block1."
end

block1 {puts "yielding..."}

Running this code would produce the following output:

Start of block1...
yielding...
yielding...
End of block1.

So block1 executes it's first line and prints "Start of block1...", then it executes the yields which branch to the block of code outside block1 but associated with it at run time, then comes back and prints "End of block1."

OK that's, fine but isn't that functionally the same as the following code:

def block2
puts "Start of block2..."
put_it("in put_it called method...")
put_it("still in put_it called method...")
puts "End of block2."
end

def put_it(text)
puts text
end

block2

Running this second set of code produces the following:

Start of block2...
in put_it called method...
still in put_it called method...
End of block2.

For the most part, these are pretty much the same. However without having to redefine any methods I could immediately execute the block1 method with a different block associated with it. The yield would branch and execute this newly associated logic:

block1 {require 'time'
puts Time.now.httpdate}

This produces the output:

Start of block1...
Tue, 05 Jun 2007 19:20:35 GMT
Tue, 05 Jun 2007 19:20:35 GMT
End of block1.

The hard coded function call in block2 forever ties the branching logic to the put_it function. To get different behavior you would need to either change the function call in block2 to call a different function, or change the behavior of the function put_it. This second way is brittle because it could break code elsewhere that calls put_it. The first is cumbersome and possibly brittle.

Ruby's yield is a wonderful construct.

What about when you don't want to branch, but want the other functionality in the block1 code to be executed? Can you just run the code without the associated block?

Let's see what happens. Running block1 without an attached block for the yield to associate to:

block1

This produces the result:

Start of block1...
LocalJumpError: no block given
from :3:in 'block1'
from :7
from :0

We get a 'no block given' error at line 3 in method 'block1'. Line 3 is our first yield in the block1 method.

What to do?

We can wrap the yield in an if statement and check for the presence of a block using the block_given? conditional. It returns true if a block was given with the method call, or false if not.

For this example I'll use a slightly more idiomatic way of checking for the presence of a block in ruby:

def block3
puts "Start of block3..."
yield if block_given?
puts "End of block3."
end

now if we run the code with a block:

block3 { puts "yielding" }

or without:

block3

It works both times producing:

Start of block3...
yielding...
End of block3.

and

Start of block3...
End of block3.

This will make your methods less brittle and more agile.

Ruby's yield is worth getting to know. You can learn more here and here. Many of Ruby's built in methods contain yields that allow you to associate blocks with them producing added functionality at run time.

# Qt Creator 2.1.0

Qt Creator 真是越用越好用了。

# Qt编程中的一个注意事项

Qt GUI编程里，默认根据ui文件生成的窗口部件的构造函数里，大体是这样的：

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}

ui对应的就是主窗口容器（个人理解），如果在这个容器之中还有其他元素的话，比如说 tableView、editBox，如果也在构造函数里对它们进行初始化，就应该注意到，初始化的操作代码应该需要写到 ui->setupUi(this) 这一句之后，个人理解是，在这句之前窗口对象上的部件还没有成型，因此不能对它们进行相应操作。给个样例代码：

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this); // ui初始化

model = new QSqlTableModel(this);
model->setTable("student");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();

ui->tableView->setModel(model); // 这一句是对ui上部件tableView的操作
}