我正在寻找最酷的事情,你可以在几行简单的代码。我相信你可以用Haskell用15行写一个Mandelbrot集,但是很难理解。

我的目标是启发学生编程很酷。

我们知道编程很酷,因为你可以创造任何你想象到的东西——它是最终的创意出口。我想激励这些初学者,让他们尽可能多地克服早期学习的困难。

Now, my reasons are selfish. I'm teaching an Intro to Computing course to a group of 60 half-engineering, half business majors; all freshmen. They are the students who came from underprivileged High schools. From my past experience, the group is generally split as follows: a few rock-stars, some who try very hard and kind of get it, the few who try very hard and barely get it, and the few who don't care. I want to reach as many of these groups as effectively as I can. Here's an example of how I'd use a computer program to teach:

Here's an example of what I'm looking for: a 1-line VBS script to get your computer to talk to you: CreateObject("sapi.spvoice").Speak InputBox("Enter your text","Talk it") I could use this to demonstrate order of operations. I'd show the code, let them play with it, then explain that There's a lot going on in that line, but the computer can make sense of it, because it knows the rules. Then I'd show them something like this: 4(5*5) / 10 + 9(.25 + .75) And you can see that first I need to do is (5*5). Then I can multiply for 4. And now I've created the Object. Dividing by 10 is the same as calling Speak - I can't Speak before I have an object, and I can't divide before I have 100. Then on the other side I first create an InputBox with some instructions for how to display it. When I hit enter on the input box it evaluates or "returns" whatever I entered. (Hint: 'oooooo' makes a funny sound) So when I say Speak, the right side is what to Speak. And I get that from the InputBox. So when you do several things on a line, like: x = 14 + y; You need to be aware of the order of things. First we add 14 and y. Then we put the result (what it evaluates to, or returns) into x.

这就是我的目标,有一堆很酷的例子来演示,并在他们玩得开心的时候教他们。我在我室友身上尝试了这个例子,虽然我可能不会把它作为第一课,但她喜欢它,并学到了一些东西。

一些很酷的数学程序,可以做出漂亮的图形或容易理解的形状,这是很好的想法,我将研究这些。这里有一些复杂的actionscript示例,但这有点太高级了,我不能教flash。你还有什么其他的想法吗?


当前回答

所以有一天,我觉得我受够了。我要学钢琴。看到像埃尔顿·约翰这样精通键盘的人,我确信这就是我想做的。

事实上,学钢琴是非常令人失望的。即使在完成了八年级的钢琴课程后,我仍然没有印象到,我弹钢琴的心理形象与我最初享受这项活动的想象有多么不同。

然而,我最喜欢的是我仅有的三年级音乐理论基础。我学到了音乐的构造。我终于能够从表演书面音乐的世界中走出来,开始创作自己的音乐。随后,我可以开始玩我想玩的东西了。


不要试图让新程序员眼花缭乱,尤其是年轻程序员。“少于十行简单代码”的整个概念似乎引起了一种“给我展示一些聪明的东西”的情绪。

你可以向新程序员展示一些聪明的东西。然后你可以教这个程序员如何复制这种“性能”。但这并不是他们迷上编程的原因。教他们一些基础知识,让他们自己合成十行聪明的代码。

我会向一个新程序员展示以下Python代码:

input = open("input.txt", "r")
output = open("output.txt", "w")

for line in input:
    edited_line = line
    edited_line = edited_line.replace("EDTA", "ethylenediaminetetraacetic acid")
    edited_line = edited_line.replace("ATP", "adenosine triphosphate")
    output.write(edited_line)

我意识到我不需要将line赋值给edited_line。然而,这只是为了让事情更清楚,并表明我没有编辑原始文档。

在不到十行里,我已经详尽地描述了一份文件。当然,还要确保向新程序员显示所有可用的字符串方法。更重要的是,我展示了我可以做的三件基本有趣的事情:变量赋值、循环、文件IO和使用标准库。

