2007年4月1日 星期日

Lesson 0: Scheme is weird

Remember that. Scheme is weird. But sometimes there is a reason. Let's write a java program to get a number from the user, add 13 to it, and print it out. We'll pretend we already have a function called System.in.getNumber() to do the first step:

Double number = System.in.getNumber();
number += 13;
System.out.println number;


We tell the computer what to do in exactly the order it should be done, just the way we do things in real life, one step at a time. But there is sometimes a problem. When we are all the way down at the end, maybe the value of number has changed. Where did it come from? This is especially hard if you name variables things like i or bg_f. What does that mean? How do you know it hasn't changed somewhere else in the program?

More importantly, it's hard to see what the goal if this program is. It doesn't tell us what we really want to do until the very end.

In scheme, instead of using lots of ='s and variables, we do something different. We write programs with the name of a function first, and the arguments to the function inside it, like this: (+ 1 2), which is the same as Java's 1 + 2 or (or x y) which is the same as Java's x || y [Edit: fixed this, thanks taishen!]. Keeping in mind that the function name cames first, and the arguments inside it, let's see that program in Scheme:

(print (+ 13 (get-number)))

Notice that this program is inside-out. It says the end goal first: we want to print something. What do we want to print? 13 plus something, where something comes from the function get-number.

Make sense? Maybe not. For your first assignment, go read section 1.1.1 and 1.1.2 in the textbook. If you have questions, try using an online English-Chinese computer terms dictionary, or ask in the comments.

7 則留言:

Andrea.c 提到...

我沒辦法download耶...會顯示不出網頁...

financialiq 提到...

hi,Adam我也來了。
似乎很有趣
-Scott

digitdream 提到...
作者已經移除這則留言。
digitdream 提到...

倒數第三段的結尾這句
(or x y) which is the same as Java's x && y.

好像寫錯了,應該同義於Java中的x || y。

Adam Solove 提到...

哈哈對了!謝謝taishen。歡迎Scott.

瑞穗追夢者 提到...

我來報到一下^^
來了解Scheme
Luke

Jeff 提到...

我有興趣 :D