我想您会同意这段代码并不令人眼花缭乱。事实上,这有点无聊。不,实际上,很无聊。但是将代码展示给一个新的程序员,看看这个程序员是否能够在一周内(如果不是当天的话)将脚本的每个部分重新用于更有趣的内容。当然,您可能不喜欢(可能使用这个脚本来创建一个简单的HTML解析器),但其他一切都需要时间和经验。

其他回答

我一直很喜欢河内塔。在计划

(define (hanoi x from to spare)
  (if (= x 1)
    (begin
      (display "move ")(display from)(display " to ")(display to)(display "\n"))
  (begin
    (hanoi (- x 1) from spare to)
    (hanoi 1 from to spare)
    (hanoi (- x 1) spare to from))))

示例输出

gosh> (hanoi 3 'start 'dest 'spare)
move start to dest
move start to spare
move dest to spare
move start to dest
move spare to start
move spare to dest
move start to dest
#<undef>

同样在Python中(尽管这不能像Scheme版本那样做1000张光盘)

def hanoi(x, source, dest, spare):
  if x == 1:
    print "%s to %s" % (source, dest)
  else:
    hanoi(x - 1, source, spare, dest)
    hanoi(1, source, dest, spare)
    hanoi(x - 1, spare, dest, source)

我从我的孩子那里得到了一个很好的反应,用一个快速的VB脚本来操作微软代理字符。对于那些不熟悉MS Agent的人来说,它是一系列可以通过COM接口操作的动画屏幕角色。您可以在Microsoft Agent下载页面下载代码和字符。

下面几句台词会让梅林角色出现在屏幕上,飞来飞去,敲屏幕引起你的注意,并打招呼。

agentName = "Merlin"
agentPath = "c:\windows\msagent\chars\" & agentName & ".acs"
Set agent = CreateObject("Agent.Control.2")

agent.Connected = TRUE
agent.Characters.Load agentName, agentPath
Set character = agent.Characters.Character(agentName)

character.Show

character.MoveTo 500, 400
character.Play "GetAttention"
character.Speak "Hello, how are you?"
Wscript.Sleep 15000
character.Stop
character.Play "Hide"

您还可以使用许多其他命令。查看http://www.microsoft.com/technet/scriptcenter/funzone/agent.mspx了解更多信息。

编辑2011-09-02 我最近发现Microsoft Agent并没有安装在Windows 7上。不过,这里提供单独的下载。我没有测试过这个,所以无法验证它是否运行。

如果你教的是工程师,这段Prolog可能会引起他们的注意:

d(x,x,1).
d(C,x,0):-number(C).
d(C*x,x,C):-number(C).
d(-U, X, -DU) :- d(U, X, DU).
d( U + V, x, RU + RV ):-d(U,x,RU), d(V,x,RV).
d( U - V, x, RU - RV ):-d(U,x,RU), d(V,x,RV).
d(U * V,x, U * DV + V * DU):- d(U,x,DU), d(V,x,DV).
d(U^N, x, N*U^(N-1)*DU) :- integer(N), d(U, x, DU).

只要写下规则,你就有了一个程序,可以用8行代码完成第一学期的所有微积分。

在您提供的SAPI示例的基础上,我使用它来自己大声读取文件(只需将文本文件拖放到它的图标上或从命令行运行它)

speakfile.vbs:

strFileName = Wscript.Arguments(0)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, 1)
strText = objFile.ReadAll
Set objVoice = CreateObject("SAPI.SpVoice")
objVoice.Speak strText

下面是一个用a - prolog语言编写的程序,它可以计算图的N种颜色(“c”表示颜色,“v”表示顶点,“e”表示边)。

c(1..n).                                           
1 {color(X,I) : c(I)} 1 :- v(X).             
:- color(X,I), color(Y,I), e(X,Y), c(I).

On a side note, the way I got my students excited last semester was to tell them a story. It went something along the lines of: "Picture a triangle. It's a purely mathematical object, no real triangles exists. We can reason about them, discover their properties, and then apply those properties towards real world solutions. An algorithm is also a purely mathematical object. Programming is a form of magic however. We can take a mathematical object, describe it a language, and lo and behold it can manipulate the physical world. Programming is a unique discipline that bridges these two worlds."