1: 注意住房位置,多带锁, 小心本人外出时别人入室抢劫。
2:可以组织同学开设电脑上门维修, 赚得外快
3:运气之上,特别是application form。
4:获取相关证明,文具厂商和外贸公司的相关授权
Friday, 6 July 2007
Sunday, 1 July 2007
别人的英语学习新得
(一)前言
自从上英语论坛以来,我交到了许多学习英语的朋友.但据我观察,英语狂热者并不是很多.当然其中也有许多和我一样热爱英语的同仁!可是我发现一个很严重的问题和恶性循环就是英语差的越来越差,英语好的越来越好,就像贫富差距剧增一样!而其中的原因就是,英语差的越来越讨厌英语,在讨厌的条件下对英语的兴趣更加是每况愈下!许多过了英语6级的朋友水平也不敢恭维。因此我写下下面的话以供大家参考。
学英语就像政治一样首先要制度一下总纲和基本纲领。然后分步制度和确立路线方针政策。总纲和基本纲领就是学英语的动力和目的!路线方针就是学习英语的方法,包括词汇,语法,听力,口语,阅读,写作,翻译的方法。
在我们学习一样东西,首先我们要扪心自问“我们为什么要学习它,为什么要学习英语”如果在你心中有那么一个声音“我是为了考试,我是逼迫的”那么好,下面的你都不用看了!因为下面的内容不仅仅是为了考试的。本人个人认为考试确实测试水平的手段。但我们应该是先为了学习而考试,而不是为了考试而学习。如果答案是肯定的“我喜欢英语”那么我相信我所说的话一定会有所帮助的!
在过去实践学习一年中,我常常和一些英语学习者交流。他们有时候会问我“我英语不好,糟透了”。那我首先会问“你想学好英语吗?那好,我问你,你能背下新概念4册吗?”很遗憾的是,我得到的答案常常是“啊。。。”“我不行”“那太难了”。如果正在看此文的你,心中的答案也是否定的,那么英语你就不用学了。我问这个问题。并不是说每个人都要背新概念。而是从这可以看出一个人学习的毅力和决心。当别人总是问我“你能。。。。样吗?”的时候,我会毫不犹豫地说“我行我一定能做到,等着看我的结果吧”因此我常常能够更快的找到别人更有效的学习方法。因为我在心中肯定自己以后,马上就开始动手探索行动。
我常常对我父母说“我是风雨无阻,无坚不摧的,我要考哈佛大学的博士”。其实我知道没有一个人能理解我的行为了。我的父母更不会理解我的话!他们总是认为我太狂妄自大。老是说“你那是海市蜃楼”。诸如此类的话,我已经听别人说过无数遍了。他们老是说,还是脚踏实地一些吧!虽然我不是幼稚无知到反击。但是我也在心中想“我的天啊,这人这么这样啊”。我一直认为,我理想是伟大的,我一定能够学成英语,我一定要不断完善自我,其实我并不是没有考虑到现实,而是想,不管我最后有没有达到目的地,但是当某个时候我回首往事的时候,我可能会发现我已经迈上许多层台阶,如果当初没有这个理想,我可能还在最底下。说不定我哪天就真的能够达到我的目标呢!我也并不是说一定要考哈佛,而是作为一种鞭策自己的动力,给自己信心给自己鼓励。而且我发现那些不相信奇迹,相信“一切都是命中注定”的人都是那些失意者。其实他们根本没有从自身原因考虑过,他们为什么会失败?他们所谓的失败正是因为他们是信奉“命中注定”者,所以不去努力了,放弃了。而真是他们这种妥协几注定他们一辈子必将失败,这就是一个恶性循环了,他们所谓的“命中注定”的这个命恰恰是他们自己给自己的,而他们自己还茫然不知,混混沌沌。
学英语也是一样的道理,所以说错过了就不要再错下去了,而不错的要让自己更加完善。学习贵在兴趣,首先一个人要清楚自己为什么学英语,我发现那些为了工作考试或者想从英语方面得到什么实际利益的人,一般很难坚持到最后都放弃,比方我之所以能坚持,是英语我本身学英语就觉得快乐,因为我从小就对远古对世界各民俗风物历史等等有着浓厚的兴趣,所以英语正好就成为了一种桥梁,一把打开这扇大门的钥匙。
我为了学习英语身体受到了很大的摧残,我的听力下降很快,以前是用耳机伤害了听力,听课都困难了,后来恢复了一点点没,但是现在听力分贝仍然丢失很严重的,所以只得改用外放的。我和别人说话都必须使用助听器,但是我不后悔,我虽然有时候很伤心啊,因为世态炎凉,人心不古,落井下石的人真的是太多了。但是我从来没有后悔过学习英语,我喜欢有什么错,我还是会坚持下去。
所以说学英语首先必须有勇气,二是有毅力。然后就是英语环境,天分等等。其实说实话,我不是个说空话的人,经过我观察,我发现语言天分好记忆力好和善于创造发掘的人如果做出努力者同样的努力其效果远远要高于后者,这是没有办法改变的事实。
还有一个就是怎么样让自己常常雄心勃勃,我觉得近朱者赤,近墨者黑,要常常与英语学习者结伴一起学习一起促进,这样你就会觉得,恩,我学英语是很有用的。
如果常常帮助别人,比如说,再向别人解说英语的时候,就会觉得,我真的很有用,我真的很厉害哦,原来学英语是可以帮助这么多人,我是挺了不起的人的,这样我就会觉得信心满满,一直帮助别人就会一直有信心而且如果在帮助别人的同时发现自己的不足,就要翻找资料以查缺补漏。使自己感觉到自己的不足就为了不耽误别人也不耽误自己而更加努力。
(二)听力口语
现在我来谈一谈听力,作为一名英语专业的学生,听力一度是我最头疼的问题。我也曾经用过一些不错的方法,结果最后都是不了了之。我曾经用得最多的方法是《逆向法》和《千万别学英语》。不过似乎《逆向法》对我来说收效并不是那么明显。显然比较花费经历和时间。我感觉我比较适合《千万别学英语》的方法。
但是,在学习的过程中,我发现这两种方法本身就是有其缺陷的。我个人认为这两种方法结合起来再根据每个学英语的朋友的本身情况的不同,因材施教,才能在最短的时间里取得最大的成果。我已经结合这两种方法,定制一套适合我自己的学习方法,已经用了两个月,显然效果要比纯用这其中的一种要好得多。我已经能够很自然的听标准英语了。错误有时会有,但并不是很多。
现在我来结合这两种方法说一说我在此方面取得的成果。
我一开始选用的材料就是VOA标准英语的资料,我曾经听别人说,不经过学习慢速英语就想在标准英语上取得效果是不可能的。可是我并没有采纳那位朋友的意见。因为我基础并不差,只是听力没有达到我自己希望的水平罢了。我一开始就做了长期抗战的思想准备。可是事实其实没有我们想象中那么难。不过我想这是因为我没有用纯《逆向法》的缘故。我是先用了《千万别学英语》打通耳朵,然后再听写,我在听写的过程中是一边写一边跟读模仿的。不过我想并不是每个朋友都能像我这样的。如果基础比较差的朋友建议使用比较通俗一点的材料。像是比慢速英语速度快一些但又比标准英语慢一些的资料。像我以前基础不好的时候就用过语速大约为120/1的短篇故事集(外声朗读)。1个月就取得了不小的成果。最好不要用慢速英语,在我看来那有点浪费时间。因为人本来是可以接受正常语速的。是可以适应的。如果用语速过慢的资料,我想以后再去接受正常语速的资料会很大程度的困难,那又何不让自己更快的达到目标呢!有的去外国定居或是留学的朋友,也不能要求他们周围的环境去适应他们,他们只能自己去适应他们周围的英语环境。不一样都很好的度过了吗?在我看来像《逆向法》那样一个个去“抠”的话真的是一种不小的痛苦。如果能先照〈千万别学英语〉介绍的方法那样,先听熟一盒磁带,先不要想他的意思。当你能很自然的听下去,而且觉得可以适应语速,能听清楚95%的词的发音的时候,再开始听写。那样真的要轻松得多了。至于〈千万别学英语〉是否真的需要经过5个阶段,那就看个人志愿和自身情况了。不过〈千万别学英语〉的材料毕竟是适合有基础的朋友。基础比较差的朋友在选材料上还是要比较慎重一些。因为毕竟时间最好不要浪费在没有效果没有意义的事情上。
还有就是练听力的时间也很重要,对效果有很大的影响。如果泛听的话最好是选择在有一点点睡意的时候听是最好不过的了。因为那样的话,那种英语意识就完全输入到你的大脑潜意识里面去了。当你瞌睡的时候要控制头脑不要想睡觉的事情最好是注意耳边的语音。所以说睡前泛听是效果最高的。或者是早晨刚起来的时候听也不错。因为睡意未退嘛~!
本篇写于去年,当时由于时间紧急并没有经过修改所以存在一些缺陷,《千万法》的缺陷并没有很具体的写进去,所以今天特地整理一下:
万法的缺陷就在于,前两个步骤作用之大相当惊人,而后面的效果就递减,而且要花费相当长的时间,千万法里面推荐的增加词汇量和提高阅读能力已经口语的方法,不怎么好。
原因有几点:
1.用英英词典增加词汇量,没有8000词的基础,不要做这种无谓的尝试,因为比如有些专业名词,并没有具体解释,只是用简单的最基础的3000----3500词做解释,比如某种花或者树木草等,英语词典里面只解释其性质,比如什么形状生长在哪,什么颜色,这样不熟悉的我们就会被搞得云里雾里不但会不能增加词汇量还会误解。另外解释一下,因为很多著名的英英词典比如朗文,牛津的都将解释单词的基础词汇压缩在3500以下,所以我这里才说有些词汇解释不能清楚明了。
2.还有就是阅读,千万法作者推荐看报纸,但是我发现报纸的专业词汇量超大,而却语法相当难和复杂,没有经过长期的训练和很好的基础一张大报很难读完,因为科技文商业文是比较难读的,而我们多半在学英语的时候都是接触的文学类性质的文章,所以建议训练阅读科技文在阅读项目的最后一个项目。
3.作者说应该按照像播报员一样讲报纸内容自我演讲出来,这样是极其错误的,因为我们还在学习英语阶段,很多朋友都没有真正受过觉得对纯正的语音训练,如果报纸内容没有语音资料,很容易误入歧途,结果最后变成不伦不类的英语。
口语部分:
我本来口语是很差的,不过经过自我苦练正在不断提升中。我个人比较喜欢美语。因为美语比英语口型起伏大得多,比较形象化,感觉比较轻松有趣!不如如果你想学习英英最好从美到英音的转变。大家都知道学口语复读机绝对是必不可少的。我的方法也和普通方法是一样的。就是疯狂模仿背咏,然后实践.我们这里没有什么英语角,很少。我能做的就是,用复读机录下自己的声音。一般内容都是2个人对话,一个A一个B。先把A说的录下来。然后复读机问。我答,做完几遍后在换过来.我问复读机答.还有一种就是背时文和演讲稿,背时文的时候把自己当成新闻主播在镜子面前不断重复演练。背演讲稿的时候就真的融自己的感情,把自己当成真的在演讲!不要太注重你所说的内容,而是要想你现在要某某人或某些人表达什么!过一段时间后你会发觉你的英语越来越流利.也许这听起来有些可笑。这是在没有语言环境下不得已的方法。不过真的有很神奇的效果!当然还有就是可以上网上外语角!当你的英语打得比中文快。那么可以说你的英语口语水平已经有一定的程度了!如果有英语角和英语环境当然是更好!网上练口语也是不错的选择。但是缺乏逼真性。
跟大家开个玩笑涂唇膏润润唇可以使我们说英语的时候不结巴,一溜烟的就说过了,因为涂纯油很滑,这个绝对有效,电视里的播报员不是不怎么打结巴的?呵呵
(四)词汇阅读部分
我们都知道词汇是基础。没有词汇量什么都免谈。一年我也曾经拿着单词书死背。背了两遍结果最后什么都忘了。后来我在不断摸索中发现。背单词不如背句子背句子不如背文章。背文章才是一举三得。一是增加词汇量,并能灵活运用;二是使语法更容易学;三是提高写作能力。再加上阅读双管齐下,那效果是神奇的。建议开始进行阅读训练时不要用难度太高词汇量太大的科技经济等类文章。先从外国趣文民俗文化等书看起。再看小说。再看专业性文章。最后看文学类作品(如散文议论文等)和英文原版小说。我开始进行阅读的时候是用的一套“DO YOU KNOW”系列的书。一套5本。每本都介绍了世界各地地理,历史,旅游,政治,礼仪,文化,民俗等等内容。可读性很强。并且每篇后面都附有重点词汇和不易理解的专业名词和词组习语。然后就是附有阅读理解题。非常好的一套书!建议初学者和要过4.6级的朋友看看!词汇量达到一定程度后选一种好的英语杂志是必要的。我首推〈英语沙龙>和〈疯狂英语阅读版〉。因为上面的生词表不但表面了4.6级必背词汇词组。而且还说明了区别用法!内容也蛮丰富的!排版更好!而〈疯狂英语〉的知识面广含盖量大,对西方文化都做精辟的解说,最重要的是彩页,彩页能够加深的大脑的印象和记忆力刺激视觉。
(五)语法写作
语法一直是我的老大难题。看着那么一本字典一样厚的语法,只想流泪!但是泪也只能往肚里面吞.所以我建议各位先背文章不是没有原因的.如果不做翻译工作者,只要有非常强的语感,了解基本语法结构就够了。因为就算你不知道一些语法结构。但是语感强的没有这方面的知识一样能分析出来,并且写得一手好文章.当然英语专业又另当别论了!
(六)材料的选择推荐
选材料对自信的建立和对英语兴趣的提升也是非常重要的,像我从来不选择自己不喜欢的材料去作为阅读资料和听力资料。
我不喜欢政治,但是偏偏外国人一天到完谈政治VOA BBC新闻听力听力里面全是政治。所以在开始听写的时候我就选择慢速英语的feature听写了。也买了听力文摘,听写我自己喜欢的天文,人文,奇闻逸事等听写。像阅读就从念浅显的一些外国文化介绍等开始。后来再开始念散文,随笔等,也是我喜欢的,小说我觉得太长了。就从简单的全英文斑小说开始,都是一些童话啊比如〈绿野仙踪〉〈海底20000里〉等开始。是外国中学生念的改编过了的,压缩了原文,变得简单了,但是原味没有变,都是非常具有英语思维的生词也不是很多,但是非常有趣,都是外国出版公司请全世界各地有名的学者教师改编的。我读了5本以后发觉自己叙述文章,写记事文章刷刷的一下就写出来了,但是当然还没达到下笔如有神的境界,我听一位当翻译的朋友说他一个学姐现在在国外,以前学英语的时候,念了500本这样的书,写起文章来,那就不用说了,就像马良握着一种神笔一样。有了语感和一定基础后我就开始一边念疯狂英语阅读版杂志,一边看小说背美文等。
我从来不念科技文章,为什么呢,因为我本身不喜欢再者VOA英语里面已经有了,我看还是算了吧。
有一点很重要的就是,除了杂志以外我从来不念英汉双译的。我的书全是英文的,因为我从来就觉得中文就是中文,英文就是英文,大家都不仅仅是几个单词这样简单,都有很深的文化底蕴和内涵,原作者要表达的经过这个翻译那个翻译就不成样子了。虽然说翻译都必须忠实于原文,但是比较中国人深受中国文字中华文化影响,读翻译过来的作品等于在读中华文化没什么两样,那又怎么是去了解英文英语文化呢?所以我从来我念翻译过来的作品。从开始学英语的时候就是这样。
(七)
另外,背记单词文章的时间的选择也是非常重要的,一般早上是最清醒的,早上听写可以效果上可以提升许多,因为反应能力快,辨别语音能力也快,另外背文章和口语的效果当然也是最快的,速率是最高的。但是晚上背东西的话可以保持长久记忆,比如说在背完东西后马上睡觉的话,印象会相当深刻,而且不会忘记。一般睡觉前背的东西只用稍微复习就可以长久不忘,不像其他时间背记东西需要反复复习。
自从上英语论坛以来,我交到了许多学习英语的朋友.但据我观察,英语狂热者并不是很多.当然其中也有许多和我一样热爱英语的同仁!可是我发现一个很严重的问题和恶性循环就是英语差的越来越差,英语好的越来越好,就像贫富差距剧增一样!而其中的原因就是,英语差的越来越讨厌英语,在讨厌的条件下对英语的兴趣更加是每况愈下!许多过了英语6级的朋友水平也不敢恭维。因此我写下下面的话以供大家参考。
学英语就像政治一样首先要制度一下总纲和基本纲领。然后分步制度和确立路线方针政策。总纲和基本纲领就是学英语的动力和目的!路线方针就是学习英语的方法,包括词汇,语法,听力,口语,阅读,写作,翻译的方法。
在我们学习一样东西,首先我们要扪心自问“我们为什么要学习它,为什么要学习英语”如果在你心中有那么一个声音“我是为了考试,我是逼迫的”那么好,下面的你都不用看了!因为下面的内容不仅仅是为了考试的。本人个人认为考试确实测试水平的手段。但我们应该是先为了学习而考试,而不是为了考试而学习。如果答案是肯定的“我喜欢英语”那么我相信我所说的话一定会有所帮助的!
在过去实践学习一年中,我常常和一些英语学习者交流。他们有时候会问我“我英语不好,糟透了”。那我首先会问“你想学好英语吗?那好,我问你,你能背下新概念4册吗?”很遗憾的是,我得到的答案常常是“啊。。。”“我不行”“那太难了”。如果正在看此文的你,心中的答案也是否定的,那么英语你就不用学了。我问这个问题。并不是说每个人都要背新概念。而是从这可以看出一个人学习的毅力和决心。当别人总是问我“你能。。。。样吗?”的时候,我会毫不犹豫地说“我行我一定能做到,等着看我的结果吧”因此我常常能够更快的找到别人更有效的学习方法。因为我在心中肯定自己以后,马上就开始动手探索行动。
我常常对我父母说“我是风雨无阻,无坚不摧的,我要考哈佛大学的博士”。其实我知道没有一个人能理解我的行为了。我的父母更不会理解我的话!他们总是认为我太狂妄自大。老是说“你那是海市蜃楼”。诸如此类的话,我已经听别人说过无数遍了。他们老是说,还是脚踏实地一些吧!虽然我不是幼稚无知到反击。但是我也在心中想“我的天啊,这人这么这样啊”。我一直认为,我理想是伟大的,我一定能够学成英语,我一定要不断完善自我,其实我并不是没有考虑到现实,而是想,不管我最后有没有达到目的地,但是当某个时候我回首往事的时候,我可能会发现我已经迈上许多层台阶,如果当初没有这个理想,我可能还在最底下。说不定我哪天就真的能够达到我的目标呢!我也并不是说一定要考哈佛,而是作为一种鞭策自己的动力,给自己信心给自己鼓励。而且我发现那些不相信奇迹,相信“一切都是命中注定”的人都是那些失意者。其实他们根本没有从自身原因考虑过,他们为什么会失败?他们所谓的失败正是因为他们是信奉“命中注定”者,所以不去努力了,放弃了。而真是他们这种妥协几注定他们一辈子必将失败,这就是一个恶性循环了,他们所谓的“命中注定”的这个命恰恰是他们自己给自己的,而他们自己还茫然不知,混混沌沌。
学英语也是一样的道理,所以说错过了就不要再错下去了,而不错的要让自己更加完善。学习贵在兴趣,首先一个人要清楚自己为什么学英语,我发现那些为了工作考试或者想从英语方面得到什么实际利益的人,一般很难坚持到最后都放弃,比方我之所以能坚持,是英语我本身学英语就觉得快乐,因为我从小就对远古对世界各民俗风物历史等等有着浓厚的兴趣,所以英语正好就成为了一种桥梁,一把打开这扇大门的钥匙。
我为了学习英语身体受到了很大的摧残,我的听力下降很快,以前是用耳机伤害了听力,听课都困难了,后来恢复了一点点没,但是现在听力分贝仍然丢失很严重的,所以只得改用外放的。我和别人说话都必须使用助听器,但是我不后悔,我虽然有时候很伤心啊,因为世态炎凉,人心不古,落井下石的人真的是太多了。但是我从来没有后悔过学习英语,我喜欢有什么错,我还是会坚持下去。
所以说学英语首先必须有勇气,二是有毅力。然后就是英语环境,天分等等。其实说实话,我不是个说空话的人,经过我观察,我发现语言天分好记忆力好和善于创造发掘的人如果做出努力者同样的努力其效果远远要高于后者,这是没有办法改变的事实。
还有一个就是怎么样让自己常常雄心勃勃,我觉得近朱者赤,近墨者黑,要常常与英语学习者结伴一起学习一起促进,这样你就会觉得,恩,我学英语是很有用的。
如果常常帮助别人,比如说,再向别人解说英语的时候,就会觉得,我真的很有用,我真的很厉害哦,原来学英语是可以帮助这么多人,我是挺了不起的人的,这样我就会觉得信心满满,一直帮助别人就会一直有信心而且如果在帮助别人的同时发现自己的不足,就要翻找资料以查缺补漏。使自己感觉到自己的不足就为了不耽误别人也不耽误自己而更加努力。
(二)听力口语
现在我来谈一谈听力,作为一名英语专业的学生,听力一度是我最头疼的问题。我也曾经用过一些不错的方法,结果最后都是不了了之。我曾经用得最多的方法是《逆向法》和《千万别学英语》。不过似乎《逆向法》对我来说收效并不是那么明显。显然比较花费经历和时间。我感觉我比较适合《千万别学英语》的方法。
但是,在学习的过程中,我发现这两种方法本身就是有其缺陷的。我个人认为这两种方法结合起来再根据每个学英语的朋友的本身情况的不同,因材施教,才能在最短的时间里取得最大的成果。我已经结合这两种方法,定制一套适合我自己的学习方法,已经用了两个月,显然效果要比纯用这其中的一种要好得多。我已经能够很自然的听标准英语了。错误有时会有,但并不是很多。
现在我来结合这两种方法说一说我在此方面取得的成果。
我一开始选用的材料就是VOA标准英语的资料,我曾经听别人说,不经过学习慢速英语就想在标准英语上取得效果是不可能的。可是我并没有采纳那位朋友的意见。因为我基础并不差,只是听力没有达到我自己希望的水平罢了。我一开始就做了长期抗战的思想准备。可是事实其实没有我们想象中那么难。不过我想这是因为我没有用纯《逆向法》的缘故。我是先用了《千万别学英语》打通耳朵,然后再听写,我在听写的过程中是一边写一边跟读模仿的。不过我想并不是每个朋友都能像我这样的。如果基础比较差的朋友建议使用比较通俗一点的材料。像是比慢速英语速度快一些但又比标准英语慢一些的资料。像我以前基础不好的时候就用过语速大约为120/1的短篇故事集(外声朗读)。1个月就取得了不小的成果。最好不要用慢速英语,在我看来那有点浪费时间。因为人本来是可以接受正常语速的。是可以适应的。如果用语速过慢的资料,我想以后再去接受正常语速的资料会很大程度的困难,那又何不让自己更快的达到目标呢!有的去外国定居或是留学的朋友,也不能要求他们周围的环境去适应他们,他们只能自己去适应他们周围的英语环境。不一样都很好的度过了吗?在我看来像《逆向法》那样一个个去“抠”的话真的是一种不小的痛苦。如果能先照〈千万别学英语〉介绍的方法那样,先听熟一盒磁带,先不要想他的意思。当你能很自然的听下去,而且觉得可以适应语速,能听清楚95%的词的发音的时候,再开始听写。那样真的要轻松得多了。至于〈千万别学英语〉是否真的需要经过5个阶段,那就看个人志愿和自身情况了。不过〈千万别学英语〉的材料毕竟是适合有基础的朋友。基础比较差的朋友在选材料上还是要比较慎重一些。因为毕竟时间最好不要浪费在没有效果没有意义的事情上。
还有就是练听力的时间也很重要,对效果有很大的影响。如果泛听的话最好是选择在有一点点睡意的时候听是最好不过的了。因为那样的话,那种英语意识就完全输入到你的大脑潜意识里面去了。当你瞌睡的时候要控制头脑不要想睡觉的事情最好是注意耳边的语音。所以说睡前泛听是效果最高的。或者是早晨刚起来的时候听也不错。因为睡意未退嘛~!
本篇写于去年,当时由于时间紧急并没有经过修改所以存在一些缺陷,《千万法》的缺陷并没有很具体的写进去,所以今天特地整理一下:
万法的缺陷就在于,前两个步骤作用之大相当惊人,而后面的效果就递减,而且要花费相当长的时间,千万法里面推荐的增加词汇量和提高阅读能力已经口语的方法,不怎么好。
原因有几点:
1.用英英词典增加词汇量,没有8000词的基础,不要做这种无谓的尝试,因为比如有些专业名词,并没有具体解释,只是用简单的最基础的3000----3500词做解释,比如某种花或者树木草等,英语词典里面只解释其性质,比如什么形状生长在哪,什么颜色,这样不熟悉的我们就会被搞得云里雾里不但会不能增加词汇量还会误解。另外解释一下,因为很多著名的英英词典比如朗文,牛津的都将解释单词的基础词汇压缩在3500以下,所以我这里才说有些词汇解释不能清楚明了。
2.还有就是阅读,千万法作者推荐看报纸,但是我发现报纸的专业词汇量超大,而却语法相当难和复杂,没有经过长期的训练和很好的基础一张大报很难读完,因为科技文商业文是比较难读的,而我们多半在学英语的时候都是接触的文学类性质的文章,所以建议训练阅读科技文在阅读项目的最后一个项目。
3.作者说应该按照像播报员一样讲报纸内容自我演讲出来,这样是极其错误的,因为我们还在学习英语阶段,很多朋友都没有真正受过觉得对纯正的语音训练,如果报纸内容没有语音资料,很容易误入歧途,结果最后变成不伦不类的英语。
口语部分:
我本来口语是很差的,不过经过自我苦练正在不断提升中。我个人比较喜欢美语。因为美语比英语口型起伏大得多,比较形象化,感觉比较轻松有趣!不如如果你想学习英英最好从美到英音的转变。大家都知道学口语复读机绝对是必不可少的。我的方法也和普通方法是一样的。就是疯狂模仿背咏,然后实践.我们这里没有什么英语角,很少。我能做的就是,用复读机录下自己的声音。一般内容都是2个人对话,一个A一个B。先把A说的录下来。然后复读机问。我答,做完几遍后在换过来.我问复读机答.还有一种就是背时文和演讲稿,背时文的时候把自己当成新闻主播在镜子面前不断重复演练。背演讲稿的时候就真的融自己的感情,把自己当成真的在演讲!不要太注重你所说的内容,而是要想你现在要某某人或某些人表达什么!过一段时间后你会发觉你的英语越来越流利.也许这听起来有些可笑。这是在没有语言环境下不得已的方法。不过真的有很神奇的效果!当然还有就是可以上网上外语角!当你的英语打得比中文快。那么可以说你的英语口语水平已经有一定的程度了!如果有英语角和英语环境当然是更好!网上练口语也是不错的选择。但是缺乏逼真性。
跟大家开个玩笑涂唇膏润润唇可以使我们说英语的时候不结巴,一溜烟的就说过了,因为涂纯油很滑,这个绝对有效,电视里的播报员不是不怎么打结巴的?呵呵
(四)词汇阅读部分
我们都知道词汇是基础。没有词汇量什么都免谈。一年我也曾经拿着单词书死背。背了两遍结果最后什么都忘了。后来我在不断摸索中发现。背单词不如背句子背句子不如背文章。背文章才是一举三得。一是增加词汇量,并能灵活运用;二是使语法更容易学;三是提高写作能力。再加上阅读双管齐下,那效果是神奇的。建议开始进行阅读训练时不要用难度太高词汇量太大的科技经济等类文章。先从外国趣文民俗文化等书看起。再看小说。再看专业性文章。最后看文学类作品(如散文议论文等)和英文原版小说。我开始进行阅读的时候是用的一套“DO YOU KNOW”系列的书。一套5本。每本都介绍了世界各地地理,历史,旅游,政治,礼仪,文化,民俗等等内容。可读性很强。并且每篇后面都附有重点词汇和不易理解的专业名词和词组习语。然后就是附有阅读理解题。非常好的一套书!建议初学者和要过4.6级的朋友看看!词汇量达到一定程度后选一种好的英语杂志是必要的。我首推〈英语沙龙>和〈疯狂英语阅读版〉。因为上面的生词表不但表面了4.6级必背词汇词组。而且还说明了区别用法!内容也蛮丰富的!排版更好!而〈疯狂英语〉的知识面广含盖量大,对西方文化都做精辟的解说,最重要的是彩页,彩页能够加深的大脑的印象和记忆力刺激视觉。
(五)语法写作
语法一直是我的老大难题。看着那么一本字典一样厚的语法,只想流泪!但是泪也只能往肚里面吞.所以我建议各位先背文章不是没有原因的.如果不做翻译工作者,只要有非常强的语感,了解基本语法结构就够了。因为就算你不知道一些语法结构。但是语感强的没有这方面的知识一样能分析出来,并且写得一手好文章.当然英语专业又另当别论了!
(六)材料的选择推荐
选材料对自信的建立和对英语兴趣的提升也是非常重要的,像我从来不选择自己不喜欢的材料去作为阅读资料和听力资料。
我不喜欢政治,但是偏偏外国人一天到完谈政治VOA BBC新闻听力听力里面全是政治。所以在开始听写的时候我就选择慢速英语的feature听写了。也买了听力文摘,听写我自己喜欢的天文,人文,奇闻逸事等听写。像阅读就从念浅显的一些外国文化介绍等开始。后来再开始念散文,随笔等,也是我喜欢的,小说我觉得太长了。就从简单的全英文斑小说开始,都是一些童话啊比如〈绿野仙踪〉〈海底20000里〉等开始。是外国中学生念的改编过了的,压缩了原文,变得简单了,但是原味没有变,都是非常具有英语思维的生词也不是很多,但是非常有趣,都是外国出版公司请全世界各地有名的学者教师改编的。我读了5本以后发觉自己叙述文章,写记事文章刷刷的一下就写出来了,但是当然还没达到下笔如有神的境界,我听一位当翻译的朋友说他一个学姐现在在国外,以前学英语的时候,念了500本这样的书,写起文章来,那就不用说了,就像马良握着一种神笔一样。有了语感和一定基础后我就开始一边念疯狂英语阅读版杂志,一边看小说背美文等。
我从来不念科技文章,为什么呢,因为我本身不喜欢再者VOA英语里面已经有了,我看还是算了吧。
有一点很重要的就是,除了杂志以外我从来不念英汉双译的。我的书全是英文的,因为我从来就觉得中文就是中文,英文就是英文,大家都不仅仅是几个单词这样简单,都有很深的文化底蕴和内涵,原作者要表达的经过这个翻译那个翻译就不成样子了。虽然说翻译都必须忠实于原文,但是比较中国人深受中国文字中华文化影响,读翻译过来的作品等于在读中华文化没什么两样,那又怎么是去了解英文英语文化呢?所以我从来我念翻译过来的作品。从开始学英语的时候就是这样。
(七)
另外,背记单词文章的时间的选择也是非常重要的,一般早上是最清醒的,早上听写可以效果上可以提升许多,因为反应能力快,辨别语音能力也快,另外背文章和口语的效果当然也是最快的,速率是最高的。但是晚上背东西的话可以保持长久记忆,比如说在背完东西后马上睡觉的话,印象会相当深刻,而且不会忘记。一般睡觉前背的东西只用稍微复习就可以长久不忘,不像其他时间背记东西需要反复复习。
概率与我们的生活
花上几元钱,买一张彩票,然后就中了几百万乃至几千万元的巨额奖金,这大概是很多人梦寐以求的事。
可是这样的机会有多大呢?答案可能会令我们大吃一惊。
大英帝国彩票
“下一个赢家就是你!”这句响亮的极具蛊惑性的话是大英帝国彩票的广告词。买一张大英帝国彩票的诱惑有多大呢?只要你花上1英镑,就有可能获得2200英镑!
一点小小的投资竟然可能得到天文数字般的奖金,这没办法不让人动心,很多人都会想:也许真如广告所说,下一个赢家就是我呢!因此自从1994年9月开始发行到现在,英国已经有超过90%的成年人购买过这种彩票,并且也真的有数以百计的人成为百万富翁。如今在世界各地都流行着类似的游戏,在我国各省各市也发行了各种福利彩票、体育彩票,各地充满诱惑的广告满天飞,而报纸、电视上关于中大奖的幸运儿的报道也热闹非凡,因此吸引了不计其数的人踊跃购买。很简单,只要花2元的人民币,就可以拥有这么一次尝试的机会,试一下自己的运气。
但一张彩票的中奖机会有多少呢?让我们以大英帝国彩票为例来计算一下。大英帝国彩票的规则是49选6,即在一至49的49个号码中选6个号码。买一张彩票,你只需要选六个号、花一英镑而已。在每一轮,有一个专门的摇奖机随机摇出6个标有数字的小球,如果6个小球的数字都被你选中了,你就获得了头等奖。
可是,当我们计算一下在49个数字中随意组合其中6个数字的方法有多少种时,我们会吓一大跳:从49个数中选6个数的组合有13983816种方法!这就是说,假如你只买了一张彩票,六个号码全对的机会是大约一千四百万分之一,这个数小得已经无法想象,大约相当于澳大利亚的任何一个普通人当上总统的机会,如果你每星期买50张彩票,你赢得一次大奖的时间约为5000年;即使每星期买1000张彩票,也大致需要270才有一次六个号码全对的机会。这几乎是人靠人力不可为的,获奖仅是我们期盼的偶然而又偶然的事件。
那么为什么总有人能成为幸运儿呢?这是因为参与的人数是极其巨大的,人们总是抱着撞大运的心里去参加。孰不知,彩民们就在这样的幻想中为彩票公司贡献了巨额的财富。一般情况下,彩票发行者只拿出回收的全部彩金的45%作为奖金返还,这意味着无论奖金的比例如何分配,无论彩票的销售总量是多少,彩民平均付出一元钱只能赢得0.45元的回报。从这个平均值出发,这个游戏是绝对不划算的。
概率:直觉容易出错
在社会和自然界中,我们可以把事件发生的情况分为三大类:在一定条件下必然发生的事件,叫做必然事件;在一定条件下不可能发生的事件,叫做不可能事件;在一定条件下可能发生也可能不发生的事件,叫做随机事件。在数学上,我们把随机事件产生的可能性称为概率。严格说来,概率就是在同一条件下,发生某种事情可能性的大小。概率在英文中的名称为probability,意为可能性、或然性,因此,概率有时也称为或然率。
彩票是否中奖就是个典型的概率事件,但概率不仅仅出现在类似买彩票这样的赌博或游戏中,在日常生活中,我们时时刻刻都要接触概率事件。比如,天气有可能是晴、阴、下雨或刮风,天气预报其实是一种概率大小的预报;又如,今天某条高速公路公路上有可能发生车祸,也有可能不发生车祸;今天出门坐公交车,车上有可能有小偷,也有可能没有小偷。这些都是无法确定的概率事件。
由于在日常生活中经常碰到概率问题,所以即使人们不懂得如何计算概率,经验和直觉也能帮助他们做出判断。但在某些情况下,如果不利用概率理论经过缜密的分析和精确的计算,人们的结论可能会错得离谱。举一个有趣的小例子:给你一张美女照片,让你猜猜她是模特还是售货员?很多人都会猜前者。实际上,模特的数量比售货员的数量要少得多,所以,从概率上说这种判断是不明智的。
其实,上面所说的彩票问题也反映了人们对概率自以为是的直觉是多么靠不住。人们在购买彩票时总是只看到那些中了大奖的故事,而不愿去考虑中大奖其实是个最典型的小概率事件,其概率低到根本不值得去买。数学家认为,概率低于1/1000,就可以忽略不计了,而大英帝国彩票中头等奖的概率只有一千四百万分之一,即使是选号范围小一些的彩票,中到头等奖的概率一般也要五百万分之一,这样小的概率居然还有那么多人趋之若鹜。有笑话说全世界的数学家都不会去买彩票,因为他们知道,在买彩票的路上被汽车撞死的概率远高于中大奖的概率。
人们在直觉上常犯的概率错误还有飞机失事事件。也许出于对在天上飞的飞机本能的恐惧心理,,也许是媒体对飞机失事的过多渲染,人们对飞机的安全性总是多一份担心。但是,据统计,飞机旅行是目前世界上最安全的交通工具,它绝少发生重大事故,造成多人伤亡的事故率约为三百万分之一,假如你每一天坐一次飞机,这样飞上8200年,你才有可能会不幸遇到一次飞行事故,三百万分之一的事故概率,说明飞机这种交通工具是最安全的,它甚至比走路和骑自行车都要安全。
事实也证明了在目前的交通工具中飞机失事的概率最低。1998年,全世界的航空公司共飞行1800万次喷气机航班,共运送约13亿人,而失事仅10次。而仅美国一个国家,在半年内其公路死亡人数就曾达到21000名,约为40年前有喷气客机以来全世界所有喷气机事故死亡人数的总和。虽然人们在坐飞机时总有些恐惧感,而坐汽车时却非常安心,但从统计概率的角度来讲,最需要防患于未然的,却恰恰是我们信赖的汽车。
随意的估算也不准
不断地抛一枚硬币,当它落到地上时,出现正、反面次数相同的概率是多少?很多人都会以为随着抛硬币次数的增加,正、反面出现次数相同的概率也在递增,但这个想法错了。恰恰相反,其概率随着抛硬币次数的增加在递减。抛2次时出现正反两面各1次的概率是50%,抛6次时出现正反两面各3次的概率是31.25%,抛10次时出现正反两面各5次的概率是24.61%,抛100次时出现正反两面各50次的概率只有大约8%(当然,随着抛地次数的增加,正、反面出现的次数非常接近,就是难以做到完全相同)。这说明,面对一个貌似简单的概率问题时,我们如果随意估算,轻易下结论,可能与实际情况恰好南辕北辙。
我们来看一个经典的生日概率问题。以1年365天计(不考虑闰年因素),你如果肯定在某人群中至少要有两人生日相同,那么需要多少人?大家不难得到结果,366人,人数只要超过365人,必然会有人生日相同。但如果一个班有50人,他们中间有人生日相同的概率是多少?你可能想,大概20%-30%,错,有97%的可能!
它的计算方式是这样的:
a.50个人可能的生日组合是365*365*365……*365(共50个)个;
b.50个人生日都不重复的组合是365*364*363*……*316(共50个)个;
c.50个人生日有重复的概率是1-b/a。
这里,50个人生日全不相同的概率是b/a=0.03,因此50个人生日有重复的概率是1-0.03=0.97,即97%。根据概率公式计算,只要有23人在一起,其中两人生日相同的概率就达到51%!但是,如果换一个角度,要求你遇到的人中至少有一人和你生日相同的概率大于50%,你最少要遇到253人才成。再来看一个常见的抽奖例子。参加抽奖,当然人人都想得奖,这时候该先抽奖还是后抽,才能让中将概率提高呢?
恐怕很多人都会在这个问题上犯糊涂,让我们用科学方法解决这个问题吧。假设有两个酸苹果,一个甜苹果,甲乙丙一次从箱中摸出一个,谁最有机会吃到甜苹果呢?首先,甲的机会是三摸一,所以甲摸到甜苹果的概率是1/3。乙的机会如何呢?甲没有摸到的概率是2/3,然后在这个概率中计算乙摸到的概率:
(2/3)*(1/2)(只剩2个苹果让乙摸)=1/3,所以乙摸到甜苹果的概率是1/3。丙呢?丙只有在甲、乙都没有摸到的情况下才可能摸到甜苹果,所以扣掉甲、乙摸中的概率,就是丙的机会大小了,其概率是1-(1/3)-(1/3)=1/3。
明白了吗?不管先摸也好,后摸也罢,每个人摸到甜苹果的机会其实都是一样的。
从赌博中发展的概率理论
既然一个事件的概率凭感觉随便估计总是容易出错,而概率又与人类生活息息相关,那人们就得严肃对待概率问题了。
概率问题的历史可以追溯到遥远的过去,很早以前,人们就用抽签、抓阄的方法解决彼此间的争端,这可能是概率的最早应用。而真正研究随机现象的概率论出现在15世纪之后,当时的保险业已在欧洲蓬勃发展起来,不过,当时的保险业非常不成熟,只是一种完全靠估计形势而出现的赌博性事业,保险公司要承担很大的不确定性风险,保险业的发展渴望能有指导保险的计算工具的出现。
这一渴望戏剧性地因15世纪末赌博现象的大量出现而得到解决。当时的主要赌博形式有玩纸牌、掷骰子、转铜币等。参加赌博的人,特别是那些专门从事以盈利为生的职业赌徒,鏖战赌场,天长日久就逐渐悟出了一个道理:在少数几次赌博中无法预料到输赢的结果,如果多次进行下去,就可能有所预料,这并不是完全的碰巧。这无意中就给学者们提供了一个比较简单而又非常典型的概率研究模型。
1654年,有一个法国赌徒梅勒遇到了一个难解的问题:梅勒和他的一个朋友每人出30个金币,两人谁先赢满3局谁就得到全部赌注。在游戏进行了一会儿后,梅勒赢了2局,他的朋友赢了1局。这时候,梅勒由于一个紧急事情必须离开,游戏不得不停止。他们该如何分配赌桌上的60个金币的赌注呢?梅勒的朋友认为,既然他接下来赢的机会是梅勒的一半,那么他该拿到梅勒所得的一半,即他拿20个金币,梅勒拿40个金币。然而梅勒争执到:在掷一次骰子,即使他输了,游戏是平局,他最少也能得到全部赌注的一半30个金币;但如果他赢了,就可拿走全部的60个金币。在下一次掷骰子之前,他实际上已经拥有了30个金币,他还有50%的机会赢得另外30个金币,所以,他应分得45个金币。
赌本究竟如何分配才合理呢?后来梅勒把这个问题告诉了当时法国著名的数学家帕斯卡,这居然也难住了帕斯卡,因为当时并没有相关知识来解决此类问题,而且两人说的似乎都有道理。帕斯卡又写信告诉了另一个著名的数学家费马,于是在这两位伟大的法国数学家之间开始了具有划时代意义的通信,在通信中,他们最终正确地解决了这个问题。他们设想:如果继续赌下去,梅勒(设为甲)和他朋友(设为乙)最终获胜的机会如何呢?他们两只多再赌两局即可分出胜负,这两局有4种可能的结果:甲甲、甲乙、乙甲、乙乙。前3种情况都是甲最后获胜,只有最后一种情况才是乙取胜,所以赌注应按3:1的比例分配,即甲得45个金币,乙得15个。虽然梅勒的计算方式不一样,但他的分配方法是对的。
三年后,也就是1657年,荷兰著名的天文、物理兼数学家惠更斯把这一问题置于更复杂的情形下,试图总结出更一般的规律,结果写成了《论掷骰子游戏中的计算》一书,这就是最早的概率论著作。正是他们把这一类问题提高到了理论的高度,并总结出了其中的一般规律。同时,他们的研究还吸引了许多学者,由此把赌博的数理讨论推向了一个新的台阶,逐渐建立起一些重要概念及运算法则,从而使这类研究从对机会性游戏的分析发展上升为一个新的数学分支。
由赌徒的问题引起,概率逐渐演变成一门严谨的科学。
概率计算:与庄家博弈
概率理论从赌博中发展而来,又反过来成为赌场老板赚钱的强大工具。进入赌场的赌徒总是相信自己运气十足,孰不知赌场庄家早已利用概率规律为他们设下了陷阱。例如,很多赌场里的老虎机上都顶着跑车,下面写着告示,告诉赌客已经有多少人玩了游戏,车还没有送出,暗示现在轮到你的机会大增。但这其实是赌场利用概率规律为赌徒设下的一个诱惑陷阱。该旅里有一个重要的规律就是随机事件的独立性,在随机事件中下次事件发生与否,与上次事件是没有关系的。设想一下,前面10个人抛硬币,没有一个人抛出了正面,现在轮到了你,难道你抛出正面的可能性就大于其余的人?因此,只要得大奖的规则没有变化,
每人是否幸运,和前面的人是否中奖毫无关系。
但人们通常都对这个规律无知无觉,很多情况下,人们因为前面已经有了大量的未中奖人群而去买彩票或参与到累计回报的游戏,实际上,并不因为前面没有人中奖你就多了中奖的机会。
庄家在参与赌博游戏时都已经设计好了一个有利于自己的概率,而很多玩家却浑然不觉。以10年前盛行于北京的摸球中奖事件为例:布袋中有六个白球与六个红球,以随便摸出6个球来确定是否中奖。奖励规则为:
a)摸出6红或6白可得3元钱;
b)摸出5红1白活5白1红,可得2元;
c)摸出4红2白或者4白2红,可得1元钱;但若摸出的是3红3白,则玩家需付3元。
表面上看,共有7种情况,竟有6种情况可获奖,而只有1种情况要“挨罚”,非常合算。但实际上赢钱的人很少,而如果连摸5次以上,几乎是必赔无疑。
这里,庄家使用了什么障眼法呢?其实,根据概率的知识,这个玩法的答案非常简单。从12个球中摸出任意的6个球,共有924种情况,其中出现6红或6白的情况,都只有1种,概率为千分之一强;而出现5红1白或5白1红的机会相等,分别为36次,其概率总共不到8%;出现4红2白或4白2红的机会相等,分别为225次,两者总概率为48%;而摸到3红3白的次数为400次,概率大致为43%。按照上述概率,当玩家摸四次球时,最大的可能为两次3红3白(共赔6元),另两次为4红2白和4白2红(获得2元),这样玩家一般都会损失4元钱,平均一次损失1元。当玩的次数增加,玩家平均损失的钱数几乎总保持在此数额。由于庄家利用了概率论,成了不变的赢家。
然而,当此类游戏的表面现象复杂时,有时“入套”的就不只是玩家,而有可能是庄家吃黄连,有苦说不出,因为玩家掌握了更精妙的概率理论。在历史上曾发生过数学教授打败赌场无敌手的有趣故事。在1959年,当年轻的麻省理工学院数学教授爱德华·索普陪妻子去赌城度周末时,出于职业上的爱好(数学教授也许对有关数学的东西统统感兴趣),他也去一试手气。他拿出10美元来玩21点的游戏。起初索普只想不要输得太快。但后来,索普聪明的大脑开始理性运转,结果大为改观。
21点的游戏规则非常简单:把一副牌放在桌上,然后轮流抓牌。A代表1点或11点2-10代表2-10点,花牌J、Q、K、大王、小王代表10点,在一局里,你手上牌的点数相加越接近21点就越大,玩家的点数只要超过庄家点数就算赢,但是超过21点就爆牌作负。21点玩法特殊性在于,前一局都对后来的输赢有影响,因为用过的牌会出局,剩下的牌越来越少,这意味着剩下的牌可以猜到,因此高手都会记住以前的牌,这就是21点的诀窍所在。因为规则规定玩家可以自由决定自己所要的牌数,而要求庄家手中的牌不能少于17点,
如果台上大牌(点数大的牌)数量较多,那么庄家就很可能爆牌作负。索普认识到如能猜出没有出局的牌的特征,那么他就可算出庄家爆牌的可能性,进而确定有利于自己的赔率。
获胜的关键在于,要准确记忆每张牌的点数,要有过目不忘的本领和极快的心算。这几乎是不可能做到的,但索普发明了一种用近似值大体记忆的方法,首先将这些牌分为3组,然后赋予每一组相同的点数:第一组2-6的赋值为+1,因为这些牌从台面上被清出去是有利于玩家的,第二组7-9赋值为0,这几张牌处于中性;第三组10、大王小王和花牌,赋值为-1,因为它们被清出台面不利于玩家,如果台面上这些牌越少,那么庄家爆牌的概率也就越小,。现在玩21点时,我们只需记忆这些牌是+1还是0或者-1就可以了,比方说大王出局,那么它是10点,我们就认为它是-1,照此类推,当出局的牌的总结果为正时,那么说明剩下的牌中大点数较多,玩家胜算的概率就大一些;如果结果为负,庄家的胜算就大一些;如果结果为零,双方的胜负概率大体相当。索普里用这套简单易行的策略,在赌城中狂赌三天三夜,最后1万美元变成了近2.1万美元,利用概率论打破了庄家不可战胜的神话。后来他出版的书《打败庄家:21点游戏赢的策略》成为玩家手中的赌博圣经。
相同的概率,不同的结论
有时,面对同一个概率事件,随着问题的着眼点不同,我们得出的结论可能截然相反。这一点会使一般人感到迷惑不解,我们在这里打一个通俗的比喻:某人是嫌疑犯,也找到了一些他的犯罪证据,但不是决定性的,若我们要求“只有找到了更重要的犯罪证据才能判他有罪”,则他将被判无罪;反之,若要求“只有找到了证明他没有犯罪的重要证据才能判他无罪”,则他将被判有罪,在这里,着眼点的不同决定了不同的判罚。
这方面的著名事例是辛普森杀妻案。
1994年6月12日深夜,美国洛杉矶西部一个豪华住宅区里,一只小狗在不停地狂吠,引起了邻居的注意。
当人们随着狗吠声来到一住宅门前时,赫然发现两具血淋淋的尸体!警察接到报警后迅速赶到现场,发现两名死者是美国黑人橄榄球明星辛普森的妻子和一个餐馆服务员。
警方在经过大量艰苦细致的调查后,搜集到了大量证据都表明辛普森有重大杀人嫌疑:他的汽车上染有死者血迹,车道上也发现血迹,案发现场还有染血手套和其他证据;还有证人作证说在辛普森妻子死亡的时间段内看到了辛普森就在豪华住宅附近;历年报警记录还显示辛普森曾多次暴力虐妻。这些证据都对辛普森极为不利,检察官据此向法院控告辛普森犯有一级谋杀罪。遗憾的是,控方所提供的证据中有一小部分因不符合法定程序而不被法庭采信。即便如此,在这起案件中,辛普森杀人的概率也有95%以上。然而,
最后的审判结果却让全世界大吃一惊:辛普森被无罪释放!
原来,美国的刑事法律是建立在无罪推定的基础上的,尤其是对于杀人案这样重大的案件,要最后给被告定罪,控方所提供的证据要近乎100%令人信服才行,稍有疑问就不得被判有罪。95%以上的概率不足以使辛普森被判有罪。
颇具戏剧性的是,当辛普森前妻的娘家在向法院提起民事诉讼时,法院却判决辛普森输,赔偿原告3350万美元。之所以有如此结果,是因为刑事审判与民事审判的证据采用规则有差别,在民事诉讼中,原告提供的证据只要比被告的有说服力就可以赢。用数学概率表示,刑事诉讼中控方需要近乎100%的证明,民事诉讼中原告只要证明有51%以上的可能性即可。在这起案件中,95%以上的概率足以使辛普森赔得倾家荡产。
【概率与我们的生活】
你能很好地判断事情会发生,或不会发生吗?专家也许会告诉你
恼人的电梯
三百多年前,有个赌徒问法国数学家巴斯加,掷骰子时,怎样计算落在某一面上的可能性?巴斯加对这个问题的回答就是“概率论”的开端。现在,“概率论”已经是数学的一个发展最快的分支。
在日常生活中,我们一般都是根据直觉或常识对可能性作出粗略判断。在大多数情况下,这是很可靠的。然而,在许多问题上,事实上的可能性与人们的想象却又相差很远,有的甚至会使人感到吃惊。
物理学家乔治·伽侔曾经在一座七层的楼里工作,经常需要从他二楼的办公室到六楼的一间办公室去。也许有点奇怪,在等电梯的时候,他希望电梯正在往上来,而实际上电梯几乎总是在由上往下走。“他们也许在楼顶制造电梯,一部一部往下放,都放到地下室里储藏起来。”他嘟哝道。然而每当他要从六楼往下去的时候,停在他面前的电梯却又总是上行的。
其实,对这种现象的解释很简单:如果你在低层等电梯,大多数时候电梯总在你上面,因此停在你那一层楼时它是下行的;相反,如果你在接近顶楼的高层等电梯,那么,电梯往往又在你下面,所以停在你面前时它是上行的。但是,人们往往凭直觉想象,认为不管在哪一层楼,开来的电梯是上行还是下行的可能性应该差不多。
生日的难题
概率计算最困难的问题之一,据信就是数学家们所谓的“生日难题”。假设你参加一个有23个人出席的聚会,有多大可能性其中有两个人是同月同日出生的?也许,凭直觉你会感到,这种可能性很小。其实上,在23个人中间,能有一对生日相同和这些人生日全部不同这两种可能性是基本一样的。
人越多,生日相同的可能性就增长得越快。如果是30个人,这种可能性就大于十分之七。如果是50个人,可能性就大于百分之九十七!也许以后在有23个或更多人的场合,你自己也会去试一试。顺举一例:在美国总统中,就有两位生日是完全相同的:詹姆斯·波尔克和沃伦·哈丁都出生于11月2日。
--流言为什么会不胫而走
还有一种现象使直觉概率判断者大为震惊,那就是“小世界问题”。这种事情并不罕见:你遇到一个来自远方的陌生人,通过交谈,竟发现你们有一个共同认识的朋友。也许,你们之中有一个会吃惊地叫起来:“这个‘世界’太小了!”
确实如此。马萨诸塞工学院的社会学家通过研究发现,在美国,平均每个人直接认识500个人。当然,每个人又都是许多不同的“熟人链”中的一个环节。他们进一步计算,结果随意挑出两个美国人来,例如说史密斯和布朗吧,那么他们俩相识的可能性只有二十万分之一。但是,史密斯认识某人,某人又认识另一个人,而另一个人认识布朗,这种可能性却高达一半以上。
心理学家斯坦利·米尔格莱姆曾经研究过“小世界问题”。他首先确定一个“目标者”一个在马萨诸塞州剑桥市正在学习当牧师的年轻人的妻子。然后,在堪萨斯州的维契托市又随便找了一组人作为“出发者”,给他们每个人一份文件,叫他们寄给他们的一个最有可能认识那个“目标者”的熟人。接到文件的熟人依同样办法再把它寄给自己的熟人,使这条“链子”有希望地接续下去,直至连接到“目标者”。叫米尔格莱姆吃惊的是,仅仅过了四天,一个男人就把文件送给了“目标者”,说:“艾丽斯,这是给你的。”这个过程是这样的:在堪萨斯的一个农人(“出发者”),首先把文件给了一个牧师,牧师把它寄给了他在剑桥的一个牧师朋友,这个人就把文件交给了艾丽斯。从“出发者”到“目标者”,这条“链子”只有两个“中间人”!
在这次试验中,各条“链子”的“中间人”数最少的是两个,最多的是十个,平均数是五个。然而,如果事先叫人估计一下,大部分人猜想需要一百个。不难想象,这样的“熟人网”便能很好地解释,为什么一些流言、有意思的新笑话会那么迅速地传遍全国。
直觉经常在犯错误
要求出某种结果的概率,就必须弄清有多少种其他各不相同的、同样可能出现的结果。你抛起一枚硬币,落地只会有两种可能:面朝上或面朝下。所以,两种可能的概率都是二分之一。但是换一个问题,你的直觉就不那么可靠了。
举个例子:如果一个家庭有三个孩子,那么这三个孩子都是一种性别的可能性有多少?有人可能这样推理:“至少有两个肯定是一种性别;那么,第三个或者与那两个相同,或者不同;所以,三个孩子同一性别的可能性是二分之一。”但是我们考察全部可能的结果,就会发现有八种不同的组合:男男男,男男女,男女男,男女女,女男男,女男女,女女男,女女女。所以,三个孩子同一性别的可能性应该是八分之二,即只有四分之一。
假设有一对夫妇希望要四个子女,是三男一女或三女一男的可能性大呢,还是两男两女的可能性大?
大部分人猜想是两男两女可能性大。但我们列出所有可能的结果(有16种)就可看到:有六种组合是两男两女,两种组合是四男四女,但有八种组合却是三一开的。所以,有一半的可能性是三男一女或三女一男,要高于两男两女。直觉又使我们错了!
出错的另一个原因是,人们在估计可能性的时候,往往假定一定的过程前后是有联系的。其实不然。
许多人设想,如果抛硬币连续几次都是面朝上落地,那么下一次很可能就是背朝上落地。其实,不论已经有多少次面朝上,下一次面朝上的可能性仍然有二分之一。
【你能中大奖吗?——概率与我们的生活】
统计开辟概率新天地
早期人们对概率的研究,都局限于我们日常接触到的有限事件的组合,例如玩牌、赌博中的计算问题,彩票的中奖问题,体育比赛时的抽签问题,等等,这些都是古典概率问题。古典概率只能处理诸如赌博中有限事务的组合,有非常大的局限性。而自然与社会中有许多事件是非常复杂的,如人口统计,男女出生统计、消费统计、各种民意调查等等,无法用简单的古典概率穷尽。经过几代数学家的努力,大约用了200年的时间,概率论发生了质的飞跃,具备了与统计结合的条件,出现了统计概率。
19世纪初,由于生产力的迅猛发展,统计事业开始走向昌盛。比利时学者A·凯特勒率先把统计方法从自然科学领域推广到社会科学领域,从而为人们认识社会发展规律的客观性打开了一扇窗口。
凯特勒认为,规律躲避着我们的理智,因为我们观察到的只是单个人的行为、个体特征我们无法记录下它们。因此,需要一种崭新的方法来反映社会的整体风貌和趋向。他把统计理论和概率理论结合起来,开创了统计概率应用的先河。
凯特勒仔细研究了当时法国,比利时和英国的司法刑事机关报的汇编,惊讶地发现,这些国家每年犯罪的次数大体不变,不仅如此,各种类型的犯罪也有惊人的重复性。凯特勒本人都为这些惊人的发现所震动,他感叹道:“这是人类多么可悲的性质啊!监狱、铁链、和断头台的命运对人类来说就像国家的收入一样,可以以某种概率被事先决定。我们甚至能预先计算出来,下一年会有多少人将用和自己一样的血弄脏自己的手,有多少人将是伪造者,多少人是投毒者,这一切就像能够确定出生与死亡的数量一样。”凯特勒还分析了人的“自由意志”的其他表现,如结婚、自杀等,也得到同样的结果。在我们以为完全是由个人的自由意志决定的地方,仍然有客观规律在起作用,这真是冥冥之中无法逃脱的宿命。
凯特勒的报告引起了当时社会的轰动。从此,统计走出了原来的杂乱无章的状况。把概率应用到统计中凯特勒是第一人,科学史上将凯特勒开创性的工作看作是现代统计学的起点,凯特勒也被誉为“现代统计学之父”。从那以后,统计概率在社会中的应用得到了蓬勃发展。这些调查使得人类首次能够从数学的角度审视自身行为,并把它们置于概率论模型之下进行研究。
用概率来统计的社会
统计概率对人们的观念的影响是深远的,自从十九世纪二三十年代凯特勒开创了统计概率以来,人们对统计数据规律性的信任超过了以往的任何一个时代。国家以各种报表来了解工业、农业、国防、人口、消费、犯罪等方面的资料。制定银行利息的高低需要消费指数和通货膨胀率,如果通货膨胀率高,消费指数低,银行就会考虑提高利率,反之亦然。国家举行重大活动需要了解几百年甚至上千年的天气资料,以避免遭遇恶劣天气的影响。例如,1990年北京亚运会的举办时间为9月22日至10月7日,就是因为根据统计资料显示,北京这期间遭遇恶劣天气的概率非常低。
今天,统计概率已经渗透到社会科学、自然科学的方方面面,特别是计算机的广泛应用,使社会统计工作得以全面展开。我们耳熟能详的食品检测报告、人口统计、犯罪统计、国民生产总值的统计,国家和公司为了了解民意所进行的民意调查,市场调查,无一不涉及统计概率。同时,概率统计已经变成现代人理念与信念体系中的一部分。比如,对于美国大选,可以通过民意调查,用概率统计的方式计算某个候选人的受欢迎程度。而反过来,这种数据又在一定程度上左右人们的好恶,只是民意倾向变化,而影响到实际的选举过程。这说明概率统计的观念也影响到每一个人的行为和生活方式。
总之,从概率的思想走出机会性(博彩)游戏的范围,到应用的不断深化,这一过程中人类的思想观念发生了巨大的转变,这就是概率带来的革命。
概率不仅出现在人类社会生活中,在大自然的精心安排之下,生命的繁殖、进化也莫不服从于概率论的神奇安排。早在1843年,捷克修道士孟德尔首先为世人揭示了大自然的奥秘。醉心于自然科学的孟德尔,在闲暇研究植物的遗传规律,他选择了豌豆作为实验材料。豌豆是一种严格自花传粉的植物,它的雄蕊被花瓣包围,将外来的花粉拒之门外;同时,具有一些如高茎对矮茎、圆形对皱形、黄子叶对绿子叶、灰种皮对白种皮等具有明显差异的性状。孟德尔发现,当把不同品种的豌豆的这些性状遗传到下一代时,总是遵循着大约3:1的统计概率:高茎德与矮茎的植株比例为2.84:1;圆形的与皱形的植株比例为2.96:1;黄子叶与绿子叶的植株比例为3.01:1;灰种皮的与白种皮的植株比例为3.15:1。
现在,人们在教科书中称这个奇妙的比例为孟德尔第一定律,这个比例产生的原因是由于两种遗传基因在进入下一代的杂种细胞时,彼此分离,互不干扰,最后在生物传粉过程中随机结合,所以这个规律又称“分离定律”。后来孟德尔经过艰苦的探索又发现了两对性状不同的植株进行遗传时,不同对的遗传基因自由组合,而且机会均等。这就是孟德尔第二定律,也称“自由组合定律”。孟德尔发现的分离规律和自由组合规律实质上就是概率统计规律在遗传过程中的体现。
体现概率规律的世界
科学家们所揭示的规律在这个世界上处处存在,我们的世界正是一个充满随机事件又遵循着概率规律的世界。如果我们生活在一个完全必然的世界里,每个人什么时候出生、什么时候幸运、什么时候有难、什么时候死亡都早已“命中注定”,每个人的长相、性格、智力都早已被决定,几千年、几万年、几亿年后宇宙会是个什么样子也早已成定论,所有的事物从一开始就沿着确定的轨道向前发展,所有的结果都被我们预见到,这样的世界是多么的枯燥乏味啊!而一个完全不能预测的世界却是一个可怕、令人发疯的世界。
椅子从不会安静地待在那儿,一会儿在这儿,一会儿在那儿,一会儿又不知道跑到什么地方去;苹果熟了一会儿落到地上、一会儿飞到天上,甚至一会儿是苹果、一会儿不是苹果;生命会无缘无故死亡,也许根本就不会有生命。任何东西一会儿这样,一会儿那样,完全无章可寻,整个世界就会乱得一团糟。
我们要庆幸不是生活在那样完全必然或完全偶然的世界里,而是生活在这样一个随机而又规律的世界里,这才是一个井然有序而又丰富多彩的世界。扔一枚硬币,这次仍完应该和下一次毫无关系,硬币正面或者反面的分布似乎是一种无规则分布,可是当我们扔的次数足够多时,正面和反面出现的次数竟趋于一致,遵守着明确的统计规律。概率无规律随机变化的外表下,有着内在的秩序和规则。
人类在对机会性游戏的研究思考中创立和发展了概率理论,并使之成为人类的根本知识之一,今天,概率理论已融入人类日常生活。正如法国大数学家拉普拉斯所说:“这门起源于靠运气取胜的游戏的科学,竟然成了人类知识最重要的一部分。”
人类爱赌博,不仅仅是一种天性,更是在一个充满概率的世界面前的必然选择,每个人在成长的道路上都面临着无数的机遇和风险,这需要我们理解概率规律,并适时挑战概率规律,才能做生活的强者。
【爱情博奕论】
一对恋人好比一对囚徒,囚于情丝编织的牢笼,并在牢笼里上演爱情的博弈,谁输谁赢,往往靠的不是情深爱笃,而是博弈手段的高低优劣。
恋人,既是你的合作伙伴,也是你的对手,甚至敌人。在恋爱的这场不太好玩的“游戏”中,谁能熟练地驾御游戏或博弈规则,谁就是爱情的赢家。所以,要想成为赢家,就要学会与伙伴一样的恋人合作,还要学会与敌人一样的恋人周旋。面对对手围追堵截的爱情围剿,要学会闪转腾挪的诸多反围剿的手段。实在不行了,再三十六计走为上:对不起,不陪你“玩”了!
几乎所有的博弈论书籍,都从经典的囚徒困境说起。虽然那是说一对纵火犯的,但其原理也照样适用于“纵爱犯”。爱,本来也就是一场大火嘛。
还是让我们看看纵火的囚徒吧。在此,不能不引述一下“囚徒困境”的公案,尽管众所周知。需要说明,公案的版本不一样,但道理是一样的。
一场火灾之后,两个嫌疑人A和B被囚禁,分别关在两个独立的不能互通信息的房间里。如果两人都承认,则各被扣留8年;如果一人承认另一人不承认,承认的放走,并且得到奖赏,不承认的被扣留10年,并且被罚款;如果两人都不承认则因证据不足各被扣留1年。
我们看到,假定A选择承认的话,B最好是选择承认,否则就要被严重处罚;假定A选择否认的话,B最好还是选择承认,因为这样不仅可以被放走,而且还可以得到奖金。就是说,不管A什么态度,B的最佳选择都是承认。反过来,同样地,不管B什么态度,A的最佳选择也是承认。结果,两个人都会选择承认。
这就是有名的“囚徒困境”,所反映的问题是个人理性和集体理性的矛盾。理性告诉被实验者,谁选择抵赖,谁就可能会大倒其霉,而对方却逍遥自在,受到奖励。所以,这就出现了一个悖论,有时对同伙的忠诚,往往会把自己送上绝境,除非双方都能100%的忠诚,但,这是不可能的,谁能在巨大的利益诱惑面前永不动心呢?
恋爱中人,也类似于走入了囚徒困境。如果双方都不变心,那是最好的结局,在天愿为比翼鸟,在地愿为连理枝嘛;如果都变了心,效果也不坏,你走你的阳关道,我过我的独木桥嘛;如果一方变了心,另外找到了更好的情侣,一方却还傻乎乎地忠贞不二,那么,另觅新欢的一方是最幸福的,比两人都不变心的结果还幸福,因为他找见了更好的情人,而被抛弃的一方是最不幸的,比两人都变心的结果更不幸,因为他承担的压力既来自于对方的太幸福,也来自于自己的太不幸福。
按照囚徒困境的分析结论,恋人最得意的选择是另觅新欢,最天真的选择是天荒地老,最理性的选择是分道扬镳,最糟糕的选择是被另有新欢的对方无情抛弃。问题是,最得意的结局过于缺德,最天真的结局过于虚幻,最理性的结局过于残酷,最糟糕的结局又(让一方)过于心痛。
当然,囚徒困境模型给恋人最重要的提醒,是在应对策略上,最好要严格防范对方,就像一个囚徒防范另一个囚徒一样。
但是,反关现实中的恋人,大都希望能够天荒地老,没有谁愿意回头是岸,甚至被对方抛弃了还不死心。
为什么会这样呢?
因为纵爱的恋人与纵火的囚徒,其博弈状况有一个重大的不同。囚徒被警方抓住之后,是隔离审查,因此无法订立功守同盟,即便能够订立,谁又能保证自己或对方永远不毁约呢?
注意!这是囚徒困境模型的一个非常重要的条件,它往往被一些经济学家忽视了。
与囚徒所处的这个重要条件相反,恋人被爱神抓住以后,一般并不是隔离“审查”的,而是整天泡在一起。泡在一起做什么?除了发誓,还是发誓!
人生发誓最多的时期,大概就是恋爱期。发什么誓呢?无非是什么非你不娶非你不嫁你是风儿我是沙啊的一类誓言罢了,目的只有一个,就是让对方相信自己能够天荒地老而此情不渝。他们希望彼此忠诚,从而换来一个好的博弈结果。
而且,为了防止对方变心,总要设法让对方相信,你遇到我是你三生有幸,而我遇到你也是万世不毁。
可是,世间没有什么誓约是永恒的。很多爱情的悲剧,往往都从背弃誓约开始,而天下又似乎没有没有誓约的爱情。
这是整天泡在一起的恋人的博弈情形。而对于天各一方的恋人来说,彼此就像被隔离审查的囚徒一样,只不过不是被关在两间牢房,而是山高水阔知何处。按照博弈原则,他们除了违背誓约外,没有更好的选择。他们要想在恋爱中成为赢家,最好是不遵守爱的诺言,如此才能走出“囚徒困境”。
然而,这个结论在实际上是有问题的。因为,生活中恋爱成功的人并不少见,厮守一生一世的人也不少见,不能说他们都是勉强的。事实上,他们有的还的确生活得很幸福。
我相信,多数情侣的爱情生活过得还是可以的。那么,究竟是什么机制让他们能够信守誓约呢?
我一直在思考这个问题,但顺着博弈论的思路走下去,每次得出的答案似乎总与道德伦理有千丝万缕的联系。后来,偶尔看到一个试验,我猛然感到峰回路转林断山明了。
原来,存在于现实中的幸福的爱情生活,也可以从博弈论中找到答案。这应该感谢美国密西根大学的罗伯特·爱克斯罗德。他组织过一场计算机竞赛的试验。竞赛的思路非常简单:任何想参加这个计算机竞赛的人都扮演“囚徒困境”案例中一个囚犯的角色,他们开始玩“囚徒困境”的游戏,每个人都要在合作与背叛之间做出选择。关键问题在于,他们不只玩一遍这个游戏,而是一遍一遍地玩上200次,这就是所谓的“重复的囚徒困境”,于是这就更逼真地反映了日常人际关系。这又是一个值得注意的条件,最简单的囚徒困境模型说的是一次性博弈,正是这一点加剧了囚徒做处坦白的决心。这正像不相识的一对男女,偶尔在旅游中相遇,接着在宾馆里春梦一场,第二天就各自扬长而去,谁也不会忠于谁的,彼此也不会为对方今后的不忠实而又任何不快,原因就在于这是一次性的博弈(一次性的“博弈”和一次“性的博弈)。可是,如果男女双方互相认识,且今后还要常常碰面,那么他们彼此的忠心就会又不同程度的增加,原因在于他们还有机会重复博弈。
试验的结果使爱克斯罗德大为吃惊,因为竞赛的冠军获得者所采取的策略一点都不高深,而是非常简单:一报还一报。中国人把它叫做“以其人之道,还治其人之身”。
说穿了,所谓“一报还一报”的策略,就是胡萝卜加大棒的原则。它坚持永远不首先背叛对方,因此是十分“善意的”。它会在下一轮中对对手的前一次合作给予回报(哪怕以前这个对手曾经背叛过它),从这个意义上来说它是“宽容的”。但它会采取背叛的行动来惩罚对手前一次的背叛,从这个意义上来说它又是“强硬的”。正所谓“人不犯我,我不犯人;人若犯我,我必犯人”。而且,它的策略极为简单,对手程序一望便知其用意何在,从这个意义来说它又是“简单明了的”。
后来,爱克斯罗德邀请了更多的人参加竞赛,结果试验来试验去,每次都是一报还一报者夺魁。
竞赛说明,具备以下特点的人,将总会是赢家:1.善意的;2.宽容的;3.强硬的;4.简单明了的。
回过头来,再说恋爱。
恋爱过程,一般都是重复博弈的过程,因此恋人有无数次的机会做到“以其人之道,还治其人之身。
那么,在这个重复博弈的过程中,谁将是情场上的赢家呢?谁将在博弈中获胜呢?
根据罗伯特·爱克斯罗德的试验,胜利也总是属于那些善意的、宽容的、强硬的、简单明了的恋人们。反之,恶意的、尖刻的、软弱的、复杂的恋人们往往才会败北。
所以,获得幸福爱情的博弈原则应该是:
第一,善意而不是恶意地对待恋人。这个道理很简单了,无须多说。
第二,宽容而不是尖刻地对待恋人。幸福的恋人可能并不是忠贞不二的,当然也肯定不是见异思迁的,他们能够生活得愉快,关键是能够彼此宽容,既宽容对方的缺点,甚至也宽容他偶尔的不忠贞。而尖刻地对待恋人的人,对恋人的偶尔不忠贞总是不肯迁就的人,往往也都不会幸福。
第三,强硬而不是软弱地对待恋人。就是要在我永远爱你的善意的前提下,做到有爱必报,有恨也必报,以眼还眼,以牙还牙,以其人之道,还治其人之身。这其中,当然是要有限度和分寸的。比如对恋人与其他异性的亲热行为,要有极其强烈的敏感与斩钉截铁地回报,当然,每次发脾气都是有限度的,而且还要能宽容对方。
第四,简单明了而不是山环水绕地对待恋人。爱克斯罗德的实验证明,在博弈过程中,过分复杂的策略使得对手难于理解,无所适从,因而难以建立稳定的合作关系。事实上,在一个非零和的环境里,“城府深严”、“兵不厌诈”、“揣着明白装糊涂”往往并非上策。相反,明晰的个性、简练的作风和坦诚的态度倒是制胜的要诀。要让恋人明白你说的是什么,切忌让对方猜来猜去的,造成误会。因为不简单明了地对待恋人最终导致误会而分手的爱情悲剧并不少啊。所以,爱情的手段,还是简单一点好,让恋人一看就明白,免去了很多猜谜的时间。至于剩下的时间嘛,哈哈,你们可以用来对天发誓啊!
本来应该提防恋人才能在恋爱中获胜的简单博弈模型,因为有了不绝于耳的爱情誓言,更因为有了对善意的、宽容的、强硬的、简单明了的原则的把握和利用,人世间才有了很多美丽的爱情和幸福的婚姻。
不过,我想最高的博弈境界,应该是忘掉博弈,忘掉善意的、宽容的、强硬的、简单明了的等诸多博弈原则,而进入浑然忘我的境界。
记得杨过在苦苦等待16年后,因为无法等来龙姑娘而纵身跳入寒潭,最后终于与小龙女团聚之后,深有感触地说了一句话:“可知一个人还是深情的好!”
是的,人,还是深情一些好。
【博奕论的两个经典案例- Jay Chen】
1. 囚徒困境
甲乙是同案犯,被隔离审讯。如果两个都抗拒,因为证据不充分,两人各判2年。如果只有一方坦白,属立功表现,判1年;而另一方则属抗拒从严,判9年。但如果两人都坦白,则各判6年。是你会怎么选?
2. 海盗分金
话说五个海盗抢得100枚金币,他们通过了这样一个关于分配的制度安排: 1、抽签决定各人的号码(1,2,3,4,5);2、由1号提出分配方案,然后5人表决,当且仅当超过半数同意方案被通过,否则他将被扔入大海喂鲨鱼;3、1号死后,由2号提方案,4人表决,当且仅当超过半数同意时案通过,否则2号同样被扔入大海;4、依次类推...... 假定"每个海盗都是很聪明的人,都能很理智的判断得失,从而做出选择",那么"第一个海盗提出怎样的分配方案才能够使自己的收益最大化?" 据说,凡在20分钟答出此题的人有望在美国赚取8万以上的年薪。
我们展开讨论后,得出的答案跟标准答案相差太远太远,感觉自己的思维模式是典型的中国式的"中庸之道",利益最大化一点都没有体现出来,然后不得不佩服博奕论之精湛,感觉接触到了一个全新的理论。回家后,神采奕奕的把题目出给LG,我算到他做不出来的,果然他的答案跟我是一样的。听到标答他全然抗拒,斥责这是赤裸裸的资本主义式的剥削和掠夺,而且生活中不可能有这样的事情,我也恼羞成怒,本来是想分享答案的巧妙,结果成了一场关于道德,思维模式的争论,前提条件是固定下来的,暂且不说其对错或现实与否,大家的答案呢?先看看再说吧!
答案 1。都会选择坦白各判6年。因为若抗拒是2年和9年的结果,若坦白是1年和6年的结果,在不知道对方是否抗拒或坦白的情况下,选择坦白无疑风险最小。
答案2。1号海盗分给3号1枚金币,4号或5号海盗2枚,独得97枚。分配方案可写成(97,0,1,2,0)或(97,0,1,0,2)。
推理过程是这样的:从后向前推,如果1-3号海盗都喂了鲨鱼,只剩4号和5号的话,5号一定投反对票让4号喂鲨鱼,以独吞全部金币。所以,4号惟有支持3号才能保命。3号知道这一点,就会提(100,0,0)的分配方案,对4号、5号一***不拔而将全部金币归为已有,因为他知道4号一无所获但还是会投赞成票,再加上自己一票他的方案即可通过。不过,2号推知到3号的方案,就会提出(98,0,1,1)的方案,即放弃3号,而给予4号和5号各一枚金币。由于该方案对于4号和5号来说比在3号分配时更为有利,他们将支持他而不希望他出局而由3号来分配。这样,2号将拿走98枚金币。不过, 2号的方案会被1号所洞悉,1号并将提出(97 ,0,1,2,0)或(97,0,1,0,2)的方案,即放弃2号,而给3号一枚金币,同时给4号(或5号)2枚金币。由于1号的这一方案对于3号和4号(或5号)来说,相比2号分配时更优,他们将投1号的赞成票,再加上1号自己的票,1号的方案可获通过,97枚金币可轻松落入囊中。这无疑是1号能够获取最大收益的方案了!
网上看到由此引出的一段思考如下:
1、当老大是不容易的,企业家就是要把各方面"摆平"。这里说的企业家包括熊比特说的政治家。
2、任何"分配者"想让自己的方案获得通过的关键是事先考虑清楚 "挑战者"的分配方案是什么,并用最小的代价获取最大收益,拉拢"挑战者"分配方案中最不得意的人们。想一想历朝历代的农民起义,想一想绵延起不断的宫廷斗争,想一想我们这个时代比比皆是的结盟与背叛,想一想企业内部的明争暗斗,想一想办公室脚下使绊的政治,哪一个得胜者不是采用的类似"海盗分金"的办法?为什么革命者总是找穷苦人,因为他们是最失意的人。为什么恐怖分子拉登在沙特阿拉伯没有市场,在阿富汗却大受欢迎,因为阿富汗是全球化的弃儿。为什么企业中的一把手,在搞内部人控制时,经常是抛开二号人物,而与会计和出纳们打得火热,难道不是因为公司里的小人物好收买,而二号人物却总是野心勃勃地想着取而代之......
3、国际交易中的先发优势和后发劣势。1号看起来最有可能喂鲨鱼,但他牢牢地把握住先发优势,结果不但消除了死亡威胁,还收益最大。这不正是全球化过程中先进国家先发优势吗?而5号,看起来最安全,没有死亡的威胁,甚至还能坐收渔人之利。却因不得不看别人脸色行事而只能分得一小杯羹。这难道不是后发劣势的写照?可以预料,如果中国人总是处于5号位置,总是坐等别人制定规则,未来就不见得会比5号好到那里去!
4、海盗的逻辑当然不是真实世界的唯一内幕。"海盗分金"模型是一个非常精致的模型,但它只是一个有益的智力测验,而现实世界要痹烩个模型复杂得多。
5、现实中肯定不会是人人都绝顶聪明兼"绝对理性"。回到"海盗分金"的模型中,只要3号,4号或5号中有一个人偏离了绝对聪明兼绝顶理性的假设,海盗1号保不准就会被扔到海里去了。所以,1号首先要考虑的就是他的海盗兄弟们的聪明和理性究竟靠得住靠不住,而断断不敢自取97颗金币,拼了性命去狂赌。
6、偏好和效用及其替代是另外的一个大问题。现实中人们是如此的复杂,某人的神经末稍微偏离一毫,就可能表现得对金币满不在乎而偏偏喜欢看同伙被扔进海里喂鲨鱼。果真如此,1号自以为得计的方案岂成了自掘坟墓!
7、再就是俗话所说的"人心隔肚皮"。这翻译成经济学语言则是信息不对称。由于信息不对称,谎言和虚假承诺就大有用武之地,而阴谋也会象杂草般疯长,并借机获益。譬如,2号完全可以对3、4、5号大放烟幕弹,假称对于1号所提出任何分配方案,他一定会再多加上一个金币给他们。果真如此,结果又当如何?
8、通常,在现实世界中,人人都有自认的公平标准,因而时常会嘟嚷:"谁动了我的奶酪"?而且,规则是由人制订的,所以,当人一旦意识到这个规则对自己不利的时候,并且知道这对大多数人不利的时候,就会想到修改规则。也就是我们说的"文化可以颠覆制度"。可以料想,一旦1号所提方案和大家所想的不符合的时候,就会有人大闹......当大家都闹将起来的时候,1号能拿着97枚金币毫发不损地、镇定自若地地走出去吗?最大的可能就是,海盗们会要求修改规则,然后重新分配。
9、假如由一次博弈变成重复博弈呢?比如,大家讲清楚下次再得100枚金币时,先由2号海盗来分......然后是3号......这颇有点象美国总统选举,轮流主政。说白了,其实是==制度下的分赃制。可能还会有痹烩更闹得凶的。比如,四人会想:1号居然要独得97枚金币,这还得了。于是,他们立即形成一个反1号的大联盟并制定出新规则:四人平分金币,独将1号扔进大海......这便是阿Q式的革命理想:高举平均主义的旗帜,将富人扔进死亡深渊,睡吴妈的床......
10、制度的重要在这个故事中得到了最突出的体现,这样一个分配制度,很容易诱导人们走向自相残杀,这是在游戏中容易被人忽略的。美国的总统制度中有一条重要的规则是,总统下台后可以建一个图书馆(当然还有其它优厚的条件),而不是"推到海里",这就保证了文明的权力交替,可以想象,如果所有的总统都没有好下场,那总统不仅不愿意下台,而且还会做很多的保护自己的手脚,政治就不可能清明。
11、现实的确是太复杂了。这也说明制度是多么的重要,也说明参与制度的制定是多么重要。
还有很多,不能全说出来了,留给大家曰后去思考。
囚徒困境引发的悲观和合作的突围
自从博弈论被用于人类行为的研究以来,“囚徒困境”这个题目已被无数次地提及,它描述的是这样一个事态:尽管个体之间面临通过合作增进各自利益的机会,但从个体的角度看,合作却不是最优选择,因而当参与博弈的个体都按其最优选择行动时,合作无法达成,潜在的合作增益也就无法实现。
比如,甲有几个金币,有真的有假的,乙有几盒雪茄,有些是用正宗古巴烟叶卷的,有些则用的是劣质廉价烟叶,金币的真假和雪茄的优劣只有甲乙自己知道,当面无法分辨;现在,甲和乙在街上相遇,他们面临一个机会:如果甲用真金币换乙的上等雪茄,各自的利益将获得很大增进,但显然,对于甲,最优选择是用假币支付(无论他将从乙那里得到上等雪茄还是劣质货),对于乙,最优选择是给对方劣质雪茄(无论他将从甲那里得到真币还是假币),这样一来,博弈的结果将是:甲用假币从乙那里换得了劣质雪茄。虽然这一结果可能对双方也略有增益,但潜在的更大交换增益却没有实现。
囚徒困境对于人类社会通过个体间合作而改进的可能性,提示了一个悲观的前景。然而,尽管人类社会不尽完美,我们却看到合作无处不在,自古就有。
面对理论与现实的这一反差,有些人欢呼人性之善——人并非生物学家所指出或经济学家所假设的那样只顾自己的利益,人具有合作和利他的本性;另一些人则声称,人类社会之所以没有陷入囚徒困境所预示的那种充满背叛和欺诈的、相互为敌的状态,是因为存在像国家这样拥有压倒性力量的利维坦,来提供秩序和公正,惩恶扬善,基于合作的改进才得以实现。
科学研究遭遇困境时,便到系统之外去物色甚或制造一块万能膏药,来贴补理论的缺口,这种做法我们已经看得很多了。搞不懂埃及初民如何建造金字塔,就找个外星人,解不透细胞化学机制之精妙,就想出智能设计者,进化过程重建困难,就要去找上帝了。种种这些,我称之为“因无知而称更有知”,或者更简化一下,叫“因惑称知”。
扯远了,回到囚徒困境的问题。研究者很快发现,实际上,囚徒困境所预示的悲观后果仅仅在单次博弈中才表现得那么纯粹,而在多次重复博弈中,个体之间或多或少会达成合作。研究者并且发现,达成合作的几率与如下几个变量密切相关:
1) 博弈者相互识别的能力;
2) 博弈者记忆先前遭遇的能力;
3) 博弈者之间先前遭遇的次数;
4) 博弈者之间先前达成合作的次数;
5) 博弈各方再次遭遇的几率;
6) 博弈者标示自己策略的能力;
7) 博弈者识别对方策略的能力;
8) 博弈者判别对方策略标示之真伪的能力;
9) 博弈者之间交流的可能性;
10) 群体内交换对他人评价的机会;
11)一个评价在群体内传播的预期广度和可能受众与被评价者的遭遇几率;
12) 预期未来博弈次数;
……
请注意,上述研究中并未引入任何除个体利益之外的影响个体选择的因素,也未引入任何集中控制者和外部强加的规则,作为合作达成之前提的这些变量,全都是关于信息表达和识别的。也就是说,博弈论研究预示了,一个由仅受自我利益驱动的众多个体所组成的群体内,有条件的合作策略不仅有存在的可能,而且将占上风。换句话说,以霍布斯的丛林模型作为逻辑起点的博弈论演绎,其结果很可能不是一个霍布斯所想象的丛林社会。
于是,在一个戴着博弈论眼镜观察人类社会演化进程的观察者眼里,自然而然的,信息便占据了核心地位。好奇心将驱使他去探索,人类个体们究竟如何发展了他们的信息表达和识别手段,这些手段又如何促成了个体间的合作。
带着上面这副眼镜,我所看到的是人类所创造的一个复杂精妙的符号系统,这一系统渗透到了人类社会的每个角落,时刻影响着人的行为。尽管它屡屡遭受机会主义行为的破坏,但仍在不断发展,并时刻显示着它的强大力量。在以后的各篇中,我将尝试着描绘出从我的眼镜所看到的一幅幅景象
可是这样的机会有多大呢?答案可能会令我们大吃一惊。
大英帝国彩票
“下一个赢家就是你!”这句响亮的极具蛊惑性的话是大英帝国彩票的广告词。买一张大英帝国彩票的诱惑有多大呢?只要你花上1英镑,就有可能获得2200英镑!
一点小小的投资竟然可能得到天文数字般的奖金,这没办法不让人动心,很多人都会想:也许真如广告所说,下一个赢家就是我呢!因此自从1994年9月开始发行到现在,英国已经有超过90%的成年人购买过这种彩票,并且也真的有数以百计的人成为百万富翁。如今在世界各地都流行着类似的游戏,在我国各省各市也发行了各种福利彩票、体育彩票,各地充满诱惑的广告满天飞,而报纸、电视上关于中大奖的幸运儿的报道也热闹非凡,因此吸引了不计其数的人踊跃购买。很简单,只要花2元的人民币,就可以拥有这么一次尝试的机会,试一下自己的运气。
但一张彩票的中奖机会有多少呢?让我们以大英帝国彩票为例来计算一下。大英帝国彩票的规则是49选6,即在一至49的49个号码中选6个号码。买一张彩票,你只需要选六个号、花一英镑而已。在每一轮,有一个专门的摇奖机随机摇出6个标有数字的小球,如果6个小球的数字都被你选中了,你就获得了头等奖。
可是,当我们计算一下在49个数字中随意组合其中6个数字的方法有多少种时,我们会吓一大跳:从49个数中选6个数的组合有13983816种方法!这就是说,假如你只买了一张彩票,六个号码全对的机会是大约一千四百万分之一,这个数小得已经无法想象,大约相当于澳大利亚的任何一个普通人当上总统的机会,如果你每星期买50张彩票,你赢得一次大奖的时间约为5000年;即使每星期买1000张彩票,也大致需要270才有一次六个号码全对的机会。这几乎是人靠人力不可为的,获奖仅是我们期盼的偶然而又偶然的事件。
那么为什么总有人能成为幸运儿呢?这是因为参与的人数是极其巨大的,人们总是抱着撞大运的心里去参加。孰不知,彩民们就在这样的幻想中为彩票公司贡献了巨额的财富。一般情况下,彩票发行者只拿出回收的全部彩金的45%作为奖金返还,这意味着无论奖金的比例如何分配,无论彩票的销售总量是多少,彩民平均付出一元钱只能赢得0.45元的回报。从这个平均值出发,这个游戏是绝对不划算的。
概率:直觉容易出错
在社会和自然界中,我们可以把事件发生的情况分为三大类:在一定条件下必然发生的事件,叫做必然事件;在一定条件下不可能发生的事件,叫做不可能事件;在一定条件下可能发生也可能不发生的事件,叫做随机事件。在数学上,我们把随机事件产生的可能性称为概率。严格说来,概率就是在同一条件下,发生某种事情可能性的大小。概率在英文中的名称为probability,意为可能性、或然性,因此,概率有时也称为或然率。
彩票是否中奖就是个典型的概率事件,但概率不仅仅出现在类似买彩票这样的赌博或游戏中,在日常生活中,我们时时刻刻都要接触概率事件。比如,天气有可能是晴、阴、下雨或刮风,天气预报其实是一种概率大小的预报;又如,今天某条高速公路公路上有可能发生车祸,也有可能不发生车祸;今天出门坐公交车,车上有可能有小偷,也有可能没有小偷。这些都是无法确定的概率事件。
由于在日常生活中经常碰到概率问题,所以即使人们不懂得如何计算概率,经验和直觉也能帮助他们做出判断。但在某些情况下,如果不利用概率理论经过缜密的分析和精确的计算,人们的结论可能会错得离谱。举一个有趣的小例子:给你一张美女照片,让你猜猜她是模特还是售货员?很多人都会猜前者。实际上,模特的数量比售货员的数量要少得多,所以,从概率上说这种判断是不明智的。
其实,上面所说的彩票问题也反映了人们对概率自以为是的直觉是多么靠不住。人们在购买彩票时总是只看到那些中了大奖的故事,而不愿去考虑中大奖其实是个最典型的小概率事件,其概率低到根本不值得去买。数学家认为,概率低于1/1000,就可以忽略不计了,而大英帝国彩票中头等奖的概率只有一千四百万分之一,即使是选号范围小一些的彩票,中到头等奖的概率一般也要五百万分之一,这样小的概率居然还有那么多人趋之若鹜。有笑话说全世界的数学家都不会去买彩票,因为他们知道,在买彩票的路上被汽车撞死的概率远高于中大奖的概率。
人们在直觉上常犯的概率错误还有飞机失事事件。也许出于对在天上飞的飞机本能的恐惧心理,,也许是媒体对飞机失事的过多渲染,人们对飞机的安全性总是多一份担心。但是,据统计,飞机旅行是目前世界上最安全的交通工具,它绝少发生重大事故,造成多人伤亡的事故率约为三百万分之一,假如你每一天坐一次飞机,这样飞上8200年,你才有可能会不幸遇到一次飞行事故,三百万分之一的事故概率,说明飞机这种交通工具是最安全的,它甚至比走路和骑自行车都要安全。
事实也证明了在目前的交通工具中飞机失事的概率最低。1998年,全世界的航空公司共飞行1800万次喷气机航班,共运送约13亿人,而失事仅10次。而仅美国一个国家,在半年内其公路死亡人数就曾达到21000名,约为40年前有喷气客机以来全世界所有喷气机事故死亡人数的总和。虽然人们在坐飞机时总有些恐惧感,而坐汽车时却非常安心,但从统计概率的角度来讲,最需要防患于未然的,却恰恰是我们信赖的汽车。
随意的估算也不准
不断地抛一枚硬币,当它落到地上时,出现正、反面次数相同的概率是多少?很多人都会以为随着抛硬币次数的增加,正、反面出现次数相同的概率也在递增,但这个想法错了。恰恰相反,其概率随着抛硬币次数的增加在递减。抛2次时出现正反两面各1次的概率是50%,抛6次时出现正反两面各3次的概率是31.25%,抛10次时出现正反两面各5次的概率是24.61%,抛100次时出现正反两面各50次的概率只有大约8%(当然,随着抛地次数的增加,正、反面出现的次数非常接近,就是难以做到完全相同)。这说明,面对一个貌似简单的概率问题时,我们如果随意估算,轻易下结论,可能与实际情况恰好南辕北辙。
我们来看一个经典的生日概率问题。以1年365天计(不考虑闰年因素),你如果肯定在某人群中至少要有两人生日相同,那么需要多少人?大家不难得到结果,366人,人数只要超过365人,必然会有人生日相同。但如果一个班有50人,他们中间有人生日相同的概率是多少?你可能想,大概20%-30%,错,有97%的可能!
它的计算方式是这样的:
a.50个人可能的生日组合是365*365*365……*365(共50个)个;
b.50个人生日都不重复的组合是365*364*363*……*316(共50个)个;
c.50个人生日有重复的概率是1-b/a。
这里,50个人生日全不相同的概率是b/a=0.03,因此50个人生日有重复的概率是1-0.03=0.97,即97%。根据概率公式计算,只要有23人在一起,其中两人生日相同的概率就达到51%!但是,如果换一个角度,要求你遇到的人中至少有一人和你生日相同的概率大于50%,你最少要遇到253人才成。再来看一个常见的抽奖例子。参加抽奖,当然人人都想得奖,这时候该先抽奖还是后抽,才能让中将概率提高呢?
恐怕很多人都会在这个问题上犯糊涂,让我们用科学方法解决这个问题吧。假设有两个酸苹果,一个甜苹果,甲乙丙一次从箱中摸出一个,谁最有机会吃到甜苹果呢?首先,甲的机会是三摸一,所以甲摸到甜苹果的概率是1/3。乙的机会如何呢?甲没有摸到的概率是2/3,然后在这个概率中计算乙摸到的概率:
(2/3)*(1/2)(只剩2个苹果让乙摸)=1/3,所以乙摸到甜苹果的概率是1/3。丙呢?丙只有在甲、乙都没有摸到的情况下才可能摸到甜苹果,所以扣掉甲、乙摸中的概率,就是丙的机会大小了,其概率是1-(1/3)-(1/3)=1/3。
明白了吗?不管先摸也好,后摸也罢,每个人摸到甜苹果的机会其实都是一样的。
从赌博中发展的概率理论
既然一个事件的概率凭感觉随便估计总是容易出错,而概率又与人类生活息息相关,那人们就得严肃对待概率问题了。
概率问题的历史可以追溯到遥远的过去,很早以前,人们就用抽签、抓阄的方法解决彼此间的争端,这可能是概率的最早应用。而真正研究随机现象的概率论出现在15世纪之后,当时的保险业已在欧洲蓬勃发展起来,不过,当时的保险业非常不成熟,只是一种完全靠估计形势而出现的赌博性事业,保险公司要承担很大的不确定性风险,保险业的发展渴望能有指导保险的计算工具的出现。
这一渴望戏剧性地因15世纪末赌博现象的大量出现而得到解决。当时的主要赌博形式有玩纸牌、掷骰子、转铜币等。参加赌博的人,特别是那些专门从事以盈利为生的职业赌徒,鏖战赌场,天长日久就逐渐悟出了一个道理:在少数几次赌博中无法预料到输赢的结果,如果多次进行下去,就可能有所预料,这并不是完全的碰巧。这无意中就给学者们提供了一个比较简单而又非常典型的概率研究模型。
1654年,有一个法国赌徒梅勒遇到了一个难解的问题:梅勒和他的一个朋友每人出30个金币,两人谁先赢满3局谁就得到全部赌注。在游戏进行了一会儿后,梅勒赢了2局,他的朋友赢了1局。这时候,梅勒由于一个紧急事情必须离开,游戏不得不停止。他们该如何分配赌桌上的60个金币的赌注呢?梅勒的朋友认为,既然他接下来赢的机会是梅勒的一半,那么他该拿到梅勒所得的一半,即他拿20个金币,梅勒拿40个金币。然而梅勒争执到:在掷一次骰子,即使他输了,游戏是平局,他最少也能得到全部赌注的一半30个金币;但如果他赢了,就可拿走全部的60个金币。在下一次掷骰子之前,他实际上已经拥有了30个金币,他还有50%的机会赢得另外30个金币,所以,他应分得45个金币。
赌本究竟如何分配才合理呢?后来梅勒把这个问题告诉了当时法国著名的数学家帕斯卡,这居然也难住了帕斯卡,因为当时并没有相关知识来解决此类问题,而且两人说的似乎都有道理。帕斯卡又写信告诉了另一个著名的数学家费马,于是在这两位伟大的法国数学家之间开始了具有划时代意义的通信,在通信中,他们最终正确地解决了这个问题。他们设想:如果继续赌下去,梅勒(设为甲)和他朋友(设为乙)最终获胜的机会如何呢?他们两只多再赌两局即可分出胜负,这两局有4种可能的结果:甲甲、甲乙、乙甲、乙乙。前3种情况都是甲最后获胜,只有最后一种情况才是乙取胜,所以赌注应按3:1的比例分配,即甲得45个金币,乙得15个。虽然梅勒的计算方式不一样,但他的分配方法是对的。
三年后,也就是1657年,荷兰著名的天文、物理兼数学家惠更斯把这一问题置于更复杂的情形下,试图总结出更一般的规律,结果写成了《论掷骰子游戏中的计算》一书,这就是最早的概率论著作。正是他们把这一类问题提高到了理论的高度,并总结出了其中的一般规律。同时,他们的研究还吸引了许多学者,由此把赌博的数理讨论推向了一个新的台阶,逐渐建立起一些重要概念及运算法则,从而使这类研究从对机会性游戏的分析发展上升为一个新的数学分支。
由赌徒的问题引起,概率逐渐演变成一门严谨的科学。
概率计算:与庄家博弈
概率理论从赌博中发展而来,又反过来成为赌场老板赚钱的强大工具。进入赌场的赌徒总是相信自己运气十足,孰不知赌场庄家早已利用概率规律为他们设下了陷阱。例如,很多赌场里的老虎机上都顶着跑车,下面写着告示,告诉赌客已经有多少人玩了游戏,车还没有送出,暗示现在轮到你的机会大增。但这其实是赌场利用概率规律为赌徒设下的一个诱惑陷阱。该旅里有一个重要的规律就是随机事件的独立性,在随机事件中下次事件发生与否,与上次事件是没有关系的。设想一下,前面10个人抛硬币,没有一个人抛出了正面,现在轮到了你,难道你抛出正面的可能性就大于其余的人?因此,只要得大奖的规则没有变化,
每人是否幸运,和前面的人是否中奖毫无关系。
但人们通常都对这个规律无知无觉,很多情况下,人们因为前面已经有了大量的未中奖人群而去买彩票或参与到累计回报的游戏,实际上,并不因为前面没有人中奖你就多了中奖的机会。
庄家在参与赌博游戏时都已经设计好了一个有利于自己的概率,而很多玩家却浑然不觉。以10年前盛行于北京的摸球中奖事件为例:布袋中有六个白球与六个红球,以随便摸出6个球来确定是否中奖。奖励规则为:
a)摸出6红或6白可得3元钱;
b)摸出5红1白活5白1红,可得2元;
c)摸出4红2白或者4白2红,可得1元钱;但若摸出的是3红3白,则玩家需付3元。
表面上看,共有7种情况,竟有6种情况可获奖,而只有1种情况要“挨罚”,非常合算。但实际上赢钱的人很少,而如果连摸5次以上,几乎是必赔无疑。
这里,庄家使用了什么障眼法呢?其实,根据概率的知识,这个玩法的答案非常简单。从12个球中摸出任意的6个球,共有924种情况,其中出现6红或6白的情况,都只有1种,概率为千分之一强;而出现5红1白或5白1红的机会相等,分别为36次,其概率总共不到8%;出现4红2白或4白2红的机会相等,分别为225次,两者总概率为48%;而摸到3红3白的次数为400次,概率大致为43%。按照上述概率,当玩家摸四次球时,最大的可能为两次3红3白(共赔6元),另两次为4红2白和4白2红(获得2元),这样玩家一般都会损失4元钱,平均一次损失1元。当玩的次数增加,玩家平均损失的钱数几乎总保持在此数额。由于庄家利用了概率论,成了不变的赢家。
然而,当此类游戏的表面现象复杂时,有时“入套”的就不只是玩家,而有可能是庄家吃黄连,有苦说不出,因为玩家掌握了更精妙的概率理论。在历史上曾发生过数学教授打败赌场无敌手的有趣故事。在1959年,当年轻的麻省理工学院数学教授爱德华·索普陪妻子去赌城度周末时,出于职业上的爱好(数学教授也许对有关数学的东西统统感兴趣),他也去一试手气。他拿出10美元来玩21点的游戏。起初索普只想不要输得太快。但后来,索普聪明的大脑开始理性运转,结果大为改观。
21点的游戏规则非常简单:把一副牌放在桌上,然后轮流抓牌。A代表1点或11点2-10代表2-10点,花牌J、Q、K、大王、小王代表10点,在一局里,你手上牌的点数相加越接近21点就越大,玩家的点数只要超过庄家点数就算赢,但是超过21点就爆牌作负。21点玩法特殊性在于,前一局都对后来的输赢有影响,因为用过的牌会出局,剩下的牌越来越少,这意味着剩下的牌可以猜到,因此高手都会记住以前的牌,这就是21点的诀窍所在。因为规则规定玩家可以自由决定自己所要的牌数,而要求庄家手中的牌不能少于17点,
如果台上大牌(点数大的牌)数量较多,那么庄家就很可能爆牌作负。索普认识到如能猜出没有出局的牌的特征,那么他就可算出庄家爆牌的可能性,进而确定有利于自己的赔率。
获胜的关键在于,要准确记忆每张牌的点数,要有过目不忘的本领和极快的心算。这几乎是不可能做到的,但索普发明了一种用近似值大体记忆的方法,首先将这些牌分为3组,然后赋予每一组相同的点数:第一组2-6的赋值为+1,因为这些牌从台面上被清出去是有利于玩家的,第二组7-9赋值为0,这几张牌处于中性;第三组10、大王小王和花牌,赋值为-1,因为它们被清出台面不利于玩家,如果台面上这些牌越少,那么庄家爆牌的概率也就越小,。现在玩21点时,我们只需记忆这些牌是+1还是0或者-1就可以了,比方说大王出局,那么它是10点,我们就认为它是-1,照此类推,当出局的牌的总结果为正时,那么说明剩下的牌中大点数较多,玩家胜算的概率就大一些;如果结果为负,庄家的胜算就大一些;如果结果为零,双方的胜负概率大体相当。索普里用这套简单易行的策略,在赌城中狂赌三天三夜,最后1万美元变成了近2.1万美元,利用概率论打破了庄家不可战胜的神话。后来他出版的书《打败庄家:21点游戏赢的策略》成为玩家手中的赌博圣经。
相同的概率,不同的结论
有时,面对同一个概率事件,随着问题的着眼点不同,我们得出的结论可能截然相反。这一点会使一般人感到迷惑不解,我们在这里打一个通俗的比喻:某人是嫌疑犯,也找到了一些他的犯罪证据,但不是决定性的,若我们要求“只有找到了更重要的犯罪证据才能判他有罪”,则他将被判无罪;反之,若要求“只有找到了证明他没有犯罪的重要证据才能判他无罪”,则他将被判有罪,在这里,着眼点的不同决定了不同的判罚。
这方面的著名事例是辛普森杀妻案。
1994年6月12日深夜,美国洛杉矶西部一个豪华住宅区里,一只小狗在不停地狂吠,引起了邻居的注意。
当人们随着狗吠声来到一住宅门前时,赫然发现两具血淋淋的尸体!警察接到报警后迅速赶到现场,发现两名死者是美国黑人橄榄球明星辛普森的妻子和一个餐馆服务员。
警方在经过大量艰苦细致的调查后,搜集到了大量证据都表明辛普森有重大杀人嫌疑:他的汽车上染有死者血迹,车道上也发现血迹,案发现场还有染血手套和其他证据;还有证人作证说在辛普森妻子死亡的时间段内看到了辛普森就在豪华住宅附近;历年报警记录还显示辛普森曾多次暴力虐妻。这些证据都对辛普森极为不利,检察官据此向法院控告辛普森犯有一级谋杀罪。遗憾的是,控方所提供的证据中有一小部分因不符合法定程序而不被法庭采信。即便如此,在这起案件中,辛普森杀人的概率也有95%以上。然而,
最后的审判结果却让全世界大吃一惊:辛普森被无罪释放!
原来,美国的刑事法律是建立在无罪推定的基础上的,尤其是对于杀人案这样重大的案件,要最后给被告定罪,控方所提供的证据要近乎100%令人信服才行,稍有疑问就不得被判有罪。95%以上的概率不足以使辛普森被判有罪。
颇具戏剧性的是,当辛普森前妻的娘家在向法院提起民事诉讼时,法院却判决辛普森输,赔偿原告3350万美元。之所以有如此结果,是因为刑事审判与民事审判的证据采用规则有差别,在民事诉讼中,原告提供的证据只要比被告的有说服力就可以赢。用数学概率表示,刑事诉讼中控方需要近乎100%的证明,民事诉讼中原告只要证明有51%以上的可能性即可。在这起案件中,95%以上的概率足以使辛普森赔得倾家荡产。
【概率与我们的生活】
你能很好地判断事情会发生,或不会发生吗?专家也许会告诉你
恼人的电梯
三百多年前,有个赌徒问法国数学家巴斯加,掷骰子时,怎样计算落在某一面上的可能性?巴斯加对这个问题的回答就是“概率论”的开端。现在,“概率论”已经是数学的一个发展最快的分支。
在日常生活中,我们一般都是根据直觉或常识对可能性作出粗略判断。在大多数情况下,这是很可靠的。然而,在许多问题上,事实上的可能性与人们的想象却又相差很远,有的甚至会使人感到吃惊。
物理学家乔治·伽侔曾经在一座七层的楼里工作,经常需要从他二楼的办公室到六楼的一间办公室去。也许有点奇怪,在等电梯的时候,他希望电梯正在往上来,而实际上电梯几乎总是在由上往下走。“他们也许在楼顶制造电梯,一部一部往下放,都放到地下室里储藏起来。”他嘟哝道。然而每当他要从六楼往下去的时候,停在他面前的电梯却又总是上行的。
其实,对这种现象的解释很简单:如果你在低层等电梯,大多数时候电梯总在你上面,因此停在你那一层楼时它是下行的;相反,如果你在接近顶楼的高层等电梯,那么,电梯往往又在你下面,所以停在你面前时它是上行的。但是,人们往往凭直觉想象,认为不管在哪一层楼,开来的电梯是上行还是下行的可能性应该差不多。
生日的难题
概率计算最困难的问题之一,据信就是数学家们所谓的“生日难题”。假设你参加一个有23个人出席的聚会,有多大可能性其中有两个人是同月同日出生的?也许,凭直觉你会感到,这种可能性很小。其实上,在23个人中间,能有一对生日相同和这些人生日全部不同这两种可能性是基本一样的。
人越多,生日相同的可能性就增长得越快。如果是30个人,这种可能性就大于十分之七。如果是50个人,可能性就大于百分之九十七!也许以后在有23个或更多人的场合,你自己也会去试一试。顺举一例:在美国总统中,就有两位生日是完全相同的:詹姆斯·波尔克和沃伦·哈丁都出生于11月2日。
--流言为什么会不胫而走
还有一种现象使直觉概率判断者大为震惊,那就是“小世界问题”。这种事情并不罕见:你遇到一个来自远方的陌生人,通过交谈,竟发现你们有一个共同认识的朋友。也许,你们之中有一个会吃惊地叫起来:“这个‘世界’太小了!”
确实如此。马萨诸塞工学院的社会学家通过研究发现,在美国,平均每个人直接认识500个人。当然,每个人又都是许多不同的“熟人链”中的一个环节。他们进一步计算,结果随意挑出两个美国人来,例如说史密斯和布朗吧,那么他们俩相识的可能性只有二十万分之一。但是,史密斯认识某人,某人又认识另一个人,而另一个人认识布朗,这种可能性却高达一半以上。
心理学家斯坦利·米尔格莱姆曾经研究过“小世界问题”。他首先确定一个“目标者”一个在马萨诸塞州剑桥市正在学习当牧师的年轻人的妻子。然后,在堪萨斯州的维契托市又随便找了一组人作为“出发者”,给他们每个人一份文件,叫他们寄给他们的一个最有可能认识那个“目标者”的熟人。接到文件的熟人依同样办法再把它寄给自己的熟人,使这条“链子”有希望地接续下去,直至连接到“目标者”。叫米尔格莱姆吃惊的是,仅仅过了四天,一个男人就把文件送给了“目标者”,说:“艾丽斯,这是给你的。”这个过程是这样的:在堪萨斯的一个农人(“出发者”),首先把文件给了一个牧师,牧师把它寄给了他在剑桥的一个牧师朋友,这个人就把文件交给了艾丽斯。从“出发者”到“目标者”,这条“链子”只有两个“中间人”!
在这次试验中,各条“链子”的“中间人”数最少的是两个,最多的是十个,平均数是五个。然而,如果事先叫人估计一下,大部分人猜想需要一百个。不难想象,这样的“熟人网”便能很好地解释,为什么一些流言、有意思的新笑话会那么迅速地传遍全国。
直觉经常在犯错误
要求出某种结果的概率,就必须弄清有多少种其他各不相同的、同样可能出现的结果。你抛起一枚硬币,落地只会有两种可能:面朝上或面朝下。所以,两种可能的概率都是二分之一。但是换一个问题,你的直觉就不那么可靠了。
举个例子:如果一个家庭有三个孩子,那么这三个孩子都是一种性别的可能性有多少?有人可能这样推理:“至少有两个肯定是一种性别;那么,第三个或者与那两个相同,或者不同;所以,三个孩子同一性别的可能性是二分之一。”但是我们考察全部可能的结果,就会发现有八种不同的组合:男男男,男男女,男女男,男女女,女男男,女男女,女女男,女女女。所以,三个孩子同一性别的可能性应该是八分之二,即只有四分之一。
假设有一对夫妇希望要四个子女,是三男一女或三女一男的可能性大呢,还是两男两女的可能性大?
大部分人猜想是两男两女可能性大。但我们列出所有可能的结果(有16种)就可看到:有六种组合是两男两女,两种组合是四男四女,但有八种组合却是三一开的。所以,有一半的可能性是三男一女或三女一男,要高于两男两女。直觉又使我们错了!
出错的另一个原因是,人们在估计可能性的时候,往往假定一定的过程前后是有联系的。其实不然。
许多人设想,如果抛硬币连续几次都是面朝上落地,那么下一次很可能就是背朝上落地。其实,不论已经有多少次面朝上,下一次面朝上的可能性仍然有二分之一。
【你能中大奖吗?——概率与我们的生活】
统计开辟概率新天地
早期人们对概率的研究,都局限于我们日常接触到的有限事件的组合,例如玩牌、赌博中的计算问题,彩票的中奖问题,体育比赛时的抽签问题,等等,这些都是古典概率问题。古典概率只能处理诸如赌博中有限事务的组合,有非常大的局限性。而自然与社会中有许多事件是非常复杂的,如人口统计,男女出生统计、消费统计、各种民意调查等等,无法用简单的古典概率穷尽。经过几代数学家的努力,大约用了200年的时间,概率论发生了质的飞跃,具备了与统计结合的条件,出现了统计概率。
19世纪初,由于生产力的迅猛发展,统计事业开始走向昌盛。比利时学者A·凯特勒率先把统计方法从自然科学领域推广到社会科学领域,从而为人们认识社会发展规律的客观性打开了一扇窗口。
凯特勒认为,规律躲避着我们的理智,因为我们观察到的只是单个人的行为、个体特征我们无法记录下它们。因此,需要一种崭新的方法来反映社会的整体风貌和趋向。他把统计理论和概率理论结合起来,开创了统计概率应用的先河。
凯特勒仔细研究了当时法国,比利时和英国的司法刑事机关报的汇编,惊讶地发现,这些国家每年犯罪的次数大体不变,不仅如此,各种类型的犯罪也有惊人的重复性。凯特勒本人都为这些惊人的发现所震动,他感叹道:“这是人类多么可悲的性质啊!监狱、铁链、和断头台的命运对人类来说就像国家的收入一样,可以以某种概率被事先决定。我们甚至能预先计算出来,下一年会有多少人将用和自己一样的血弄脏自己的手,有多少人将是伪造者,多少人是投毒者,这一切就像能够确定出生与死亡的数量一样。”凯特勒还分析了人的“自由意志”的其他表现,如结婚、自杀等,也得到同样的结果。在我们以为完全是由个人的自由意志决定的地方,仍然有客观规律在起作用,这真是冥冥之中无法逃脱的宿命。
凯特勒的报告引起了当时社会的轰动。从此,统计走出了原来的杂乱无章的状况。把概率应用到统计中凯特勒是第一人,科学史上将凯特勒开创性的工作看作是现代统计学的起点,凯特勒也被誉为“现代统计学之父”。从那以后,统计概率在社会中的应用得到了蓬勃发展。这些调查使得人类首次能够从数学的角度审视自身行为,并把它们置于概率论模型之下进行研究。
用概率来统计的社会
统计概率对人们的观念的影响是深远的,自从十九世纪二三十年代凯特勒开创了统计概率以来,人们对统计数据规律性的信任超过了以往的任何一个时代。国家以各种报表来了解工业、农业、国防、人口、消费、犯罪等方面的资料。制定银行利息的高低需要消费指数和通货膨胀率,如果通货膨胀率高,消费指数低,银行就会考虑提高利率,反之亦然。国家举行重大活动需要了解几百年甚至上千年的天气资料,以避免遭遇恶劣天气的影响。例如,1990年北京亚运会的举办时间为9月22日至10月7日,就是因为根据统计资料显示,北京这期间遭遇恶劣天气的概率非常低。
今天,统计概率已经渗透到社会科学、自然科学的方方面面,特别是计算机的广泛应用,使社会统计工作得以全面展开。我们耳熟能详的食品检测报告、人口统计、犯罪统计、国民生产总值的统计,国家和公司为了了解民意所进行的民意调查,市场调查,无一不涉及统计概率。同时,概率统计已经变成现代人理念与信念体系中的一部分。比如,对于美国大选,可以通过民意调查,用概率统计的方式计算某个候选人的受欢迎程度。而反过来,这种数据又在一定程度上左右人们的好恶,只是民意倾向变化,而影响到实际的选举过程。这说明概率统计的观念也影响到每一个人的行为和生活方式。
总之,从概率的思想走出机会性(博彩)游戏的范围,到应用的不断深化,这一过程中人类的思想观念发生了巨大的转变,这就是概率带来的革命。
概率不仅出现在人类社会生活中,在大自然的精心安排之下,生命的繁殖、进化也莫不服从于概率论的神奇安排。早在1843年,捷克修道士孟德尔首先为世人揭示了大自然的奥秘。醉心于自然科学的孟德尔,在闲暇研究植物的遗传规律,他选择了豌豆作为实验材料。豌豆是一种严格自花传粉的植物,它的雄蕊被花瓣包围,将外来的花粉拒之门外;同时,具有一些如高茎对矮茎、圆形对皱形、黄子叶对绿子叶、灰种皮对白种皮等具有明显差异的性状。孟德尔发现,当把不同品种的豌豆的这些性状遗传到下一代时,总是遵循着大约3:1的统计概率:高茎德与矮茎的植株比例为2.84:1;圆形的与皱形的植株比例为2.96:1;黄子叶与绿子叶的植株比例为3.01:1;灰种皮的与白种皮的植株比例为3.15:1。
现在,人们在教科书中称这个奇妙的比例为孟德尔第一定律,这个比例产生的原因是由于两种遗传基因在进入下一代的杂种细胞时,彼此分离,互不干扰,最后在生物传粉过程中随机结合,所以这个规律又称“分离定律”。后来孟德尔经过艰苦的探索又发现了两对性状不同的植株进行遗传时,不同对的遗传基因自由组合,而且机会均等。这就是孟德尔第二定律,也称“自由组合定律”。孟德尔发现的分离规律和自由组合规律实质上就是概率统计规律在遗传过程中的体现。
体现概率规律的世界
科学家们所揭示的规律在这个世界上处处存在,我们的世界正是一个充满随机事件又遵循着概率规律的世界。如果我们生活在一个完全必然的世界里,每个人什么时候出生、什么时候幸运、什么时候有难、什么时候死亡都早已“命中注定”,每个人的长相、性格、智力都早已被决定,几千年、几万年、几亿年后宇宙会是个什么样子也早已成定论,所有的事物从一开始就沿着确定的轨道向前发展,所有的结果都被我们预见到,这样的世界是多么的枯燥乏味啊!而一个完全不能预测的世界却是一个可怕、令人发疯的世界。
椅子从不会安静地待在那儿,一会儿在这儿,一会儿在那儿,一会儿又不知道跑到什么地方去;苹果熟了一会儿落到地上、一会儿飞到天上,甚至一会儿是苹果、一会儿不是苹果;生命会无缘无故死亡,也许根本就不会有生命。任何东西一会儿这样,一会儿那样,完全无章可寻,整个世界就会乱得一团糟。
我们要庆幸不是生活在那样完全必然或完全偶然的世界里,而是生活在这样一个随机而又规律的世界里,这才是一个井然有序而又丰富多彩的世界。扔一枚硬币,这次仍完应该和下一次毫无关系,硬币正面或者反面的分布似乎是一种无规则分布,可是当我们扔的次数足够多时,正面和反面出现的次数竟趋于一致,遵守着明确的统计规律。概率无规律随机变化的外表下,有着内在的秩序和规则。
人类在对机会性游戏的研究思考中创立和发展了概率理论,并使之成为人类的根本知识之一,今天,概率理论已融入人类日常生活。正如法国大数学家拉普拉斯所说:“这门起源于靠运气取胜的游戏的科学,竟然成了人类知识最重要的一部分。”
人类爱赌博,不仅仅是一种天性,更是在一个充满概率的世界面前的必然选择,每个人在成长的道路上都面临着无数的机遇和风险,这需要我们理解概率规律,并适时挑战概率规律,才能做生活的强者。
【爱情博奕论】
一对恋人好比一对囚徒,囚于情丝编织的牢笼,并在牢笼里上演爱情的博弈,谁输谁赢,往往靠的不是情深爱笃,而是博弈手段的高低优劣。
恋人,既是你的合作伙伴,也是你的对手,甚至敌人。在恋爱的这场不太好玩的“游戏”中,谁能熟练地驾御游戏或博弈规则,谁就是爱情的赢家。所以,要想成为赢家,就要学会与伙伴一样的恋人合作,还要学会与敌人一样的恋人周旋。面对对手围追堵截的爱情围剿,要学会闪转腾挪的诸多反围剿的手段。实在不行了,再三十六计走为上:对不起,不陪你“玩”了!
几乎所有的博弈论书籍,都从经典的囚徒困境说起。虽然那是说一对纵火犯的,但其原理也照样适用于“纵爱犯”。爱,本来也就是一场大火嘛。
还是让我们看看纵火的囚徒吧。在此,不能不引述一下“囚徒困境”的公案,尽管众所周知。需要说明,公案的版本不一样,但道理是一样的。
一场火灾之后,两个嫌疑人A和B被囚禁,分别关在两个独立的不能互通信息的房间里。如果两人都承认,则各被扣留8年;如果一人承认另一人不承认,承认的放走,并且得到奖赏,不承认的被扣留10年,并且被罚款;如果两人都不承认则因证据不足各被扣留1年。
我们看到,假定A选择承认的话,B最好是选择承认,否则就要被严重处罚;假定A选择否认的话,B最好还是选择承认,因为这样不仅可以被放走,而且还可以得到奖金。就是说,不管A什么态度,B的最佳选择都是承认。反过来,同样地,不管B什么态度,A的最佳选择也是承认。结果,两个人都会选择承认。
这就是有名的“囚徒困境”,所反映的问题是个人理性和集体理性的矛盾。理性告诉被实验者,谁选择抵赖,谁就可能会大倒其霉,而对方却逍遥自在,受到奖励。所以,这就出现了一个悖论,有时对同伙的忠诚,往往会把自己送上绝境,除非双方都能100%的忠诚,但,这是不可能的,谁能在巨大的利益诱惑面前永不动心呢?
恋爱中人,也类似于走入了囚徒困境。如果双方都不变心,那是最好的结局,在天愿为比翼鸟,在地愿为连理枝嘛;如果都变了心,效果也不坏,你走你的阳关道,我过我的独木桥嘛;如果一方变了心,另外找到了更好的情侣,一方却还傻乎乎地忠贞不二,那么,另觅新欢的一方是最幸福的,比两人都不变心的结果还幸福,因为他找见了更好的情人,而被抛弃的一方是最不幸的,比两人都变心的结果更不幸,因为他承担的压力既来自于对方的太幸福,也来自于自己的太不幸福。
按照囚徒困境的分析结论,恋人最得意的选择是另觅新欢,最天真的选择是天荒地老,最理性的选择是分道扬镳,最糟糕的选择是被另有新欢的对方无情抛弃。问题是,最得意的结局过于缺德,最天真的结局过于虚幻,最理性的结局过于残酷,最糟糕的结局又(让一方)过于心痛。
当然,囚徒困境模型给恋人最重要的提醒,是在应对策略上,最好要严格防范对方,就像一个囚徒防范另一个囚徒一样。
但是,反关现实中的恋人,大都希望能够天荒地老,没有谁愿意回头是岸,甚至被对方抛弃了还不死心。
为什么会这样呢?
因为纵爱的恋人与纵火的囚徒,其博弈状况有一个重大的不同。囚徒被警方抓住之后,是隔离审查,因此无法订立功守同盟,即便能够订立,谁又能保证自己或对方永远不毁约呢?
注意!这是囚徒困境模型的一个非常重要的条件,它往往被一些经济学家忽视了。
与囚徒所处的这个重要条件相反,恋人被爱神抓住以后,一般并不是隔离“审查”的,而是整天泡在一起。泡在一起做什么?除了发誓,还是发誓!
人生发誓最多的时期,大概就是恋爱期。发什么誓呢?无非是什么非你不娶非你不嫁你是风儿我是沙啊的一类誓言罢了,目的只有一个,就是让对方相信自己能够天荒地老而此情不渝。他们希望彼此忠诚,从而换来一个好的博弈结果。
而且,为了防止对方变心,总要设法让对方相信,你遇到我是你三生有幸,而我遇到你也是万世不毁。
可是,世间没有什么誓约是永恒的。很多爱情的悲剧,往往都从背弃誓约开始,而天下又似乎没有没有誓约的爱情。
这是整天泡在一起的恋人的博弈情形。而对于天各一方的恋人来说,彼此就像被隔离审查的囚徒一样,只不过不是被关在两间牢房,而是山高水阔知何处。按照博弈原则,他们除了违背誓约外,没有更好的选择。他们要想在恋爱中成为赢家,最好是不遵守爱的诺言,如此才能走出“囚徒困境”。
然而,这个结论在实际上是有问题的。因为,生活中恋爱成功的人并不少见,厮守一生一世的人也不少见,不能说他们都是勉强的。事实上,他们有的还的确生活得很幸福。
我相信,多数情侣的爱情生活过得还是可以的。那么,究竟是什么机制让他们能够信守誓约呢?
我一直在思考这个问题,但顺着博弈论的思路走下去,每次得出的答案似乎总与道德伦理有千丝万缕的联系。后来,偶尔看到一个试验,我猛然感到峰回路转林断山明了。
原来,存在于现实中的幸福的爱情生活,也可以从博弈论中找到答案。这应该感谢美国密西根大学的罗伯特·爱克斯罗德。他组织过一场计算机竞赛的试验。竞赛的思路非常简单:任何想参加这个计算机竞赛的人都扮演“囚徒困境”案例中一个囚犯的角色,他们开始玩“囚徒困境”的游戏,每个人都要在合作与背叛之间做出选择。关键问题在于,他们不只玩一遍这个游戏,而是一遍一遍地玩上200次,这就是所谓的“重复的囚徒困境”,于是这就更逼真地反映了日常人际关系。这又是一个值得注意的条件,最简单的囚徒困境模型说的是一次性博弈,正是这一点加剧了囚徒做处坦白的决心。这正像不相识的一对男女,偶尔在旅游中相遇,接着在宾馆里春梦一场,第二天就各自扬长而去,谁也不会忠于谁的,彼此也不会为对方今后的不忠实而又任何不快,原因就在于这是一次性的博弈(一次性的“博弈”和一次“性的博弈)。可是,如果男女双方互相认识,且今后还要常常碰面,那么他们彼此的忠心就会又不同程度的增加,原因在于他们还有机会重复博弈。
试验的结果使爱克斯罗德大为吃惊,因为竞赛的冠军获得者所采取的策略一点都不高深,而是非常简单:一报还一报。中国人把它叫做“以其人之道,还治其人之身”。
说穿了,所谓“一报还一报”的策略,就是胡萝卜加大棒的原则。它坚持永远不首先背叛对方,因此是十分“善意的”。它会在下一轮中对对手的前一次合作给予回报(哪怕以前这个对手曾经背叛过它),从这个意义上来说它是“宽容的”。但它会采取背叛的行动来惩罚对手前一次的背叛,从这个意义上来说它又是“强硬的”。正所谓“人不犯我,我不犯人;人若犯我,我必犯人”。而且,它的策略极为简单,对手程序一望便知其用意何在,从这个意义来说它又是“简单明了的”。
后来,爱克斯罗德邀请了更多的人参加竞赛,结果试验来试验去,每次都是一报还一报者夺魁。
竞赛说明,具备以下特点的人,将总会是赢家:1.善意的;2.宽容的;3.强硬的;4.简单明了的。
回过头来,再说恋爱。
恋爱过程,一般都是重复博弈的过程,因此恋人有无数次的机会做到“以其人之道,还治其人之身。
那么,在这个重复博弈的过程中,谁将是情场上的赢家呢?谁将在博弈中获胜呢?
根据罗伯特·爱克斯罗德的试验,胜利也总是属于那些善意的、宽容的、强硬的、简单明了的恋人们。反之,恶意的、尖刻的、软弱的、复杂的恋人们往往才会败北。
所以,获得幸福爱情的博弈原则应该是:
第一,善意而不是恶意地对待恋人。这个道理很简单了,无须多说。
第二,宽容而不是尖刻地对待恋人。幸福的恋人可能并不是忠贞不二的,当然也肯定不是见异思迁的,他们能够生活得愉快,关键是能够彼此宽容,既宽容对方的缺点,甚至也宽容他偶尔的不忠贞。而尖刻地对待恋人的人,对恋人的偶尔不忠贞总是不肯迁就的人,往往也都不会幸福。
第三,强硬而不是软弱地对待恋人。就是要在我永远爱你的善意的前提下,做到有爱必报,有恨也必报,以眼还眼,以牙还牙,以其人之道,还治其人之身。这其中,当然是要有限度和分寸的。比如对恋人与其他异性的亲热行为,要有极其强烈的敏感与斩钉截铁地回报,当然,每次发脾气都是有限度的,而且还要能宽容对方。
第四,简单明了而不是山环水绕地对待恋人。爱克斯罗德的实验证明,在博弈过程中,过分复杂的策略使得对手难于理解,无所适从,因而难以建立稳定的合作关系。事实上,在一个非零和的环境里,“城府深严”、“兵不厌诈”、“揣着明白装糊涂”往往并非上策。相反,明晰的个性、简练的作风和坦诚的态度倒是制胜的要诀。要让恋人明白你说的是什么,切忌让对方猜来猜去的,造成误会。因为不简单明了地对待恋人最终导致误会而分手的爱情悲剧并不少啊。所以,爱情的手段,还是简单一点好,让恋人一看就明白,免去了很多猜谜的时间。至于剩下的时间嘛,哈哈,你们可以用来对天发誓啊!
本来应该提防恋人才能在恋爱中获胜的简单博弈模型,因为有了不绝于耳的爱情誓言,更因为有了对善意的、宽容的、强硬的、简单明了的原则的把握和利用,人世间才有了很多美丽的爱情和幸福的婚姻。
不过,我想最高的博弈境界,应该是忘掉博弈,忘掉善意的、宽容的、强硬的、简单明了的等诸多博弈原则,而进入浑然忘我的境界。
记得杨过在苦苦等待16年后,因为无法等来龙姑娘而纵身跳入寒潭,最后终于与小龙女团聚之后,深有感触地说了一句话:“可知一个人还是深情的好!”
是的,人,还是深情一些好。
【博奕论的两个经典案例- Jay Chen】
1. 囚徒困境
甲乙是同案犯,被隔离审讯。如果两个都抗拒,因为证据不充分,两人各判2年。如果只有一方坦白,属立功表现,判1年;而另一方则属抗拒从严,判9年。但如果两人都坦白,则各判6年。是你会怎么选?
2. 海盗分金
话说五个海盗抢得100枚金币,他们通过了这样一个关于分配的制度安排: 1、抽签决定各人的号码(1,2,3,4,5);2、由1号提出分配方案,然后5人表决,当且仅当超过半数同意方案被通过,否则他将被扔入大海喂鲨鱼;3、1号死后,由2号提方案,4人表决,当且仅当超过半数同意时案通过,否则2号同样被扔入大海;4、依次类推...... 假定"每个海盗都是很聪明的人,都能很理智的判断得失,从而做出选择",那么"第一个海盗提出怎样的分配方案才能够使自己的收益最大化?" 据说,凡在20分钟答出此题的人有望在美国赚取8万以上的年薪。
我们展开讨论后,得出的答案跟标准答案相差太远太远,感觉自己的思维模式是典型的中国式的"中庸之道",利益最大化一点都没有体现出来,然后不得不佩服博奕论之精湛,感觉接触到了一个全新的理论。回家后,神采奕奕的把题目出给LG,我算到他做不出来的,果然他的答案跟我是一样的。听到标答他全然抗拒,斥责这是赤裸裸的资本主义式的剥削和掠夺,而且生活中不可能有这样的事情,我也恼羞成怒,本来是想分享答案的巧妙,结果成了一场关于道德,思维模式的争论,前提条件是固定下来的,暂且不说其对错或现实与否,大家的答案呢?先看看再说吧!
答案 1。都会选择坦白各判6年。因为若抗拒是2年和9年的结果,若坦白是1年和6年的结果,在不知道对方是否抗拒或坦白的情况下,选择坦白无疑风险最小。
答案2。1号海盗分给3号1枚金币,4号或5号海盗2枚,独得97枚。分配方案可写成(97,0,1,2,0)或(97,0,1,0,2)。
推理过程是这样的:从后向前推,如果1-3号海盗都喂了鲨鱼,只剩4号和5号的话,5号一定投反对票让4号喂鲨鱼,以独吞全部金币。所以,4号惟有支持3号才能保命。3号知道这一点,就会提(100,0,0)的分配方案,对4号、5号一***不拔而将全部金币归为已有,因为他知道4号一无所获但还是会投赞成票,再加上自己一票他的方案即可通过。不过,2号推知到3号的方案,就会提出(98,0,1,1)的方案,即放弃3号,而给予4号和5号各一枚金币。由于该方案对于4号和5号来说比在3号分配时更为有利,他们将支持他而不希望他出局而由3号来分配。这样,2号将拿走98枚金币。不过, 2号的方案会被1号所洞悉,1号并将提出(97 ,0,1,2,0)或(97,0,1,0,2)的方案,即放弃2号,而给3号一枚金币,同时给4号(或5号)2枚金币。由于1号的这一方案对于3号和4号(或5号)来说,相比2号分配时更优,他们将投1号的赞成票,再加上1号自己的票,1号的方案可获通过,97枚金币可轻松落入囊中。这无疑是1号能够获取最大收益的方案了!
网上看到由此引出的一段思考如下:
1、当老大是不容易的,企业家就是要把各方面"摆平"。这里说的企业家包括熊比特说的政治家。
2、任何"分配者"想让自己的方案获得通过的关键是事先考虑清楚 "挑战者"的分配方案是什么,并用最小的代价获取最大收益,拉拢"挑战者"分配方案中最不得意的人们。想一想历朝历代的农民起义,想一想绵延起不断的宫廷斗争,想一想我们这个时代比比皆是的结盟与背叛,想一想企业内部的明争暗斗,想一想办公室脚下使绊的政治,哪一个得胜者不是采用的类似"海盗分金"的办法?为什么革命者总是找穷苦人,因为他们是最失意的人。为什么恐怖分子拉登在沙特阿拉伯没有市场,在阿富汗却大受欢迎,因为阿富汗是全球化的弃儿。为什么企业中的一把手,在搞内部人控制时,经常是抛开二号人物,而与会计和出纳们打得火热,难道不是因为公司里的小人物好收买,而二号人物却总是野心勃勃地想着取而代之......
3、国际交易中的先发优势和后发劣势。1号看起来最有可能喂鲨鱼,但他牢牢地把握住先发优势,结果不但消除了死亡威胁,还收益最大。这不正是全球化过程中先进国家先发优势吗?而5号,看起来最安全,没有死亡的威胁,甚至还能坐收渔人之利。却因不得不看别人脸色行事而只能分得一小杯羹。这难道不是后发劣势的写照?可以预料,如果中国人总是处于5号位置,总是坐等别人制定规则,未来就不见得会比5号好到那里去!
4、海盗的逻辑当然不是真实世界的唯一内幕。"海盗分金"模型是一个非常精致的模型,但它只是一个有益的智力测验,而现实世界要痹烩个模型复杂得多。
5、现实中肯定不会是人人都绝顶聪明兼"绝对理性"。回到"海盗分金"的模型中,只要3号,4号或5号中有一个人偏离了绝对聪明兼绝顶理性的假设,海盗1号保不准就会被扔到海里去了。所以,1号首先要考虑的就是他的海盗兄弟们的聪明和理性究竟靠得住靠不住,而断断不敢自取97颗金币,拼了性命去狂赌。
6、偏好和效用及其替代是另外的一个大问题。现实中人们是如此的复杂,某人的神经末稍微偏离一毫,就可能表现得对金币满不在乎而偏偏喜欢看同伙被扔进海里喂鲨鱼。果真如此,1号自以为得计的方案岂成了自掘坟墓!
7、再就是俗话所说的"人心隔肚皮"。这翻译成经济学语言则是信息不对称。由于信息不对称,谎言和虚假承诺就大有用武之地,而阴谋也会象杂草般疯长,并借机获益。譬如,2号完全可以对3、4、5号大放烟幕弹,假称对于1号所提出任何分配方案,他一定会再多加上一个金币给他们。果真如此,结果又当如何?
8、通常,在现实世界中,人人都有自认的公平标准,因而时常会嘟嚷:"谁动了我的奶酪"?而且,规则是由人制订的,所以,当人一旦意识到这个规则对自己不利的时候,并且知道这对大多数人不利的时候,就会想到修改规则。也就是我们说的"文化可以颠覆制度"。可以料想,一旦1号所提方案和大家所想的不符合的时候,就会有人大闹......当大家都闹将起来的时候,1号能拿着97枚金币毫发不损地、镇定自若地地走出去吗?最大的可能就是,海盗们会要求修改规则,然后重新分配。
9、假如由一次博弈变成重复博弈呢?比如,大家讲清楚下次再得100枚金币时,先由2号海盗来分......然后是3号......这颇有点象美国总统选举,轮流主政。说白了,其实是==制度下的分赃制。可能还会有痹烩更闹得凶的。比如,四人会想:1号居然要独得97枚金币,这还得了。于是,他们立即形成一个反1号的大联盟并制定出新规则:四人平分金币,独将1号扔进大海......这便是阿Q式的革命理想:高举平均主义的旗帜,将富人扔进死亡深渊,睡吴妈的床......
10、制度的重要在这个故事中得到了最突出的体现,这样一个分配制度,很容易诱导人们走向自相残杀,这是在游戏中容易被人忽略的。美国的总统制度中有一条重要的规则是,总统下台后可以建一个图书馆(当然还有其它优厚的条件),而不是"推到海里",这就保证了文明的权力交替,可以想象,如果所有的总统都没有好下场,那总统不仅不愿意下台,而且还会做很多的保护自己的手脚,政治就不可能清明。
11、现实的确是太复杂了。这也说明制度是多么的重要,也说明参与制度的制定是多么重要。
还有很多,不能全说出来了,留给大家曰后去思考。
囚徒困境引发的悲观和合作的突围
自从博弈论被用于人类行为的研究以来,“囚徒困境”这个题目已被无数次地提及,它描述的是这样一个事态:尽管个体之间面临通过合作增进各自利益的机会,但从个体的角度看,合作却不是最优选择,因而当参与博弈的个体都按其最优选择行动时,合作无法达成,潜在的合作增益也就无法实现。
比如,甲有几个金币,有真的有假的,乙有几盒雪茄,有些是用正宗古巴烟叶卷的,有些则用的是劣质廉价烟叶,金币的真假和雪茄的优劣只有甲乙自己知道,当面无法分辨;现在,甲和乙在街上相遇,他们面临一个机会:如果甲用真金币换乙的上等雪茄,各自的利益将获得很大增进,但显然,对于甲,最优选择是用假币支付(无论他将从乙那里得到上等雪茄还是劣质货),对于乙,最优选择是给对方劣质雪茄(无论他将从甲那里得到真币还是假币),这样一来,博弈的结果将是:甲用假币从乙那里换得了劣质雪茄。虽然这一结果可能对双方也略有增益,但潜在的更大交换增益却没有实现。
囚徒困境对于人类社会通过个体间合作而改进的可能性,提示了一个悲观的前景。然而,尽管人类社会不尽完美,我们却看到合作无处不在,自古就有。
面对理论与现实的这一反差,有些人欢呼人性之善——人并非生物学家所指出或经济学家所假设的那样只顾自己的利益,人具有合作和利他的本性;另一些人则声称,人类社会之所以没有陷入囚徒困境所预示的那种充满背叛和欺诈的、相互为敌的状态,是因为存在像国家这样拥有压倒性力量的利维坦,来提供秩序和公正,惩恶扬善,基于合作的改进才得以实现。
科学研究遭遇困境时,便到系统之外去物色甚或制造一块万能膏药,来贴补理论的缺口,这种做法我们已经看得很多了。搞不懂埃及初民如何建造金字塔,就找个外星人,解不透细胞化学机制之精妙,就想出智能设计者,进化过程重建困难,就要去找上帝了。种种这些,我称之为“因无知而称更有知”,或者更简化一下,叫“因惑称知”。
扯远了,回到囚徒困境的问题。研究者很快发现,实际上,囚徒困境所预示的悲观后果仅仅在单次博弈中才表现得那么纯粹,而在多次重复博弈中,个体之间或多或少会达成合作。研究者并且发现,达成合作的几率与如下几个变量密切相关:
1) 博弈者相互识别的能力;
2) 博弈者记忆先前遭遇的能力;
3) 博弈者之间先前遭遇的次数;
4) 博弈者之间先前达成合作的次数;
5) 博弈各方再次遭遇的几率;
6) 博弈者标示自己策略的能力;
7) 博弈者识别对方策略的能力;
8) 博弈者判别对方策略标示之真伪的能力;
9) 博弈者之间交流的可能性;
10) 群体内交换对他人评价的机会;
11)一个评价在群体内传播的预期广度和可能受众与被评价者的遭遇几率;
12) 预期未来博弈次数;
……
请注意,上述研究中并未引入任何除个体利益之外的影响个体选择的因素,也未引入任何集中控制者和外部强加的规则,作为合作达成之前提的这些变量,全都是关于信息表达和识别的。也就是说,博弈论研究预示了,一个由仅受自我利益驱动的众多个体所组成的群体内,有条件的合作策略不仅有存在的可能,而且将占上风。换句话说,以霍布斯的丛林模型作为逻辑起点的博弈论演绎,其结果很可能不是一个霍布斯所想象的丛林社会。
于是,在一个戴着博弈论眼镜观察人类社会演化进程的观察者眼里,自然而然的,信息便占据了核心地位。好奇心将驱使他去探索,人类个体们究竟如何发展了他们的信息表达和识别手段,这些手段又如何促成了个体间的合作。
带着上面这副眼镜,我所看到的是人类所创造的一个复杂精妙的符号系统,这一系统渗透到了人类社会的每个角落,时刻影响着人的行为。尽管它屡屡遭受机会主义行为的破坏,但仍在不断发展,并时刻显示着它的强大力量。在以后的各篇中,我将尝试着描绘出从我的眼镜所看到的一幅幅景象
'New door' can be arranged to 'one word'
How could you rearrange the letters in the words ‘new door’ to make one word? (Note: There is only one correct answer.)
'New door' can be arranged to 'one word'
'New door' can be arranged to 'one word'
One answer to the Morris puzzle
1 11 21 1211 111221 ... What's the next number?
the numeral 1 is just one one
one one can be written as 1 1
1 1 is actually two ones.
two ones can be written as 2 1
2 1 is one two and one one
one one and one two is 1 2 1 1
1 2 1 1 is one one, one two, and two ones.
one one, one two, and two ones is 1 1 1 2 2 1.
1 1 1 2 2 1 is three ones, two twos, and one one.
So the next number is 3 1 2 2 1 1.
the numeral 1 is just one one
one one can be written as 1 1
1 1 is actually two ones.
two ones can be written as 2 1
2 1 is one two and one one
one one and one two is 1 2 1 1
1 2 1 1 is one one, one two, and two ones.
one one, one two, and two ones is 1 1 1 2 2 1.
1 1 1 2 2 1 is three ones, two twos, and one one.
So the next number is 3 1 2 2 1 1.
Thursday, 28 June 2007
国际贸易流程详解
为使您的产品顺利出口,我们将贸易出口货物的基本流程介绍给您,以使您对产品出口程序做到心中有数、万无一失 。
出口货物流程主要包括:报价、订货、付款方式、备货、包装、通关手续、装船、运输保险、提单、结汇。
一、报价
在国际贸易中一般是由产品的询价、报价作为贸易的开始。其中,对于出口产品的报价主要包括:产品的质量等级、产品的规格型号、产品是否有特殊包装要求、所购产品量的多少、交货期的要求、产品的运输方式、产品的材质等内容。
比较常用的报价有: FOB"船上交货"、CNF"成本加运费"、CIF"成本、保险费加运费"等形式。
二、订货(签约)
贸易双方就报价达成意向后,买方企业正式订货并就一些相关事项与卖方企业进行协商,双方协商认可后,需要签订《购货合同》。在签订《购货合同》过程中,主要对商品名称、规格型号、数量、价格、包装、产地、装运期、付款条件、结算方式、索赔、仲裁等内容进行商谈,并将商谈后达成的协议写入《购货合同》。这标志着出口业务的正式开始。通常情况下,签订购货合同一式两份由双方盖本公司公章生效,双方各保存一份。
三、付款方式
比较常用的国际付款方式有三种,即信用证付款方式、TT付款方式和直接付款方式。1、信用证付款方式
信用证分为光票信用证和跟单信用证两类。跟单信用证是指附有指定单据的信用证,不附任何单据的信用证称光票信用证。简单地说,信用证是保证出口商收回货款的保证文件。请注意,出口货物的装运期限应在信用证的有效期限内进行,信用证交单期限必须不迟于信用证的有效日期内提交。
国际贸易中以信用证为付款方式的居多,信用证的开证日期应当明确、清楚、完整。中国的几家国有商业银行,如中国银行、中国建设银行、中国农业银行、中国工商银行等,都能够对外开立信用证(这几家主要银行的开证手续费都是开证金额的1.5‰)。
2、TT付款方式
TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款。
3、直接付款方式
是指买卖双方直接交货付款。
四、备货
备货在整个贸易流程中,起到举足轻重的重要地位,须按照合同逐一落实。备货的主要核对内容如下:
1、货物品质、规格,应按合同的要求核实。
2、货物数量:保证满足合同或信用证对数量的要求。
3、备货时间:应根据信用证规定,结合船期安排,以利于船货衔接。
五、包装
您可以根据货物的不同,来选择包装形式(如:纸箱、木箱、编织袋等)。不同的包装形式其包装要求也有所不同。
1、一般出口包装标准:根据贸易出口通用的标准进行包装。
2、特殊出口包装标准:根据客户的特殊要求进行出口货物包装。
3、货物的包装和唛头(运输标志):应进行认真检查核实,使之符合信用证的规定。
六、通关手续
通关手续极为烦琐又极其重要,如不能顺利通关则无法完成交易。
1、属法定检验的出口商品须办出口商品检验证书。
目前我国进出口商品检验工作主要有四个环节:
○接受报验:报验是指对外贸易关系人向商检机构报请检验。
○抽样:商检机构接受报验之后,及时派员赴货物堆存地点进行现场检验、鉴定。
○检验:商检机构接受报验之后,认真研究申报的检验项目,确定检验内容。并仔细审核合同(信用证)对品质、规格、包装的规定,弄清检验的依据,确定检验标准、方法。(检验方法有抽样检验,仪器分析检验;物理检验;感官检验;微生物检验等)
○签发证书:在出口方面,凡列入〖种类表〗内的出口商品,经商检机构检验合格后,签发放行单(或在"出口货物报关单"上加盖放行章,以代替放行单)。
2、须由专业持有报关证人员,持箱单、发票、报关委托书、出口结汇核销单、出口货物合同副本、出口商品检验证书等文本去海关办理通关手续。
○箱单是由出口商提供的出口产品装箱明细。
○发票是由出口商提供的出口产品证明。
○报关委托书是没有报关能力的单位或个人委托报关代理行来报关的证明书。
○出口核销单由出口单位到外汇局申领,指有出口能力的单位取得出口退税的一种单据。
○商检证书是经过出入境检验检疫部门或其指定的检验机构检验合格后而得到的,是各种进出口商品检验证书、鉴定证书和其他证明书的统称。是对外贸易有关各方履行契约义务、处理索赔争、议和仲裁、诉讼举证,具有法律依据的有效证件,同时也是海关验放、征收关税和优惠减免关税的必要证明。
附流程图 转自合众出口网
为使您的产品顺利出口,我们将贸易出口货物的基本流程介绍给您,以使您对产品出口程序做到心中有数、万无一失 。
出口货物流程主要包括:报价、订货、付款方式、备货、包装、通关手续、装船、运输保险、提单、结汇。
一、报价
在国际贸易中一般是由产品的询价、报价作为贸易的开始。其中,对于出口产品的报价主要包括:产品的质量等级、产品的规格型号、产品是否有特殊包装要求、所购产品量的多少、交货期的要求、产品的运输方式、产品的材质等内容。
比较常用的报价有: FOB"船上交货"、CNF"成本加运费"、CIF"成本、保险费加运费"等形式。
二、订货(签约)
贸易双方就报价达成意向后,买方企业正式订货并就一些相关事项与卖方企业进行协商,双方协商认可后,需要签订《购货合同》。在签订《购货合同》过程中,主要对商品名称、规格型号、数量、价格、包装、产地、装运期、付款条件、结算方式、索赔、仲裁等内容进行商谈,并将商谈后达成的协议写入《购货合同》。这标志着出口业务的正式开始。通常情况下,签订购货合同一式两份由双方盖本公司公章生效,双方各保存一份。
三、付款方式
比较常用的国际付款方式有三种,即信用证付款方式、TT付款方式和直接付款方式。1、信用证付款方式
信用证分为光票信用证和跟单信用证两类。跟单信用证是指附有指定单据的信用证,不附任何单据的信用证称光票信用证。简单地说,信用证是保证出口商收回货款的保证文件。请注意,出口货物的装运期限应在信用证的有效期限内进行,信用证交单期限必须不迟于信用证的有效日期内提交。
国际贸易中以信用证为付款方式的居多,信用证的开证日期应当明确、清楚、完整。中国的几家国有商业银行,如中国银行、中国建设银行、中国农业银行、中国工商银行等,都能够对外开立信用证(这几家主要银行的开证手续费都是开证金额的1.5‰)。
2、TT付款方式
TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款。
3、直接付款方式
是指买卖双方直接交货付款。
四、备货
备货在整个贸易流程中,起到举足轻重的重要地位,须按照合同逐一落实。备货的主要核对内容如下:
1、货物品质、规格,应按合同的要求核实。
2、货物数量:保证满足合同或信用证对数量的要求。
3、备货时间:应根据信用证规定,结合船期安排,以利于船货衔接。
五、包装
您可以根据货物的不同,来选择包装形式(如:纸箱、木箱、编织袋等)。不同的包装形式其包装要求也有所不同。
1、一般出口包装标准:根据贸易出口通用的标准进行包装。
2、特殊出口包装标准:根据客户的特殊要求进行出口货物包装。
3、货物的包装和唛头(运输标志):应进行认真检查核实,使之符合信用证的规定。
六、通关手续
通关手续极为烦琐又极其重要,如不能顺利通关则无法完成交易。
1、属法定检验的出口商品须办出口商品检验证书。
目前我国进出口商品检验工作主要有四个环节:
○接受报验:报验是指对外贸易关系人向商检机构报请检验。
○抽样:商检机构接受报验之后,及时派员赴货物堆存地点进行现场检验、鉴定。
○检验:商检机构接受报验之后,认真研究申报的检验项目,确定检验内容。并仔细审核合同(信用证)对品质、规格、包装的规定,弄清检验的依据,确定检验标准、方法。(检验方法有抽样检验,仪器分析检验;物理检验;感官检验;微生物检验等)
○签发证书:在出口方面,凡列入〖种类表〗内的出口商品,经商检机构检验合格后,签发放行单(或在"出口货物报关单"上加盖放行章,以代替放行单)。
2、须由专业持有报关证人员,持箱单、发票、报关委托书、出口结汇核销单、出口货物合同副本、出口商品检验证书等文本去海关办理通关手续。
○箱单是由出口商提供的出口产品装箱明细。
○发票是由出口商提供的出口产品证明。
○报关委托书是没有报关能力的单位或个人委托报关代理行来报关的证明书。
○出口核销单由出口单位到外汇局申领,指有出口能力的单位取得出口退税的一种单据。
○商检证书是经过出入境检验检疫部门或其指定的检验机构检验合格后而得到的,是各种进出口商品检验证书、鉴定证书和其他证明书的统称。是对外贸易有关各方履行契约义务、处理索赔争、议和仲裁、诉讼举证,具有法律依据的有效证件,同时也是海关验放、征收关税和优惠减免关税的必要证明。
附流程图 转自合众出口网
七、装船
在货物装船过程中,您可以根据货物的多少来决定装船方式,并根据《购货合同》所定的险种来进行投保。可选择:
1、整装集装箱
集装箱(又称货柜)的种类:
(1)按规格尺寸分:目前,国际上通常使用的干货柜(DRYCONTAINER)有:
外尺寸为20英尺X8英尺X8英尺6吋,简称20尺货柜;
40英尺X8英尺X8英尺6吋,简称40尺货柜; 及近年较多使用的40英尺X8英尺X9英尺6吋,简称40尺高柜。
20尺柜:内容积为5.69米X2.13米X2.18米,配货毛重一般为17.5吨,体积为24-26立方米.
40尺柜:内容积为11.8米X2.13米X2.18米,配货毛重一般为22吨,体积为54立方米.
40尺高柜:内容积为11.8米X2.13米X2.72米.配货毛重一般为22吨,体积为68立方米.
45尺高柜:内容积为:13.58米X2.34米X2.71米,配货毛重一般为29吨,体积为86立方米.
20尺开顶柜:内容积为5.89米X2.32米X2.31米,配货毛重20吨,体积31.5立方米.
40尺开顶柜:内容积为12.01米X2.33米X2.15米,配货毛重30.4吨,体积65立方米.
20尺平底货柜:内容积5.85米X2.23米X2.15米,配货毛重23吨,体积28立方米.
40尺平底货柜:内容积12.05米X2.12米X1.96米,配货毛重36吨,体积50立方米.
(2)按制箱材料分:有铝合金集装箱,钢板集装箱,纤维板集装箱,玻璃钢集装箱.
(3)按用途分:有干集装箱;冷冻集装箱(REEFER CONTAINER);挂衣集装箱(DRESS HANGER CONTAINER);开顶集装箱(OPENTOP CONTAINER);框架集装箱(FLAT RACK CONTAINER);罐式集装箱(TANK CONTAINER).
2、拼装集装箱
拼装集装箱,一般按出口货物的体积货重量计算运费。
八、运输保险
通常双方在签定《购货合同》中已事先约定运输保险的相关事项。常见的保险有海洋货物运输保险、陆空邮货运输保险等。其中,海洋运输货物保险条款所承保的险别,分为基本险别和附加险别两类:
(1) 基本险别有平安险(Free from Paricular Average-F.P.A)、水渍险(With Average or With Particular Average-W.A or W.P.A)和一切险(All Risk-A.R.)三种。平安险的责任范围包括:由于海上自然灾害引起的货物全损;货物在装卸和转船过程中的整体灭失;由于共同海损引起的牺牲、分担和救助费用;由于运输船只触礁、搁浅、沉没、碰撞、水灾、爆炸引起的货物全损和部分损失。水渍险是海洋运输保险的基本险之一。按中国人民保险公司的保险条款,其责任范围除了承担平安险所列各项风险外,还承担恶劣气候、雷电、海啸、洪水等自然灾害的风险。一切险的承报保责任范围相当于水渍险和一般附加险的总和。
(2) 附加险别。附加险别有一般附加险和特别附加险两种类型。一般附加险有偷窃提货不着险、淡水雨淋险、抽窃短量险、渗漏险、破损破碎险、钩损险、混杂沾污险、包装破裂险、霉变险、受潮受热险、串味险等。特别附加险有战争险、罢工险等。
九、提单
提单是出口商办理完出口通关手续、海关放行后,由外运公司签出、供进口商提货、结汇所用单据。
所签提单根据信用证所提要求份数签发,一般是三份。出口商留二份,办理退税等业务,一份寄给进口商用来办理提货等手续。
进行海运货物时,进口商必须持正本提单、箱单、发票来提取货物。(须由出口商将正本提单、箱单、发票寄给进口商。)
若是空运货物,则可直接用提单、箱单、发票的传真件来提取货物。
十、结汇
出口货物装出之后,进出口公司即应按照信用证的规定,正确缮制(箱单、发票、提单、出口产地证明、出口结汇)等单据。在信用证规定的交单有效期内,递交银行办理议付结汇手续。
除采用信用证结汇外,其它付款的汇款方式一般有电汇(TELEGRAPHIC TRANSFER(T/T))、票汇(DEMAND DRAFT(D/D))、信汇(MAIL TRANDFER(M/T))等方式,由于电子化的高速发展,现在汇款主要使用电汇方式。(在中国,企业出口享有出口退税优惠政策)
出口货物流程主要包括:报价、订货、付款方式、备货、包装、通关手续、装船、运输保险、提单、结汇。
一、报价
在国际贸易中一般是由产品的询价、报价作为贸易的开始。其中,对于出口产品的报价主要包括:产品的质量等级、产品的规格型号、产品是否有特殊包装要求、所购产品量的多少、交货期的要求、产品的运输方式、产品的材质等内容。
比较常用的报价有: FOB"船上交货"、CNF"成本加运费"、CIF"成本、保险费加运费"等形式。
二、订货(签约)
贸易双方就报价达成意向后,买方企业正式订货并就一些相关事项与卖方企业进行协商,双方协商认可后,需要签订《购货合同》。在签订《购货合同》过程中,主要对商品名称、规格型号、数量、价格、包装、产地、装运期、付款条件、结算方式、索赔、仲裁等内容进行商谈,并将商谈后达成的协议写入《购货合同》。这标志着出口业务的正式开始。通常情况下,签订购货合同一式两份由双方盖本公司公章生效,双方各保存一份。
三、付款方式
比较常用的国际付款方式有三种,即信用证付款方式、TT付款方式和直接付款方式。1、信用证付款方式
信用证分为光票信用证和跟单信用证两类。跟单信用证是指附有指定单据的信用证,不附任何单据的信用证称光票信用证。简单地说,信用证是保证出口商收回货款的保证文件。请注意,出口货物的装运期限应在信用证的有效期限内进行,信用证交单期限必须不迟于信用证的有效日期内提交。
国际贸易中以信用证为付款方式的居多,信用证的开证日期应当明确、清楚、完整。中国的几家国有商业银行,如中国银行、中国建设银行、中国农业银行、中国工商银行等,都能够对外开立信用证(这几家主要银行的开证手续费都是开证金额的1.5‰)。
2、TT付款方式
TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款。
3、直接付款方式
是指买卖双方直接交货付款。
四、备货
备货在整个贸易流程中,起到举足轻重的重要地位,须按照合同逐一落实。备货的主要核对内容如下:
1、货物品质、规格,应按合同的要求核实。
2、货物数量:保证满足合同或信用证对数量的要求。
3、备货时间:应根据信用证规定,结合船期安排,以利于船货衔接。
五、包装
您可以根据货物的不同,来选择包装形式(如:纸箱、木箱、编织袋等)。不同的包装形式其包装要求也有所不同。
1、一般出口包装标准:根据贸易出口通用的标准进行包装。
2、特殊出口包装标准:根据客户的特殊要求进行出口货物包装。
3、货物的包装和唛头(运输标志):应进行认真检查核实,使之符合信用证的规定。
六、通关手续
通关手续极为烦琐又极其重要,如不能顺利通关则无法完成交易。
1、属法定检验的出口商品须办出口商品检验证书。
目前我国进出口商品检验工作主要有四个环节:
○接受报验:报验是指对外贸易关系人向商检机构报请检验。
○抽样:商检机构接受报验之后,及时派员赴货物堆存地点进行现场检验、鉴定。
○检验:商检机构接受报验之后,认真研究申报的检验项目,确定检验内容。并仔细审核合同(信用证)对品质、规格、包装的规定,弄清检验的依据,确定检验标准、方法。(检验方法有抽样检验,仪器分析检验;物理检验;感官检验;微生物检验等)
○签发证书:在出口方面,凡列入〖种类表〗内的出口商品,经商检机构检验合格后,签发放行单(或在"出口货物报关单"上加盖放行章,以代替放行单)。
2、须由专业持有报关证人员,持箱单、发票、报关委托书、出口结汇核销单、出口货物合同副本、出口商品检验证书等文本去海关办理通关手续。
○箱单是由出口商提供的出口产品装箱明细。
○发票是由出口商提供的出口产品证明。
○报关委托书是没有报关能力的单位或个人委托报关代理行来报关的证明书。
○出口核销单由出口单位到外汇局申领,指有出口能力的单位取得出口退税的一种单据。
○商检证书是经过出入境检验检疫部门或其指定的检验机构检验合格后而得到的,是各种进出口商品检验证书、鉴定证书和其他证明书的统称。是对外贸易有关各方履行契约义务、处理索赔争、议和仲裁、诉讼举证,具有法律依据的有效证件,同时也是海关验放、征收关税和优惠减免关税的必要证明。
附流程图 转自合众出口网
为使您的产品顺利出口,我们将贸易出口货物的基本流程介绍给您,以使您对产品出口程序做到心中有数、万无一失 。
出口货物流程主要包括:报价、订货、付款方式、备货、包装、通关手续、装船、运输保险、提单、结汇。
一、报价
在国际贸易中一般是由产品的询价、报价作为贸易的开始。其中,对于出口产品的报价主要包括:产品的质量等级、产品的规格型号、产品是否有特殊包装要求、所购产品量的多少、交货期的要求、产品的运输方式、产品的材质等内容。
比较常用的报价有: FOB"船上交货"、CNF"成本加运费"、CIF"成本、保险费加运费"等形式。
二、订货(签约)
贸易双方就报价达成意向后,买方企业正式订货并就一些相关事项与卖方企业进行协商,双方协商认可后,需要签订《购货合同》。在签订《购货合同》过程中,主要对商品名称、规格型号、数量、价格、包装、产地、装运期、付款条件、结算方式、索赔、仲裁等内容进行商谈,并将商谈后达成的协议写入《购货合同》。这标志着出口业务的正式开始。通常情况下,签订购货合同一式两份由双方盖本公司公章生效,双方各保存一份。
三、付款方式
比较常用的国际付款方式有三种,即信用证付款方式、TT付款方式和直接付款方式。1、信用证付款方式
信用证分为光票信用证和跟单信用证两类。跟单信用证是指附有指定单据的信用证,不附任何单据的信用证称光票信用证。简单地说,信用证是保证出口商收回货款的保证文件。请注意,出口货物的装运期限应在信用证的有效期限内进行,信用证交单期限必须不迟于信用证的有效日期内提交。
国际贸易中以信用证为付款方式的居多,信用证的开证日期应当明确、清楚、完整。中国的几家国有商业银行,如中国银行、中国建设银行、中国农业银行、中国工商银行等,都能够对外开立信用证(这几家主要银行的开证手续费都是开证金额的1.5‰)。
2、TT付款方式
TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款。
3、直接付款方式
是指买卖双方直接交货付款。
四、备货
备货在整个贸易流程中,起到举足轻重的重要地位,须按照合同逐一落实。备货的主要核对内容如下:
1、货物品质、规格,应按合同的要求核实。
2、货物数量:保证满足合同或信用证对数量的要求。
3、备货时间:应根据信用证规定,结合船期安排,以利于船货衔接。
五、包装
您可以根据货物的不同,来选择包装形式(如:纸箱、木箱、编织袋等)。不同的包装形式其包装要求也有所不同。
1、一般出口包装标准:根据贸易出口通用的标准进行包装。
2、特殊出口包装标准:根据客户的特殊要求进行出口货物包装。
3、货物的包装和唛头(运输标志):应进行认真检查核实,使之符合信用证的规定。
六、通关手续
通关手续极为烦琐又极其重要,如不能顺利通关则无法完成交易。
1、属法定检验的出口商品须办出口商品检验证书。
目前我国进出口商品检验工作主要有四个环节:
○接受报验:报验是指对外贸易关系人向商检机构报请检验。
○抽样:商检机构接受报验之后,及时派员赴货物堆存地点进行现场检验、鉴定。
○检验:商检机构接受报验之后,认真研究申报的检验项目,确定检验内容。并仔细审核合同(信用证)对品质、规格、包装的规定,弄清检验的依据,确定检验标准、方法。(检验方法有抽样检验,仪器分析检验;物理检验;感官检验;微生物检验等)
○签发证书:在出口方面,凡列入〖种类表〗内的出口商品,经商检机构检验合格后,签发放行单(或在"出口货物报关单"上加盖放行章,以代替放行单)。
2、须由专业持有报关证人员,持箱单、发票、报关委托书、出口结汇核销单、出口货物合同副本、出口商品检验证书等文本去海关办理通关手续。
○箱单是由出口商提供的出口产品装箱明细。
○发票是由出口商提供的出口产品证明。
○报关委托书是没有报关能力的单位或个人委托报关代理行来报关的证明书。
○出口核销单由出口单位到外汇局申领,指有出口能力的单位取得出口退税的一种单据。
○商检证书是经过出入境检验检疫部门或其指定的检验机构检验合格后而得到的,是各种进出口商品检验证书、鉴定证书和其他证明书的统称。是对外贸易有关各方履行契约义务、处理索赔争、议和仲裁、诉讼举证,具有法律依据的有效证件,同时也是海关验放、征收关税和优惠减免关税的必要证明。
附流程图 转自合众出口网
七、装船
在货物装船过程中,您可以根据货物的多少来决定装船方式,并根据《购货合同》所定的险种来进行投保。可选择:
1、整装集装箱
集装箱(又称货柜)的种类:
(1)按规格尺寸分:目前,国际上通常使用的干货柜(DRYCONTAINER)有:
外尺寸为20英尺X8英尺X8英尺6吋,简称20尺货柜;
40英尺X8英尺X8英尺6吋,简称40尺货柜; 及近年较多使用的40英尺X8英尺X9英尺6吋,简称40尺高柜。
20尺柜:内容积为5.69米X2.13米X2.18米,配货毛重一般为17.5吨,体积为24-26立方米.
40尺柜:内容积为11.8米X2.13米X2.18米,配货毛重一般为22吨,体积为54立方米.
40尺高柜:内容积为11.8米X2.13米X2.72米.配货毛重一般为22吨,体积为68立方米.
45尺高柜:内容积为:13.58米X2.34米X2.71米,配货毛重一般为29吨,体积为86立方米.
20尺开顶柜:内容积为5.89米X2.32米X2.31米,配货毛重20吨,体积31.5立方米.
40尺开顶柜:内容积为12.01米X2.33米X2.15米,配货毛重30.4吨,体积65立方米.
20尺平底货柜:内容积5.85米X2.23米X2.15米,配货毛重23吨,体积28立方米.
40尺平底货柜:内容积12.05米X2.12米X1.96米,配货毛重36吨,体积50立方米.
(2)按制箱材料分:有铝合金集装箱,钢板集装箱,纤维板集装箱,玻璃钢集装箱.
(3)按用途分:有干集装箱;冷冻集装箱(REEFER CONTAINER);挂衣集装箱(DRESS HANGER CONTAINER);开顶集装箱(OPENTOP CONTAINER);框架集装箱(FLAT RACK CONTAINER);罐式集装箱(TANK CONTAINER).
2、拼装集装箱
拼装集装箱,一般按出口货物的体积货重量计算运费。
八、运输保险
通常双方在签定《购货合同》中已事先约定运输保险的相关事项。常见的保险有海洋货物运输保险、陆空邮货运输保险等。其中,海洋运输货物保险条款所承保的险别,分为基本险别和附加险别两类:
(1) 基本险别有平安险(Free from Paricular Average-F.P.A)、水渍险(With Average or With Particular Average-W.A or W.P.A)和一切险(All Risk-A.R.)三种。平安险的责任范围包括:由于海上自然灾害引起的货物全损;货物在装卸和转船过程中的整体灭失;由于共同海损引起的牺牲、分担和救助费用;由于运输船只触礁、搁浅、沉没、碰撞、水灾、爆炸引起的货物全损和部分损失。水渍险是海洋运输保险的基本险之一。按中国人民保险公司的保险条款,其责任范围除了承担平安险所列各项风险外,还承担恶劣气候、雷电、海啸、洪水等自然灾害的风险。一切险的承报保责任范围相当于水渍险和一般附加险的总和。
(2) 附加险别。附加险别有一般附加险和特别附加险两种类型。一般附加险有偷窃提货不着险、淡水雨淋险、抽窃短量险、渗漏险、破损破碎险、钩损险、混杂沾污险、包装破裂险、霉变险、受潮受热险、串味险等。特别附加险有战争险、罢工险等。
九、提单
提单是出口商办理完出口通关手续、海关放行后,由外运公司签出、供进口商提货、结汇所用单据。
所签提单根据信用证所提要求份数签发,一般是三份。出口商留二份,办理退税等业务,一份寄给进口商用来办理提货等手续。
进行海运货物时,进口商必须持正本提单、箱单、发票来提取货物。(须由出口商将正本提单、箱单、发票寄给进口商。)
若是空运货物,则可直接用提单、箱单、发票的传真件来提取货物。
十、结汇
出口货物装出之后,进出口公司即应按照信用证的规定,正确缮制(箱单、发票、提单、出口产地证明、出口结汇)等单据。在信用证规定的交单有效期内,递交银行办理议付结汇手续。
除采用信用证结汇外,其它付款的汇款方式一般有电汇(TELEGRAPHIC TRANSFER(T/T))、票汇(DEMAND DRAFT(D/D))、信汇(MAIL TRANDFER(M/T))等方式,由于电子化的高速发展,现在汇款主要使用电汇方式。(在中国,企业出口享有出口退税优惠政策)
Wednesday, 27 June 2007
Wednesday, 20 June 2007
心怀大众
今天毫无准备的情况下,被通知去做了思想谈话。会后, 这位老师告诫我要考虑的利益的角度应该在大一些。 能把全心全意为人民服务内化到心里去, 而不是放在口头。的确以前过多的喊口号, 这几年, 浙江范围内,人们特别是学生谈论的都是经济, 可是这种人文方面的东西也非常重要。 如果我们在创业之处,就能站在一个比较高的角度,目的不是一个致富,而是将这个企业做大作强,利于百年, 那就不会遇到现在很多企业家在做大之后遇到的那写困惑了。 这样的想法其实就是在给自己施压和减压, 施压意味着一开始他需要花更多的时间去平衡, 减压意味着很多利益纠纷将不存在,特别是家庭内部纷争。这个其实不是共产党的专利, 而是成功人类结合中国特点的共性。做任何事情, 事先考虑好, 端正好自己的利益取向, 方能在成功之后再成功。
才聚人散, 人聚才散!
才聚人散, 人聚才散!
Functional Programming For The Rest of Us
Introduction
Programmers are procrastinators. Get in, get some coffee, check the mailbox, read the RSS feeds, read the news, check out latest articles on techie websites, browse through political discussions on the designated sections of the programming forums. Rinse and repeat to make sure nothing is missed. Go to lunch. Come back, stare at the IDE for a few minutes. Check the mailbox. Get some coffee. Before you know it, the day is over.
The only thing, every once in a while challenging articles actually do pop up. If you're looking at the right places you'll find at least one of these every couple of days. These articles are hard to get through and take some time, so they start piling up. Before you know it, you have a list of links and a folder full of PDF files and you wish you had a year in a small hut in the middle of the forest with nobody around for miles so you could catch up. Would be nice if someone came in every morning while you're taking a walk down the river to bring some food and take out the garbage.
I don't know about your list, but a large chunk of the articles in mine are about functional programming. These generally are the hardest to get through. Written in a dry academic language, even the "ten year Wall Street industry veterans" don't understand what functional programming (also referred to as FP) articles are all about. If you ask a project manager in Citi Group or in Deutsche Bank1 why they chose to use JMS instead of Erlang they'll say they can't use academic languages for industrial strength applications. The problem is, some of the most complex systems with the most rigid requirements are written using functional programming elements. Something doesn't add up.
It's true that FP articles and papers are hard to understand, but they don't have to be. The reasons for the knowledge gap are purely historical. There is nothing inherently hard about FP concepts. Consider this article "an accessible guide to FP", a bridge from our imperative minds into the world of FP. Grab a coffee and keep on reading. With any luck your coworkers will start making fun of you for your FP comments in no time.
So what is FP? How did it come about? Is it edible? If it's as useful as its advocates claim, why isn't it being used more often in the industry? Why is it that only people with PhDs tend to use it? Most importantly, why is it so damn hard to learn? What is all this closure, continuation, currying, lazy evaluation and no side effects business? How can it be used in projects that don't involve a university? Why does it seem to be so different from everything good, and holy, and dear to our imperative hearts? We'll clear this up very soon. Let's start with explaining the reasons for the huge gap between the real world and academic articles. The answer is as easy as taking a walk in the park.
A Walk In The Park
Fire up the time machine. Our walk in the park took place more than two thousand years ago, on a beautiful sunny day of a long forgotten spring in 380 B.C. Outside the city walls of Athens, under the pleasant shade of olive trees Plato was walking towards the Academy with a beautiful slave boy. The weather was lovely, the dinner was filling, and the conversation turned to philosophy.
"Look at these two students", said Plato carefully picking words to make the question educational. "Who do you think is taller?" The slave boy looked towards the basin of water where two men were standing. "They're about the same height", he said. "What do you mean 'about the same'?", asked Plato. "Well, they look the same from here but I'm sure if I were to get closer I'd see that there is some difference."
Plato smiled. He was leading the boy in the right direction. "So you would say that there is nothing perfectly equal in our world?" After some thinking the boy replied: "I don't think so. Everything is at least a little different, even if we can't see it." The point hit home! "Then if nothing is perfectly equal in this world, how do you think you understand the concept of 'perfect' equality?" The slave boy looked puzzled. "I don't know", he replied.
So was born the first attempt to understand the nature of mathematics. Plato suggested that everything in our world is just an approximation of perfection. He also realized that we understand the concept of perfection even though we never encountered it. He came to conclusion that perfect mathematical forms must live in another world and that we somehow know about them by having a connection to that "alternative" universe. It's fairly clear that there is no perfect circle that we can observe. But we also understand what a perfect circle is and can describe it via equations. What is mathematics, then? Why is the universe described with mathematical laws? Can all of the phenomena of our universe be described by mathematics?2
Philosophy of mathematics is a very complex subject. Like most philosophical disciplines it is far more adapt at posing questions rather than providing answers. Much of the consensus revolves around the fact that mathematics is really a puzzle: we set up a set of basic non-conflicting principles and a set of rules on how to operate with these principles. We can then stack these rules together to come up with more complex rules. Mathematicians call this method a "formal system" or a "calculus". We can effectively write a formal system for Tetris if we wanted to. In fact, a working implementation of Tetris is a formal system, just specified using an unusual representation.
A civilization of furry creatures on Alpha Centauri would not be able to read our formalisms of Tetris and circles because their only sensory input might be an organ that senses smells. They likely will never find out about the Tetris formalism, but they very well might have a formalism for circles. We probably wouldn't be able to read it because our sense of smell isn't that sophisticated, but once you get past the representation of the formalism (via various sensory instruments and standard code breaking techniques to understand the language), the concepts underneath are understandable to any intelligent civilization.
Interestingly if no intelligent civilization ever existed in the universe the formalisms for Tetris and circles would still hold water, it's just that nobody would be around to find out about them. If an intelligent civilization popped up, it would likely discover some formalisms that help describe the laws of our universe. They also would be very unlikely to ever find out about Tetris because there is nothing in the universe that resembles it. Tetris is one of countless examples of a formal system, a puzzle, that has nothing to do with the real world. We can't even be sure that natural numbers have full resemblance to the real world, after all one can easily think of a number so big that it cannot describe anything in our universe since it might actually turn out to be finite.
A Bit of History3
Let's shift gears in our time machine. This time we'll travel a lot closer, to the 1930s. The Great Depression was ravaging the New and the Old worlds. Almost every family from every social class was affected by the tremendous economic downturn. Very few sanctuaries remained where people were safe from the perils of poverty. Few people were fortunate enough to be in these sanctuaries, but they did exist. Our interest lies in mathematicians in Princeton University.
The new offices constructed in gothic style gave Princeton an aura of a safe haven. Logicians from all over the world were invited to Princeton to build out a new department. While most of America couldn't find a piece of bread for dinner, high ceilings, walls covered with elaborately carved wood, daily discussions by a cup of tea, and walks in the forest were some of the conditions in Princeton.
One mathematician living in such lavish lifestyle was a young man named Alonzo Church. Alonzo received a B.S. degree from Princeton and was persuaded to stay for graduate school. Alonzo felt the architecture was fancier than necessary. He rarely showed up to discuss mathematics with a cup of tea and he didn't enjoy the walks in the woods. Alonzo was a loner: he was most productive when working on his own. Nevertheless Alonzo had regular contacts with other Princeton inhabitants. Among them were Alan Turing, John von Neumann, and Kurt G枚del.
The four men were interested in formal systems. They didn't pay much heed to the physical world, they were interested in dealing with abstract mathematical puzzles instead. Their puzzles had something in common: the men were working on answering questions about computation. If we had machines that had infinite computational power, what problems would we be able to solve? Could we solve them automatically? Could some problems remain unsolved and why? Would various machines with different designs be equal in power?
In cooperation with other men Alonzo Church developed a formal system called lambda calculus. The system was essentially a programming language for one of these imaginary machines. It was based on functions that took other functions as parameters and returned functions as results. The function was identified by a Greek letter lambda, hence the system's name4. Using this formalism Alonzo was able to reason about many of the above questions and provide conclusive answers.
Independently of Alonzo Church, Alan Turing was performing similar work. He developed a different formalism (now referred to as the Turing machine), and used it to independently come to similar conclusions as Alonzo. Later it was shown that Turing machines and lambda calculus were equivalent in power.
This is where the story would stop, I'd wrap up the article, and you'd navigate to another page, if not for the beginning of World War II. The world was in flames. The U.S. Army and Navy used artillery more often than ever. In attempts to improve accuracy the Army employed a large group of mathematicians to continuously calculate differential equations required for solving ballistic firing tables. It was becoming obvious that the task was too great for being solved manually and various equipment was developed in order to overcome this problem. The first machine to solve ballistic tables was a Mark I built by IBM - it weighed five tons, had 750,000 parts and could do three operations per second.
The race, of course, wasn't over. In 1949 an Electronic Discrete Variable Automatic Computer (EDVAC) was unveiled and had tremendous success. It was a first example of von Neumann's architecture and was effectively a real world implementation of a Turing machine. For the time being Alonzo Church was out of luck.
In late 1950s an MIT professor John McCarthy (also a Princeton graduate) developed interest in Alonzo Church's work. In 1958 he unveiled a List Processing language (Lisp). Lisp was an implementation of Alonzo's lambda calculus that worked on von Neumann computers! Many computer scientists recognized the expressive power of Lisp. In 1973 a group of programmers at MIT's Artificial Intelligence Lab developed hardware they called a Lisp machine - effectively a native hardware implementation of Alonzo's lambda calculus!
Functional Programming
Functional programming is a practical implementation of Alonzo Church's ideas. Not all lambda calculus ideas transform to practice because lambda calculus was not designed to work under physical limitations. Therefore, like object oriented programming, functional programming is a set of ideas, not a set of strict guidelines. There are many functional programming languages, and most of them do many things very differently. In this article I will explain the most widely used ideas from functional languages using examples written in Java (yes, you could write functional programs in Java if you felt particularly masochistic). In the next couple of sections we'll take Java as is, and will make modifications to it to transform it into a useable functional language. Let's begin our quest.
Lambda calculus was designed to investigate problems related to calculation. Functional programming, therefore, primarily deals with calculation, and, surprisingly, uses functions to do so. A function is a very basic unit in functional programming. Functions are used for almost everything, even the simplest of calculations. Even variables are replaced with functions. In functional programming variables are simply aliases for expressions (so we don't have to type everything on one line). They cannot be modified. All variables can only be assigned to once. In Java terms this means that every single variable is declared as final (or const if we're dealing with C++). There are no non-final variables in FP.
final int i = 5;
final int j = i + 3;
Since every variable in FP is final two fairly interesting statements can be made. It does not make sense to always write the keyword final and it does not make sense to call variables, well... variables. We will now make two modifications to Java: every variable declared in our functional Java will be final by default, and we will refer to variables as symbols.
By now you are probably wondering how you could possibly write anything reasonably complicated in our newly created language. If every symbol is non-mutable we cannot change the state of anything! This isn't strictly true. When Alonzo was working on lambda calculus he wasn't interested in maintaining state over periods of time in order to modify it later. He was interested in performing operations on data (also commonly referred to as "calculating stuff"). However, it was proved that lambda calculus is equivalent to a Turing machine. It can do all the same things an imperative programming language can. How, then, can we achieve the same results?
It turns out that functional programs can keep state, except they don't use variables to do it. They use functions instead. The state is kept in function parameters, on the stack. If you want to keep state for a while and every now and then modify it, you write a recursive function. As an example, let's write a function that reverses a Java string. Remember, every variable we declare is final by default5.
String reverse(String arg) {
if(arg.length == 0) {
return arg;
}
else {
return reverse(arg.substring(1, arg.length)) + arg.substring(0, 1);
}
}
This function is slow because it repeatedly calls itself6. It's a memory hog because it repeatedly allocates objects. But it's functional in style. You may be interested why someone would want to program in this manner. Well, I was just about to tell you.
Benefits of FP
You're probably thinking that there's no way I can rationalize the monstrosity of a function above. When I was learning functional programming I was thinking that too. I was wrong. There are very good arguments for using this style. Some of them are subjective. For example, people claim that functional programs are easier to understand. I will leave out these arguments because every kid on the block knows that ease of understanding is in the eye of the beholder. Fortunately for me, there are plenty of objective arguments.
Unit Testing
Since every symbol in FP is final, no function can ever cause side effects. You can never modify things in place, nor can one function modify a value outside of its scope for another function to use (like a class member or a global variable). That means that the only effect of evaluating a function is its return value and the only thing that affects the return value of a function is its arguments.
This is a unit tester's wet dream. You can test every function in your program only worrying about its arguments. You don't have to worry about calling functions in the right order, or setting up external state properly. All you need to do is pass arguments that represent edge cases. If every function in your program passes unit tests you can be a lot more confident about quality of your software than if you were using an imperative language. In Java or C++ checking a return value of a function is not sufficient - it may modify external state that we would need to verify. Not so in a functional language.
Debugging
If a functional program doesn't behave the way you expect it to, debugging it is a breeze. You will always be able to reproduce your problem because a bug in a functional program doesn't depend on unrelated code paths that were executed before it. In an imperative program a bug resurfaces only some of the time. Because functions depend on external state produced by side effects from other functions you may have to go through a series of steps in no way related to the bug. In a functional program this isn't the case - if a return value of a function is wrong, it is always wrong, regardless of what code you execute before running the function.
Once you reproduce the problem, getting to the bottom of it is trivial. It is almost pleasant. You break the execution of your program and examine the stack. Every argument in every function call in the stack is available for your inspection, just like in an imperative program. Except in an imperative program that's not enough because functions depend on member variables, global variables, and the state of other classes (which in turn depend on these very same things). A function in a functional program depends only on its arguments, and that information is right before your eyes! Furthermore, in an imperative program examining a return value of a function will not give you a good idea of whether the function behaves properly. You need to hunt down dozens of objects outside its scope to verify that it performed correct actions. In a functional program all you have to do is look at the return value!
Walking through the stack you look at arguments passed to functions and their return values. The minute a return value doesn't make sense you step into the offending function and walk through it. You repeat this recursively until the process leads you to the source of the bug!
Concurrency
A functional program is ready for concurrency without any further modifications. You never have to worry about deadlocks and race conditions because you don't need to use locks! No piece of data in a functional program is modified twice by the same thread, let alone by two different threads. That means you can easily add threads without ever giving conventional problems that plague concurrency applications a second thought!
If this is the case, why doesn't anybody use functional programs for highly concurrent applications? Well, it turns out that they do. Ericsson designed a functional language called Erlang for use in its highly tolerant and scalable telecommunication switches. Many others recognized the benefits provided by Erlang and started using it. We're talking about telecommunication and traffic control systems that are far more scalable and reliable than typical systems designed on Wall Street. Actually, Erlang systems are not scalable and reliable. Java systems are. Erlang systems are simply rock solid.
The concurrency story doesn't stop here. If your application is inherently single threaded the compiler can still optimize functional programs to run on multiple CPUs. Take a look at the following code fragment:
String s1 = somewhatLongOperation1();
String s2 = somewhatLongOperation2();
String s3 = concatenate(s1, s2);
In a functional language the compiler could analyze the code, classify the functions that create strings s1 and s2 as potentially time consuming operations, and run them concurrently. This is impossible to do in an imperative language because each function may modify state outside of its scope and the function following it may depend on it. In functional languages automatic analysis of functions and finding good candidates for concurrent execution is as trivial as automatic inlining! In this sense functional style programs are "future proof" (as much as I hate buzzwords, I'll indulge this time). Hardware manufacturers can no longer make CPUs run any faster. Instead they increase the number of cores and attribute quadruple speed increases to concurrency. Of course they conveniently forget to mention that we get our money's worth only on software that deals with parallelizable problems. This is a very small fraction of imperative software but 100% of functional software because functional programs are all parallelizable out of the box.
Hot Code Deployment
In the old days of Windows in order to install updates it was necessary to restart the computer. Many times. After installing a newer version of a media players. With Windows XP the situation has improved significantly, yet it still isn't ideal (I ran Windows Update at work today and now an annoying system tray icon won't go away until I restart). Unix systems have had a better model for a while. In order to install an update you only need to stop relevant components, not the whole OS. While it is a better situation, for a large class of server applications it still isn't acceptable. Telecommunication systems need to be up 100% of the time because if dialing emergency is not available due to upgrades, lives may be lost. There is no reason Wall Street firms need to bring down their systems to install software updates over the weekend.
An ideal situation is updating relevant parts of the code without stopping any part of the system at all. In an imperative world this isn't possible. Consider unloading a Java class at runtime and reloading a new definition. If we were to do that every instance of a class would become unusable because the state it holds would be lost. We would need to resort to writing tricky version control code. We'd need to serialize all running instances of the class, destroy them, create instances of the new class, try to load serialized data into them hoping the loading code properly migrates the data to work with the new instance. On top of that, every time we change something we'd have to write our migration code manually. And our migration code would have to take special care not to break relationships between objects. Nice in theory, but would never work well in practice.
In a functional program all state is stored on the stack in the arguments passed to functions. This makes hot deployment significantly easier! In fact, all we'd really have to do is run a diff between the code in production and the new version, and deploy the new code. The rest could be done by language tools automatically! If you think this is science fiction, think again. Erlang engineers have been upgrading live systems without stopping them for years.
Machine Assisted Proofs and Optimizations
An interesting property of functional languages is that they can be reasoned about mathematically. Since a functional language is simply an implementation of a formal system, all mathematical operations that could be done on paper still apply to the programs written in that language. The compiler could, for example, convert pieces of code into equivalent but more efficient pieces with a mathematical proof that two pieces of code are equivalent7. Relational databases have been performing these optimizations for years. There is no reason the same techniques can't apply to regular software.
Additionally, you can use these techniques to prove that parts of your program are correct. It is even possible to create tools that analyze code and generate edge cases for unit tests automatically! This functionality is invaluable for rock solid systems. If you are designing pace makers and air traffic control systems such tools are almost always a requirement. If you are writing an application outside of truly mission critical industries, these tools can give you a tremendous edge over your competitors.
Higher Order Functions
I remember learning about the benefits I outlined above and thinking "that's all very nice but it's useless if I have to program in a crippled language where everything is final." This was a misconception. Making all variables final is crippled in a context of an imperative language like Java but it isn't in a context of functional languages. Functional languages offer a different kind of abstraction tools that make you forget you've ever liked modifying variables. One such tool is capability to work with higher order functions.
A function in such languages is different from a function in Java or C. It is a superset - it can do all the things a Java function can do, and more. We create a function in the same manner we do in C:
int add(int i, int j) {
return i + j;
}
This means something different from equivalent C code. Let's extend our Java compiler to support this notation. When we type something like this our compiler will convert it to the following Java code (don't forget, everything is final):
class add_function_t {
int add(int i, int j) {
return i + j;
}
}
add_function_t add = new add_function_t();
The symbol add isn't really a function. It is a small class with one function as its member. We can now pass add around in our code as an argument to other functions. We can assign it to another symbol. We can create instances of add_function_t at runtime and they will be garbage collected when we no longer need them. This makes functions first class objects no different from integers or strings. Functions that operate on other functions (accept them as arguments) are called higher order functions. Don't let this term intimidate you, it's no different from Java classes that operate on each other (we can pass class instances to other classes). We can call them "higher order classes" but nobody cares to because there is no strong academic community behind Java.
How, and when, do you use higher order functions? Well, I'm glad you asked. You write your program as a big monolithic blob of code without worrying about class hierarchies. When you see that a particular piece of code is repeated, you break it out into a function (fortunately they still teach this in schools). If you see that a piece of logic within your function needs to behave differently in different situations, you break it out into a higher order function. Confused? Here's a real life example from my work.
Suppose we have a piece of Java code that receives a message, transforms it in various ways, and forwards it to another server.
class MessageHandler {
void handleMessage(Message msg) {
// ...
msg.setClientCode("ABCD_123");
// ...
sendMessage(msg);
}
// ...
}
Now imagine that our system has changed and we now route messages to two servers instead of one. Everything is handled in exactly the same way except the client code - the second server wants it in a different format. How do we handle this situation? We could check where the message is headed and format the client code differently, like this:
class MessageHandler {
void handleMessage(Message msg) {
// ...
if(msg.getDestination().equals("server1") {
msg.setClientCode("ABCD_123");
} else {
msg.setClientCode("123_ABC");
}
// ...
sendMessage(msg);
}
// ...
}
This approach, however, isn't scalable. If more servers are added our function will grow linearly and we'll have a nightmare updating it. An object oriented approach is to make MessageHandler a base class and specialize the client code operation in derived classes:
abstract class MessageHandler {
void handleMessage(Message msg) {
// ...
msg.setClientCode(getClientCode());
// ...
sendMessage(msg);
}
abstract String getClientCode();
// ...
}
class MessageHandlerOne extends MessageHandler {
String getClientCode() {
return "ABCD_123";
}
}
class MessageHandlerTwo extends MessageHandler {
String getClientCode() {
return "123_ABCD";
}
}
We can now instantiate an appropriate class for each server. Adding servers becomes much more maintainable. That's a lot of code for such a simple modification though. We have to create two new types just to support different client codes! Now let's do the same thing in our language that supports higher order functions:
class MessageHandler {
void handleMessage(Message msg, Function getClientCode) {
// ...
Message msg1 = msg.setClientCode(getClientCode());
// ...
sendMessage(msg1);
}
// ...
}
String getClientCodeOne() {
return "ABCD_123";
}
String getClientCodeTwo() {
return "123_ABCD";
}
MessageHandler handler = new MessageHandler();
handler.handleMessage(someMsg, getClientCodeOne);
We've created no new types and no class hierarchy. We simply pass appropriate functions as a parameter. We've achieved the same thing as the object oriented counterpart with a number of advantages. We don't restrict ourselves to class hierarchies: we can pass new functions at runtime and change them at any time with a much higher degree of granularity with less code. Effectively the compiler has written object oriented "glue" code for us! In addition we get all the other benefits of FP. Of course the abstractions provided by functional languages don't stop here. Higher order functions are just the beginning.
Currying
Most people I've met have read the Design Patterns book by the Gang of Four. Any self respecting programmer will tell you that the book is language agnostic and the patterns apply to software engineering in general, regardless of which language you use. This is a noble claim. Unfortunately it is far removed from the truth.
Functional languages are extremely expressive. In a functional language one does not need design patterns because the language is likely so high level, you end up programming in concepts that eliminate design patterns all together. Once such pattern is an Adapter pattern (how is it different from Facade again? Sounds like somebody needed to fill more pages to satisfy their contract). It is eliminated once a language supports a technique called currying.
Adapter pattern is best known when applied to the "default" abstraction unit in Java - a class. In functional languages the pattern is applied to functions. The pattern takes an interface and transforms it to another interface someone else expects. Here's an example of an adapter pattern:
int pow(int i, int j);
int square(int i)
{
return pow(i, 2);
}
The code above adapts an interface of a function that raises an integer to an integer power to an interface of a function that squares an integer. In academic circles this trivial technique is called currying (after a logician Haskell Curry who performed mathematical acrobatics necessary to formalize it). Because in FP functions (as opposed to classes) are passed around as arguments, currying is used very often to adapt functions to an interface that someone else expects. Since the interface to functions is its arguments, currying is used to reduce the number of arguments (like in the example above).
Functional languages come with this technique built in. You don't need to manually create a function that wraps the original, functional languages will do that for you. As usual, let's extend our language to support this technique.
square = int pow(int i, 2);
This will automatically create a function square for us with one argument. It will call pow function with the second argument set to 2. This will get compiled to the following Java code:
class square_function_t {
int square(int i) {
return pow(i, 2);
}
}
square_function_t square = new square_function_t();
As you can see, we've simply created a wrapper for the original function. In FP currying is just that - a shortcut to quickly and easily create wrappers. You concentrate on your task, and the compiler writes the appropriate code for you! When do you use currying? This should be easy. Any time you'd like to use an adapter pattern (a wrapper).
Lazy Evaluation
Lazy (or delayed) evaluation is an interesting technique that becomes possible once we adopt a functional philosophy. We've already seen the following piece of code when we were talking about concurrency:
String s1 = somewhatLongOperation1();
String s2 = somewhatLongOperation2();
String s3 = concatenate(s1, s2);
In an imperative language the order of evaluation would be clear. Because each function may affect or depend on an external state it would be necessary to execute them in order: first somewhatLongOperation1, then somewhatLongOperation2, followed by concatenate. Not so in functional languages.
As we saw earlier somewhatLongOperation1 and somewhatLongOperation2 can be executed concurrently because we're guaranteed no function affects or depends on global state. But what if we don't want to run the two concurrently, do we need to run them in order? The answer is no. We only need to run these operations when another function depends on s1 and s2. We don't even have to run them before concatenate is called - we can delay their evaluation until they're required within concatenate. If we replace concatenate with a function that has a conditional and uses only one of its two parameters we may never evaluate one of the parameters at all! Haskell is an example of a delayed evaluation language. In Haskell you are not guaranteed that anything will be executed in order (or at all) because Haskell only executes code when it's required.
Lazy evaluation has numerous advantages as well as disadvantages. We will discuss the advantages here and will explain how to counter the disadvantages in the next section.
Optimization
Lazy evaluation provides a tremendous potential for optimizations. A lazy compiler thinks of functional code exactly as mathematicians think of an algebra expression - it can cancel things out and completely prevent execution, rearrange pieces of code for higher efficiency, even arrange code in a way that reduces errors, all guaranteeing optimizations won't break the code. This is the biggest benefit of representing programs strictly using formal primitives - code adheres to mathematical laws and can be reasoned about mathematically.
Abstracting Control Structures
Lazy evaluation provides a higher order of abstraction that allows implementing things in a way that would otherwise be impossible. For example consider implementing the following control structure:
unless(stock.isEuropean()) {
sendToSEC(stock);
}
We want sendToSEC executed unless the stock is European. How can we implement unless? Without lazy evaluation we'd need some form of a macro system, but in a language like Haskell that's unnecessary. We can implement unless as a function!
void unless(boolean condition, List code) {
if(!condition)
code;
}
Note that code is never evaluated if the condition is true. We cannot reproduce this behavior in a strict language because the arguments would be evaluated before unless is entered.
Infinite Data Structures
Lazy languages allow for definition of infinite data structures, something that's much more complicated in a strict language. For example, consider a list with Fibonacci numbers. We clearly can't compute and infinite list in a reasonable amount of time or store it in memory. In strict languages like Java we simply define a Fibonacci function that returns a particular member from the sequence. In a language like Haskell we can abstract it further and simply define an infinite list of Fibonacci numbers. Because the language is lazy, only the necessary parts of the list that are actually used by the program are ever evaluated. This allows for abstracting a lot of problems and looking at them from a higher level (for example, we can use list processing functions on an infinite list).
Disadvantages
Of course there ain't no such thing as a free lunch(tm). Lazy evaluation comes with a number of disadvantages. Mainly that it is, well, lazy. Many real world problems require strict evaluation. For example consider the following:
System.out.println("Please enter your name: ");
System.in.readLine();
In a lazy language you have no guarantee that the first line will be executed before the second! This means we can't do IO, can't use native functions in any meaningful way (because they need to be called in order since they depend on side effects), and can't interact with the outside world! If we were to introduce primitives that allow ordered code execution we'd lose the benefits of reasoning about our code mathematically (which would take all of the benefits of functional programming with it). Fortunately not all is lost. Mathematicians got to work and developed a number of tricks to ensure code gets executed in particular order in a functional setting. We get the best of both worlds! These techniques include continuations, monads, and uniqueness typing. In this article we'll only deal with continuations. We'll leave monads and uniqueness typing for another time. Interestingly, continuations are useful for many things other than enforcing a particular order of evaluation. We'll talk about that as well.
Continuations
Continuations to programming are what Da Vinci Code is to human history: an amazing revelation of the greatest cover-up known to man. Well, may be not, but they're certainly revealing of deceit in the same sense as square roots of negative numbers.
When we learned about functions we only learned half truths based on a faulty assumption that functions must return their value to the original caller. In this sense continuations are a generalization of functions. A function must not necessarily return to its caller and may return to any part of the program. A "continuation" is a parameter we may choose to pass to our function that specifies where the function should return. The description may be more complicated than it sounds. Take a look at the following code:
int i = add(5, 10);
int j = square(i);
The function add returns 15 to be assigned to i, the place where add was originally called. After that the value of i is used to call square. Note that a lazy compiler can't rearrange these lines of code because the second line depends on successful evaluation of the first. We can rewrite this code block using Continuation Passing Style or CPS, where the function add doesn't return to the original caller but instead returns its result to square.
int j = add(5, 10, square);
In this case add gets another parameter - a function that add must call with its result upon completion. In this case square is a continuation of add. In both cases j will equal 225.
Here lays the first trick to force a lazy language to evaluate two expressions in order. Consider the following (familiar) IO code:
System.out.println("Please enter your name: ");
System.in.readLine();
The two lines don't depend on each other and the compiler is free to rearrange them as it wishes. However, if we rewrite this code in CPS, there will be a dependency and the compiler will be forced to evaluate the two lines in order!
System.out.println("Please enter your name: ", System.in.readLine);
In this case println needs to call readLine with its result and return the result of readLine. This allows us to ensure that the two lines are executed in order and that readLine is evaluated at all (because the whole computation expects the last value as a result). In case of Java println returns void but if it were to return an abstract value (that readLine would accept), we'd solve our problem! Of course chaining function calls like that will quickly become unreadable, but it isn't necessary. We could add syntactic sugar to the language that will allow us to simply type expressions in order, and the compiler would chain the calls for us automatically. We can now evaluate expressions in any order we wish without losing any of the benefits of FP (including the ability to reason about our programs mathematically)! If this is still confusing, remember that a function is just an instance of a class with one member. Rewrite above two lines so that println and readLine are instances of classes and everything will become clear.
I would now wrap up this section, except that we've only scratched the surface of continuations and their usefulness. We can write entire programs in CPS, where every function takes an extra continuation argument and passes the result to it. We can also convert any program to CPS simply by treating functions as special cases of continuations (functions that always return to their caller). This conversion is trivial to do automatically (in fact, many compilers do just that).
Once we convert a program to CPS it becomes clear that every instruction has some continuation, a function it will call with the result, which in a regular program would be a place it must return to. Let's pick any instruction from above code, say add(5, 10). In a program written in CPS style it's clear what add's continuation is - it's a function that add calls once it's done. But what is it in a non-CPS program? We could, of course, convert the program to CPS, but do we have to?
It turns out that we don't. Look carefully at our CPS conversion. If you try to write a compiler for it and think about it long enough you'll realize that the CPS version needs no stack! No function ever "returns" in the traditional sense, it just calls another function with the result instead. We don't need to push function arguments on the stack with every call and then pop them back, we can simply store them in some block of memory and use a jump instruction instead. We'll never need the original arguments - they'll never be used again since no function ever returns!
So, programs written in CPS style have no stack but have an extra argument with a function to call. Programs not written in CPS style have no argument with a function to call, but have the stack instead. What does the stack contain? Simply the arguments, and a pointer to memory where the function should return. Do you see a light bulb? The stack simply contains continuation information! The pointer to the return instruction in the stack is essentially the same thing as the function to call in CPS programs! If you wanted to find out what continuation for add(5, 10) is, you'd simply have to examine the stack at the point of its execution!
So that was easy. A continuation and a pointer to the return instruction in the stack are really the same thing, only a continuation is passed explicitly, so that it doesn't need to be the same place where the function was called from. If you remember that a continuation is a function, and a function in our language is compiled to an instance of a class, you'll realize that a pointer to the return instruction in the stack and the continuation argument are really the same thing, since our function (just like an instance of a class) is simply a pointer. This means that at any given point in time in your program you can ask for a current continuation (which is simply the information on the stack).
Ok, so we know what a current continuation is. What does it mean? When we get a current continuation and store it somewhere, we end up storing the current state of our program - freezing it in time. This is similar to an OS putting itself into hibernation. A continuation object contains the information necessary to restart the program from the point where the continuation object was acquired. An operating system does this to your program all the time when it context switches between the threads. The only difference is that it keeps all the control. If you ask for a continuation object (in Scheme this is done by calling call-with-current-continuation function) you'll get an object that contains the current continuation - the stack (or in a CPS case the function to call next). You can store this object in a variable (or alternatively, on disk). When you choose to "restart" your program with this continuation object you will "transform" to the state of the program when you grabbed the continuation object. It's the same thing as switching back to a suspended thread or waking up an OS from hibernation, except you can do it again and again. When an OS wakes up, the hibernation information is destroyed. If it wasn't, you'd be able to wake up from the same point over and over again, almost like going back in time. You have that control with continuations!
In what situations are continuations useful? Usually when you're trying to simulate state in an application of inherently stateless nature to ease your life. A great application of continuations are web applications. Microsoft's ASP.NET goes to tremendous lengths to try and simulate state so that you can write your application with less hassle. If C# supported continuations half of ASP.NET's complexity would disappear - you'd simply store a continuation and restart it when a user makes the web request again. To a programmer of the web application there would be no interruption - the program would simply start from the next line! Continuations are an incredibly useful abstraction for some problems. Considering that many of the traditional fat clients are moving to the web, continuations will become more and more important in the future.
Pattern Matching
Pattern matching is not a new or innovative feature. In fact, it has little to do with functional programming. The only reason why it's usually attributed to FP is that functional languages have had pattern matching for some time, while modern imperative languages still don't.
Let's dive into pattern matching with an example. Here's a Fibonacci function in Java:
int fib(int n) {
if(n == 0) return 1;
if(n == 1) return 1;
return fib(n - 2) + fib(n - 1);
}
And here's an example of a Fibonacci function in our Java-derived language that supports pattern matching:
int fib(0) {
return 1;
}
int fib(1) {
return 1;
}
int fib(int n) {
return fib(n - 2) + fib(n - 1);
}
What's the difference? The compiler implements branching for us.
What's the big deal? There isn't any. Someone noticed that a large number of functions contain very complicated switch statements (this is particularly true about functional programs) and decided that it's a good idea to abstract that away. We split the function definition into multiple ones, and put patterns in place of some arguments (sort of like overloading). When the function is called, the compiler compares the arguments with the definitions at runtime, and picks the correct one. This is usually done by picking the most specific definition available. For example, int fib(int n) can be called with n equal to 1, but it isn't because int fib(1) is more specific.
Pattern matching is usually more complex than our example reveals. For example, an advanced pattern matching system will allow us to do the following:
int f(int n < 10) { ... }
int f(int n) { ... }
When is pattern matching useful? In a surprisingly large number of cases! Every time you have a complex structure of nested ifs, pattern matching can do a better job with less code on your part. A good function that comes to mind is a standard WndProc function that all Win32 applications must provide (even though it's often abstracted away). Usually a pattern matching system can examine collections as well as simple values. For example, if you pass an array to your function you could pick out all arrays in which the first element is equal to 1 and the third element is greater than 3.
Another benefit of pattern matching is that if you need to add or modify conditions, you don't have to go into one huge function. You simply add (or modify) appropriate definitions. This eliminates the need for a whole range of design patterns from the GoF book. The more complex your conditions are, the more pattern matching will help you. Once you're used to it, you start wondering how you ever got through your day without it.
Closures
So far we've discussed features in the context of "pure" functional languages - languages that are implementations of lambda calculus and don't include features that conflict with Church's formalism. However, many of the features of functional languages are useful outside of lambda calculus framework. While an implementation of an axiomatic system is useful because it allows thinking about programs in terms of mathematical expressions, it may or may not always be practical. Many languages choose to incorporate functional elements without strictly adhering to functional doctrine. Many such languages (like Common Lisp) don't require variables to be final - you can modify things in place. They also don't require functions to depend only on their arguments - functions are allowed to access state outside of their boundaries. But they do include functional features - like higher order functions. Passing functions around in impure languages is a little bit different than doing it in the confines of lambda calculus and requires support for an interesting feature often referred to as lexical closure. Let's take a look at some sample code. Remember, in this case variables aren't final and functions can refer to variables outside of their scope:
Function makePowerFn(int power) {
int powerFn(int base) {
return pow(base, power);
}
return powerFn;
}
Function square = makePowerFn(2);
square(3); // returns 9
The function make-power-fn returns a function that takes a single argument and raises it to a certain power. What happens when we try to evaluate square(3)? The variable power isn't anywhere in scope of powerFn because makePowerFn has returned and its stack is long gone. How can square work, then? The language must, somehow, store the value of power somewhere for square to work. What if we create another function, cube, that raises something to the third power? The runtime must now store two copies of power, one for each function we generated using make-power-fn. The phenomenon of storing these values is called a closure. Closures don't only store arguments of a host function. For example, a closure can look like this:
Function makeIncrementer() {
int n = 0;
int increment() {
return ++n;
}
}
Function inc1 = makeIncrementer();
Function inc2 = makeIncrementer();
inc1(); // returns 1;
inc1(); // returns 2;
inc1(); // returns 3;
inc2(); // returns 1;
inc2(); // returns 2;
inc2(); // returns 3;
The runtime manages to store n, so incrementers can access it. Furthermore, it stores various copies, one for each incrementer, even though they're supposed to disappear when makeIncrementer returns. What does this code compile to? How do closures work behind the scenes? Fortunately, we have a back stage pass.
A little common sense goes a long way. The first observation is that local variables are no longer limited to simple scope rules and have an undefined lifetime. The obvious conclusion is that they're no longer stored on the stack - they must be stored on the heap instead8. A closure, then, is implemented just like a function we discussed earlier, except that it has an additional reference to the surrounding variables:
class some_function_t {
SymbolTable parentScope;
// ...
}
When a closure references a variable that's not in its local scope, it consults this reference for a parent scope. That's it! Closures bring functional and OO worlds closer together. Every time you create a class that holds some state and pass it to somewhere else, think of closures. A closure is just an object that creates "member variables" on the fly by grabbing them from the scope, so you don't have to!
What's next?
This article only scratches the surface of functional programming. Sometimes a small scratch can progress to something bigger and in our case it's a good thing. In the future I plan to write about category theory, monads, functional data structures, type systems in functional languages, functional concurrency, functional databases and much more. If I get to write (and in the process learn) about half of these topics my life will be complete. In the meantime, Google is our friend.
Comments?
If you have any questions, comments, or suggestions, please drop a note at coffeemug@gmail.com. I'll be glad to hear your feedback.
1When I was looking for a job in the fall of 2005 I often did ask this question. It's quite amusing how many blank stares I got. You would think that at about $300,000 a piece these people would at least have a good understanding of most tools available to them.
2This appears to be a controversial question. Physicists and mathematicians are forced to acknowledge that it isn't at all clear whether everything in the universe obeys the laws that can be described by mathematics.
3I've always hated history lessons that offer a dry chronology of dates, names, and events. To me history is about the lives of people who changed the world. It is about their private reasons behind their actions, and the mechanisms by which they affected millions of souls. For this reason this history section is hopelessly incomplete. Only very relevant people and events are discussed.
4When I was learning about functional programming I was very annoyed by the term "lambda" because I couldn't really understand what it really means. In this context lambda is a function, the single Greek letter was just easier to write in a mathematical notation. Every time you hear "lambda" when talking about functional programming just translate it in your mind to "function".
5Interestingly Java strings are immutable anyway. It's rather interesting to explore the reasons for this treachery, but that would distract us from our goal.
6Most functional language compilers optimize recursive functions by transforming them to their iterative alternatives whenever possible. This is called a tail call optimization.
7The opposite isn't always true. While it is sometimes possible to prove that two pieces of code are equivalent, it isn't possible in all situations.
8This is actually no slower than storing on the stack because once you introduce a garbage collector, memory allocation becomes an O(1) operation.
Programmers are procrastinators. Get in, get some coffee, check the mailbox, read the RSS feeds, read the news, check out latest articles on techie websites, browse through political discussions on the designated sections of the programming forums. Rinse and repeat to make sure nothing is missed. Go to lunch. Come back, stare at the IDE for a few minutes. Check the mailbox. Get some coffee. Before you know it, the day is over.
The only thing, every once in a while challenging articles actually do pop up. If you're looking at the right places you'll find at least one of these every couple of days. These articles are hard to get through and take some time, so they start piling up. Before you know it, you have a list of links and a folder full of PDF files and you wish you had a year in a small hut in the middle of the forest with nobody around for miles so you could catch up. Would be nice if someone came in every morning while you're taking a walk down the river to bring some food and take out the garbage.
I don't know about your list, but a large chunk of the articles in mine are about functional programming. These generally are the hardest to get through. Written in a dry academic language, even the "ten year Wall Street industry veterans" don't understand what functional programming (also referred to as FP) articles are all about. If you ask a project manager in Citi Group or in Deutsche Bank1 why they chose to use JMS instead of Erlang they'll say they can't use academic languages for industrial strength applications. The problem is, some of the most complex systems with the most rigid requirements are written using functional programming elements. Something doesn't add up.
It's true that FP articles and papers are hard to understand, but they don't have to be. The reasons for the knowledge gap are purely historical. There is nothing inherently hard about FP concepts. Consider this article "an accessible guide to FP", a bridge from our imperative minds into the world of FP. Grab a coffee and keep on reading. With any luck your coworkers will start making fun of you for your FP comments in no time.
So what is FP? How did it come about? Is it edible? If it's as useful as its advocates claim, why isn't it being used more often in the industry? Why is it that only people with PhDs tend to use it? Most importantly, why is it so damn hard to learn? What is all this closure, continuation, currying, lazy evaluation and no side effects business? How can it be used in projects that don't involve a university? Why does it seem to be so different from everything good, and holy, and dear to our imperative hearts? We'll clear this up very soon. Let's start with explaining the reasons for the huge gap between the real world and academic articles. The answer is as easy as taking a walk in the park.
A Walk In The Park
Fire up the time machine. Our walk in the park took place more than two thousand years ago, on a beautiful sunny day of a long forgotten spring in 380 B.C. Outside the city walls of Athens, under the pleasant shade of olive trees Plato was walking towards the Academy with a beautiful slave boy. The weather was lovely, the dinner was filling, and the conversation turned to philosophy.
"Look at these two students", said Plato carefully picking words to make the question educational. "Who do you think is taller?" The slave boy looked towards the basin of water where two men were standing. "They're about the same height", he said. "What do you mean 'about the same'?", asked Plato. "Well, they look the same from here but I'm sure if I were to get closer I'd see that there is some difference."
Plato smiled. He was leading the boy in the right direction. "So you would say that there is nothing perfectly equal in our world?" After some thinking the boy replied: "I don't think so. Everything is at least a little different, even if we can't see it." The point hit home! "Then if nothing is perfectly equal in this world, how do you think you understand the concept of 'perfect' equality?" The slave boy looked puzzled. "I don't know", he replied.
So was born the first attempt to understand the nature of mathematics. Plato suggested that everything in our world is just an approximation of perfection. He also realized that we understand the concept of perfection even though we never encountered it. He came to conclusion that perfect mathematical forms must live in another world and that we somehow know about them by having a connection to that "alternative" universe. It's fairly clear that there is no perfect circle that we can observe. But we also understand what a perfect circle is and can describe it via equations. What is mathematics, then? Why is the universe described with mathematical laws? Can all of the phenomena of our universe be described by mathematics?2
Philosophy of mathematics is a very complex subject. Like most philosophical disciplines it is far more adapt at posing questions rather than providing answers. Much of the consensus revolves around the fact that mathematics is really a puzzle: we set up a set of basic non-conflicting principles and a set of rules on how to operate with these principles. We can then stack these rules together to come up with more complex rules. Mathematicians call this method a "formal system" or a "calculus". We can effectively write a formal system for Tetris if we wanted to. In fact, a working implementation of Tetris is a formal system, just specified using an unusual representation.
A civilization of furry creatures on Alpha Centauri would not be able to read our formalisms of Tetris and circles because their only sensory input might be an organ that senses smells. They likely will never find out about the Tetris formalism, but they very well might have a formalism for circles. We probably wouldn't be able to read it because our sense of smell isn't that sophisticated, but once you get past the representation of the formalism (via various sensory instruments and standard code breaking techniques to understand the language), the concepts underneath are understandable to any intelligent civilization.
Interestingly if no intelligent civilization ever existed in the universe the formalisms for Tetris and circles would still hold water, it's just that nobody would be around to find out about them. If an intelligent civilization popped up, it would likely discover some formalisms that help describe the laws of our universe. They also would be very unlikely to ever find out about Tetris because there is nothing in the universe that resembles it. Tetris is one of countless examples of a formal system, a puzzle, that has nothing to do with the real world. We can't even be sure that natural numbers have full resemblance to the real world, after all one can easily think of a number so big that it cannot describe anything in our universe since it might actually turn out to be finite.
A Bit of History3
Let's shift gears in our time machine. This time we'll travel a lot closer, to the 1930s. The Great Depression was ravaging the New and the Old worlds. Almost every family from every social class was affected by the tremendous economic downturn. Very few sanctuaries remained where people were safe from the perils of poverty. Few people were fortunate enough to be in these sanctuaries, but they did exist. Our interest lies in mathematicians in Princeton University.
The new offices constructed in gothic style gave Princeton an aura of a safe haven. Logicians from all over the world were invited to Princeton to build out a new department. While most of America couldn't find a piece of bread for dinner, high ceilings, walls covered with elaborately carved wood, daily discussions by a cup of tea, and walks in the forest were some of the conditions in Princeton.
One mathematician living in such lavish lifestyle was a young man named Alonzo Church. Alonzo received a B.S. degree from Princeton and was persuaded to stay for graduate school. Alonzo felt the architecture was fancier than necessary. He rarely showed up to discuss mathematics with a cup of tea and he didn't enjoy the walks in the woods. Alonzo was a loner: he was most productive when working on his own. Nevertheless Alonzo had regular contacts with other Princeton inhabitants. Among them were Alan Turing, John von Neumann, and Kurt G枚del.
The four men were interested in formal systems. They didn't pay much heed to the physical world, they were interested in dealing with abstract mathematical puzzles instead. Their puzzles had something in common: the men were working on answering questions about computation. If we had machines that had infinite computational power, what problems would we be able to solve? Could we solve them automatically? Could some problems remain unsolved and why? Would various machines with different designs be equal in power?
In cooperation with other men Alonzo Church developed a formal system called lambda calculus. The system was essentially a programming language for one of these imaginary machines. It was based on functions that took other functions as parameters and returned functions as results. The function was identified by a Greek letter lambda, hence the system's name4. Using this formalism Alonzo was able to reason about many of the above questions and provide conclusive answers.
Independently of Alonzo Church, Alan Turing was performing similar work. He developed a different formalism (now referred to as the Turing machine), and used it to independently come to similar conclusions as Alonzo. Later it was shown that Turing machines and lambda calculus were equivalent in power.
This is where the story would stop, I'd wrap up the article, and you'd navigate to another page, if not for the beginning of World War II. The world was in flames. The U.S. Army and Navy used artillery more often than ever. In attempts to improve accuracy the Army employed a large group of mathematicians to continuously calculate differential equations required for solving ballistic firing tables. It was becoming obvious that the task was too great for being solved manually and various equipment was developed in order to overcome this problem. The first machine to solve ballistic tables was a Mark I built by IBM - it weighed five tons, had 750,000 parts and could do three operations per second.
The race, of course, wasn't over. In 1949 an Electronic Discrete Variable Automatic Computer (EDVAC) was unveiled and had tremendous success. It was a first example of von Neumann's architecture and was effectively a real world implementation of a Turing machine. For the time being Alonzo Church was out of luck.
In late 1950s an MIT professor John McCarthy (also a Princeton graduate) developed interest in Alonzo Church's work. In 1958 he unveiled a List Processing language (Lisp). Lisp was an implementation of Alonzo's lambda calculus that worked on von Neumann computers! Many computer scientists recognized the expressive power of Lisp. In 1973 a group of programmers at MIT's Artificial Intelligence Lab developed hardware they called a Lisp machine - effectively a native hardware implementation of Alonzo's lambda calculus!
Functional Programming
Functional programming is a practical implementation of Alonzo Church's ideas. Not all lambda calculus ideas transform to practice because lambda calculus was not designed to work under physical limitations. Therefore, like object oriented programming, functional programming is a set of ideas, not a set of strict guidelines. There are many functional programming languages, and most of them do many things very differently. In this article I will explain the most widely used ideas from functional languages using examples written in Java (yes, you could write functional programs in Java if you felt particularly masochistic). In the next couple of sections we'll take Java as is, and will make modifications to it to transform it into a useable functional language. Let's begin our quest.
Lambda calculus was designed to investigate problems related to calculation. Functional programming, therefore, primarily deals with calculation, and, surprisingly, uses functions to do so. A function is a very basic unit in functional programming. Functions are used for almost everything, even the simplest of calculations. Even variables are replaced with functions. In functional programming variables are simply aliases for expressions (so we don't have to type everything on one line). They cannot be modified. All variables can only be assigned to once. In Java terms this means that every single variable is declared as final (or const if we're dealing with C++). There are no non-final variables in FP.
final int i = 5;
final int j = i + 3;
Since every variable in FP is final two fairly interesting statements can be made. It does not make sense to always write the keyword final and it does not make sense to call variables, well... variables. We will now make two modifications to Java: every variable declared in our functional Java will be final by default, and we will refer to variables as symbols.
By now you are probably wondering how you could possibly write anything reasonably complicated in our newly created language. If every symbol is non-mutable we cannot change the state of anything! This isn't strictly true. When Alonzo was working on lambda calculus he wasn't interested in maintaining state over periods of time in order to modify it later. He was interested in performing operations on data (also commonly referred to as "calculating stuff"). However, it was proved that lambda calculus is equivalent to a Turing machine. It can do all the same things an imperative programming language can. How, then, can we achieve the same results?
It turns out that functional programs can keep state, except they don't use variables to do it. They use functions instead. The state is kept in function parameters, on the stack. If you want to keep state for a while and every now and then modify it, you write a recursive function. As an example, let's write a function that reverses a Java string. Remember, every variable we declare is final by default5.
String reverse(String arg) {
if(arg.length == 0) {
return arg;
}
else {
return reverse(arg.substring(1, arg.length)) + arg.substring(0, 1);
}
}
This function is slow because it repeatedly calls itself6. It's a memory hog because it repeatedly allocates objects. But it's functional in style. You may be interested why someone would want to program in this manner. Well, I was just about to tell you.
Benefits of FP
You're probably thinking that there's no way I can rationalize the monstrosity of a function above. When I was learning functional programming I was thinking that too. I was wrong. There are very good arguments for using this style. Some of them are subjective. For example, people claim that functional programs are easier to understand. I will leave out these arguments because every kid on the block knows that ease of understanding is in the eye of the beholder. Fortunately for me, there are plenty of objective arguments.
Unit Testing
Since every symbol in FP is final, no function can ever cause side effects. You can never modify things in place, nor can one function modify a value outside of its scope for another function to use (like a class member or a global variable). That means that the only effect of evaluating a function is its return value and the only thing that affects the return value of a function is its arguments.
This is a unit tester's wet dream. You can test every function in your program only worrying about its arguments. You don't have to worry about calling functions in the right order, or setting up external state properly. All you need to do is pass arguments that represent edge cases. If every function in your program passes unit tests you can be a lot more confident about quality of your software than if you were using an imperative language. In Java or C++ checking a return value of a function is not sufficient - it may modify external state that we would need to verify. Not so in a functional language.
Debugging
If a functional program doesn't behave the way you expect it to, debugging it is a breeze. You will always be able to reproduce your problem because a bug in a functional program doesn't depend on unrelated code paths that were executed before it. In an imperative program a bug resurfaces only some of the time. Because functions depend on external state produced by side effects from other functions you may have to go through a series of steps in no way related to the bug. In a functional program this isn't the case - if a return value of a function is wrong, it is always wrong, regardless of what code you execute before running the function.
Once you reproduce the problem, getting to the bottom of it is trivial. It is almost pleasant. You break the execution of your program and examine the stack. Every argument in every function call in the stack is available for your inspection, just like in an imperative program. Except in an imperative program that's not enough because functions depend on member variables, global variables, and the state of other classes (which in turn depend on these very same things). A function in a functional program depends only on its arguments, and that information is right before your eyes! Furthermore, in an imperative program examining a return value of a function will not give you a good idea of whether the function behaves properly. You need to hunt down dozens of objects outside its scope to verify that it performed correct actions. In a functional program all you have to do is look at the return value!
Walking through the stack you look at arguments passed to functions and their return values. The minute a return value doesn't make sense you step into the offending function and walk through it. You repeat this recursively until the process leads you to the source of the bug!
Concurrency
A functional program is ready for concurrency without any further modifications. You never have to worry about deadlocks and race conditions because you don't need to use locks! No piece of data in a functional program is modified twice by the same thread, let alone by two different threads. That means you can easily add threads without ever giving conventional problems that plague concurrency applications a second thought!
If this is the case, why doesn't anybody use functional programs for highly concurrent applications? Well, it turns out that they do. Ericsson designed a functional language called Erlang for use in its highly tolerant and scalable telecommunication switches. Many others recognized the benefits provided by Erlang and started using it. We're talking about telecommunication and traffic control systems that are far more scalable and reliable than typical systems designed on Wall Street. Actually, Erlang systems are not scalable and reliable. Java systems are. Erlang systems are simply rock solid.
The concurrency story doesn't stop here. If your application is inherently single threaded the compiler can still optimize functional programs to run on multiple CPUs. Take a look at the following code fragment:
String s1 = somewhatLongOperation1();
String s2 = somewhatLongOperation2();
String s3 = concatenate(s1, s2);
In a functional language the compiler could analyze the code, classify the functions that create strings s1 and s2 as potentially time consuming operations, and run them concurrently. This is impossible to do in an imperative language because each function may modify state outside of its scope and the function following it may depend on it. In functional languages automatic analysis of functions and finding good candidates for concurrent execution is as trivial as automatic inlining! In this sense functional style programs are "future proof" (as much as I hate buzzwords, I'll indulge this time). Hardware manufacturers can no longer make CPUs run any faster. Instead they increase the number of cores and attribute quadruple speed increases to concurrency. Of course they conveniently forget to mention that we get our money's worth only on software that deals with parallelizable problems. This is a very small fraction of imperative software but 100% of functional software because functional programs are all parallelizable out of the box.
Hot Code Deployment
In the old days of Windows in order to install updates it was necessary to restart the computer. Many times. After installing a newer version of a media players. With Windows XP the situation has improved significantly, yet it still isn't ideal (I ran Windows Update at work today and now an annoying system tray icon won't go away until I restart). Unix systems have had a better model for a while. In order to install an update you only need to stop relevant components, not the whole OS. While it is a better situation, for a large class of server applications it still isn't acceptable. Telecommunication systems need to be up 100% of the time because if dialing emergency is not available due to upgrades, lives may be lost. There is no reason Wall Street firms need to bring down their systems to install software updates over the weekend.
An ideal situation is updating relevant parts of the code without stopping any part of the system at all. In an imperative world this isn't possible. Consider unloading a Java class at runtime and reloading a new definition. If we were to do that every instance of a class would become unusable because the state it holds would be lost. We would need to resort to writing tricky version control code. We'd need to serialize all running instances of the class, destroy them, create instances of the new class, try to load serialized data into them hoping the loading code properly migrates the data to work with the new instance. On top of that, every time we change something we'd have to write our migration code manually. And our migration code would have to take special care not to break relationships between objects. Nice in theory, but would never work well in practice.
In a functional program all state is stored on the stack in the arguments passed to functions. This makes hot deployment significantly easier! In fact, all we'd really have to do is run a diff between the code in production and the new version, and deploy the new code. The rest could be done by language tools automatically! If you think this is science fiction, think again. Erlang engineers have been upgrading live systems without stopping them for years.
Machine Assisted Proofs and Optimizations
An interesting property of functional languages is that they can be reasoned about mathematically. Since a functional language is simply an implementation of a formal system, all mathematical operations that could be done on paper still apply to the programs written in that language. The compiler could, for example, convert pieces of code into equivalent but more efficient pieces with a mathematical proof that two pieces of code are equivalent7. Relational databases have been performing these optimizations for years. There is no reason the same techniques can't apply to regular software.
Additionally, you can use these techniques to prove that parts of your program are correct. It is even possible to create tools that analyze code and generate edge cases for unit tests automatically! This functionality is invaluable for rock solid systems. If you are designing pace makers and air traffic control systems such tools are almost always a requirement. If you are writing an application outside of truly mission critical industries, these tools can give you a tremendous edge over your competitors.
Higher Order Functions
I remember learning about the benefits I outlined above and thinking "that's all very nice but it's useless if I have to program in a crippled language where everything is final." This was a misconception. Making all variables final is crippled in a context of an imperative language like Java but it isn't in a context of functional languages. Functional languages offer a different kind of abstraction tools that make you forget you've ever liked modifying variables. One such tool is capability to work with higher order functions.
A function in such languages is different from a function in Java or C. It is a superset - it can do all the things a Java function can do, and more. We create a function in the same manner we do in C:
int add(int i, int j) {
return i + j;
}
This means something different from equivalent C code. Let's extend our Java compiler to support this notation. When we type something like this our compiler will convert it to the following Java code (don't forget, everything is final):
class add_function_t {
int add(int i, int j) {
return i + j;
}
}
add_function_t add = new add_function_t();
The symbol add isn't really a function. It is a small class with one function as its member. We can now pass add around in our code as an argument to other functions. We can assign it to another symbol. We can create instances of add_function_t at runtime and they will be garbage collected when we no longer need them. This makes functions first class objects no different from integers or strings. Functions that operate on other functions (accept them as arguments) are called higher order functions. Don't let this term intimidate you, it's no different from Java classes that operate on each other (we can pass class instances to other classes). We can call them "higher order classes" but nobody cares to because there is no strong academic community behind Java.
How, and when, do you use higher order functions? Well, I'm glad you asked. You write your program as a big monolithic blob of code without worrying about class hierarchies. When you see that a particular piece of code is repeated, you break it out into a function (fortunately they still teach this in schools). If you see that a piece of logic within your function needs to behave differently in different situations, you break it out into a higher order function. Confused? Here's a real life example from my work.
Suppose we have a piece of Java code that receives a message, transforms it in various ways, and forwards it to another server.
class MessageHandler {
void handleMessage(Message msg) {
// ...
msg.setClientCode("ABCD_123");
// ...
sendMessage(msg);
}
// ...
}
Now imagine that our system has changed and we now route messages to two servers instead of one. Everything is handled in exactly the same way except the client code - the second server wants it in a different format. How do we handle this situation? We could check where the message is headed and format the client code differently, like this:
class MessageHandler {
void handleMessage(Message msg) {
// ...
if(msg.getDestination().equals("server1") {
msg.setClientCode("ABCD_123");
} else {
msg.setClientCode("123_ABC");
}
// ...
sendMessage(msg);
}
// ...
}
This approach, however, isn't scalable. If more servers are added our function will grow linearly and we'll have a nightmare updating it. An object oriented approach is to make MessageHandler a base class and specialize the client code operation in derived classes:
abstract class MessageHandler {
void handleMessage(Message msg) {
// ...
msg.setClientCode(getClientCode());
// ...
sendMessage(msg);
}
abstract String getClientCode();
// ...
}
class MessageHandlerOne extends MessageHandler {
String getClientCode() {
return "ABCD_123";
}
}
class MessageHandlerTwo extends MessageHandler {
String getClientCode() {
return "123_ABCD";
}
}
We can now instantiate an appropriate class for each server. Adding servers becomes much more maintainable. That's a lot of code for such a simple modification though. We have to create two new types just to support different client codes! Now let's do the same thing in our language that supports higher order functions:
class MessageHandler {
void handleMessage(Message msg, Function getClientCode) {
// ...
Message msg1 = msg.setClientCode(getClientCode());
// ...
sendMessage(msg1);
}
// ...
}
String getClientCodeOne() {
return "ABCD_123";
}
String getClientCodeTwo() {
return "123_ABCD";
}
MessageHandler handler = new MessageHandler();
handler.handleMessage(someMsg, getClientCodeOne);
We've created no new types and no class hierarchy. We simply pass appropriate functions as a parameter. We've achieved the same thing as the object oriented counterpart with a number of advantages. We don't restrict ourselves to class hierarchies: we can pass new functions at runtime and change them at any time with a much higher degree of granularity with less code. Effectively the compiler has written object oriented "glue" code for us! In addition we get all the other benefits of FP. Of course the abstractions provided by functional languages don't stop here. Higher order functions are just the beginning.
Currying
Most people I've met have read the Design Patterns book by the Gang of Four. Any self respecting programmer will tell you that the book is language agnostic and the patterns apply to software engineering in general, regardless of which language you use. This is a noble claim. Unfortunately it is far removed from the truth.
Functional languages are extremely expressive. In a functional language one does not need design patterns because the language is likely so high level, you end up programming in concepts that eliminate design patterns all together. Once such pattern is an Adapter pattern (how is it different from Facade again? Sounds like somebody needed to fill more pages to satisfy their contract). It is eliminated once a language supports a technique called currying.
Adapter pattern is best known when applied to the "default" abstraction unit in Java - a class. In functional languages the pattern is applied to functions. The pattern takes an interface and transforms it to another interface someone else expects. Here's an example of an adapter pattern:
int pow(int i, int j);
int square(int i)
{
return pow(i, 2);
}
The code above adapts an interface of a function that raises an integer to an integer power to an interface of a function that squares an integer. In academic circles this trivial technique is called currying (after a logician Haskell Curry who performed mathematical acrobatics necessary to formalize it). Because in FP functions (as opposed to classes) are passed around as arguments, currying is used very often to adapt functions to an interface that someone else expects. Since the interface to functions is its arguments, currying is used to reduce the number of arguments (like in the example above).
Functional languages come with this technique built in. You don't need to manually create a function that wraps the original, functional languages will do that for you. As usual, let's extend our language to support this technique.
square = int pow(int i, 2);
This will automatically create a function square for us with one argument. It will call pow function with the second argument set to 2. This will get compiled to the following Java code:
class square_function_t {
int square(int i) {
return pow(i, 2);
}
}
square_function_t square = new square_function_t();
As you can see, we've simply created a wrapper for the original function. In FP currying is just that - a shortcut to quickly and easily create wrappers. You concentrate on your task, and the compiler writes the appropriate code for you! When do you use currying? This should be easy. Any time you'd like to use an adapter pattern (a wrapper).
Lazy Evaluation
Lazy (or delayed) evaluation is an interesting technique that becomes possible once we adopt a functional philosophy. We've already seen the following piece of code when we were talking about concurrency:
String s1 = somewhatLongOperation1();
String s2 = somewhatLongOperation2();
String s3 = concatenate(s1, s2);
In an imperative language the order of evaluation would be clear. Because each function may affect or depend on an external state it would be necessary to execute them in order: first somewhatLongOperation1, then somewhatLongOperation2, followed by concatenate. Not so in functional languages.
As we saw earlier somewhatLongOperation1 and somewhatLongOperation2 can be executed concurrently because we're guaranteed no function affects or depends on global state. But what if we don't want to run the two concurrently, do we need to run them in order? The answer is no. We only need to run these operations when another function depends on s1 and s2. We don't even have to run them before concatenate is called - we can delay their evaluation until they're required within concatenate. If we replace concatenate with a function that has a conditional and uses only one of its two parameters we may never evaluate one of the parameters at all! Haskell is an example of a delayed evaluation language. In Haskell you are not guaranteed that anything will be executed in order (or at all) because Haskell only executes code when it's required.
Lazy evaluation has numerous advantages as well as disadvantages. We will discuss the advantages here and will explain how to counter the disadvantages in the next section.
Optimization
Lazy evaluation provides a tremendous potential for optimizations. A lazy compiler thinks of functional code exactly as mathematicians think of an algebra expression - it can cancel things out and completely prevent execution, rearrange pieces of code for higher efficiency, even arrange code in a way that reduces errors, all guaranteeing optimizations won't break the code. This is the biggest benefit of representing programs strictly using formal primitives - code adheres to mathematical laws and can be reasoned about mathematically.
Abstracting Control Structures
Lazy evaluation provides a higher order of abstraction that allows implementing things in a way that would otherwise be impossible. For example consider implementing the following control structure:
unless(stock.isEuropean()) {
sendToSEC(stock);
}
We want sendToSEC executed unless the stock is European. How can we implement unless? Without lazy evaluation we'd need some form of a macro system, but in a language like Haskell that's unnecessary. We can implement unless as a function!
void unless(boolean condition, List code) {
if(!condition)
code;
}
Note that code is never evaluated if the condition is true. We cannot reproduce this behavior in a strict language because the arguments would be evaluated before unless is entered.
Infinite Data Structures
Lazy languages allow for definition of infinite data structures, something that's much more complicated in a strict language. For example, consider a list with Fibonacci numbers. We clearly can't compute and infinite list in a reasonable amount of time or store it in memory. In strict languages like Java we simply define a Fibonacci function that returns a particular member from the sequence. In a language like Haskell we can abstract it further and simply define an infinite list of Fibonacci numbers. Because the language is lazy, only the necessary parts of the list that are actually used by the program are ever evaluated. This allows for abstracting a lot of problems and looking at them from a higher level (for example, we can use list processing functions on an infinite list).
Disadvantages
Of course there ain't no such thing as a free lunch(tm). Lazy evaluation comes with a number of disadvantages. Mainly that it is, well, lazy. Many real world problems require strict evaluation. For example consider the following:
System.out.println("Please enter your name: ");
System.in.readLine();
In a lazy language you have no guarantee that the first line will be executed before the second! This means we can't do IO, can't use native functions in any meaningful way (because they need to be called in order since they depend on side effects), and can't interact with the outside world! If we were to introduce primitives that allow ordered code execution we'd lose the benefits of reasoning about our code mathematically (which would take all of the benefits of functional programming with it). Fortunately not all is lost. Mathematicians got to work and developed a number of tricks to ensure code gets executed in particular order in a functional setting. We get the best of both worlds! These techniques include continuations, monads, and uniqueness typing. In this article we'll only deal with continuations. We'll leave monads and uniqueness typing for another time. Interestingly, continuations are useful for many things other than enforcing a particular order of evaluation. We'll talk about that as well.
Continuations
Continuations to programming are what Da Vinci Code is to human history: an amazing revelation of the greatest cover-up known to man. Well, may be not, but they're certainly revealing of deceit in the same sense as square roots of negative numbers.
When we learned about functions we only learned half truths based on a faulty assumption that functions must return their value to the original caller. In this sense continuations are a generalization of functions. A function must not necessarily return to its caller and may return to any part of the program. A "continuation" is a parameter we may choose to pass to our function that specifies where the function should return. The description may be more complicated than it sounds. Take a look at the following code:
int i = add(5, 10);
int j = square(i);
The function add returns 15 to be assigned to i, the place where add was originally called. After that the value of i is used to call square. Note that a lazy compiler can't rearrange these lines of code because the second line depends on successful evaluation of the first. We can rewrite this code block using Continuation Passing Style or CPS, where the function add doesn't return to the original caller but instead returns its result to square.
int j = add(5, 10, square);
In this case add gets another parameter - a function that add must call with its result upon completion. In this case square is a continuation of add. In both cases j will equal 225.
Here lays the first trick to force a lazy language to evaluate two expressions in order. Consider the following (familiar) IO code:
System.out.println("Please enter your name: ");
System.in.readLine();
The two lines don't depend on each other and the compiler is free to rearrange them as it wishes. However, if we rewrite this code in CPS, there will be a dependency and the compiler will be forced to evaluate the two lines in order!
System.out.println("Please enter your name: ", System.in.readLine);
In this case println needs to call readLine with its result and return the result of readLine. This allows us to ensure that the two lines are executed in order and that readLine is evaluated at all (because the whole computation expects the last value as a result). In case of Java println returns void but if it were to return an abstract value (that readLine would accept), we'd solve our problem! Of course chaining function calls like that will quickly become unreadable, but it isn't necessary. We could add syntactic sugar to the language that will allow us to simply type expressions in order, and the compiler would chain the calls for us automatically. We can now evaluate expressions in any order we wish without losing any of the benefits of FP (including the ability to reason about our programs mathematically)! If this is still confusing, remember that a function is just an instance of a class with one member. Rewrite above two lines so that println and readLine are instances of classes and everything will become clear.
I would now wrap up this section, except that we've only scratched the surface of continuations and their usefulness. We can write entire programs in CPS, where every function takes an extra continuation argument and passes the result to it. We can also convert any program to CPS simply by treating functions as special cases of continuations (functions that always return to their caller). This conversion is trivial to do automatically (in fact, many compilers do just that).
Once we convert a program to CPS it becomes clear that every instruction has some continuation, a function it will call with the result, which in a regular program would be a place it must return to. Let's pick any instruction from above code, say add(5, 10). In a program written in CPS style it's clear what add's continuation is - it's a function that add calls once it's done. But what is it in a non-CPS program? We could, of course, convert the program to CPS, but do we have to?
It turns out that we don't. Look carefully at our CPS conversion. If you try to write a compiler for it and think about it long enough you'll realize that the CPS version needs no stack! No function ever "returns" in the traditional sense, it just calls another function with the result instead. We don't need to push function arguments on the stack with every call and then pop them back, we can simply store them in some block of memory and use a jump instruction instead. We'll never need the original arguments - they'll never be used again since no function ever returns!
So, programs written in CPS style have no stack but have an extra argument with a function to call. Programs not written in CPS style have no argument with a function to call, but have the stack instead. What does the stack contain? Simply the arguments, and a pointer to memory where the function should return. Do you see a light bulb? The stack simply contains continuation information! The pointer to the return instruction in the stack is essentially the same thing as the function to call in CPS programs! If you wanted to find out what continuation for add(5, 10) is, you'd simply have to examine the stack at the point of its execution!
So that was easy. A continuation and a pointer to the return instruction in the stack are really the same thing, only a continuation is passed explicitly, so that it doesn't need to be the same place where the function was called from. If you remember that a continuation is a function, and a function in our language is compiled to an instance of a class, you'll realize that a pointer to the return instruction in the stack and the continuation argument are really the same thing, since our function (just like an instance of a class) is simply a pointer. This means that at any given point in time in your program you can ask for a current continuation (which is simply the information on the stack).
Ok, so we know what a current continuation is. What does it mean? When we get a current continuation and store it somewhere, we end up storing the current state of our program - freezing it in time. This is similar to an OS putting itself into hibernation. A continuation object contains the information necessary to restart the program from the point where the continuation object was acquired. An operating system does this to your program all the time when it context switches between the threads. The only difference is that it keeps all the control. If you ask for a continuation object (in Scheme this is done by calling call-with-current-continuation function) you'll get an object that contains the current continuation - the stack (or in a CPS case the function to call next). You can store this object in a variable (or alternatively, on disk). When you choose to "restart" your program with this continuation object you will "transform" to the state of the program when you grabbed the continuation object. It's the same thing as switching back to a suspended thread or waking up an OS from hibernation, except you can do it again and again. When an OS wakes up, the hibernation information is destroyed. If it wasn't, you'd be able to wake up from the same point over and over again, almost like going back in time. You have that control with continuations!
In what situations are continuations useful? Usually when you're trying to simulate state in an application of inherently stateless nature to ease your life. A great application of continuations are web applications. Microsoft's ASP.NET goes to tremendous lengths to try and simulate state so that you can write your application with less hassle. If C# supported continuations half of ASP.NET's complexity would disappear - you'd simply store a continuation and restart it when a user makes the web request again. To a programmer of the web application there would be no interruption - the program would simply start from the next line! Continuations are an incredibly useful abstraction for some problems. Considering that many of the traditional fat clients are moving to the web, continuations will become more and more important in the future.
Pattern Matching
Pattern matching is not a new or innovative feature. In fact, it has little to do with functional programming. The only reason why it's usually attributed to FP is that functional languages have had pattern matching for some time, while modern imperative languages still don't.
Let's dive into pattern matching with an example. Here's a Fibonacci function in Java:
int fib(int n) {
if(n == 0) return 1;
if(n == 1) return 1;
return fib(n - 2) + fib(n - 1);
}
And here's an example of a Fibonacci function in our Java-derived language that supports pattern matching:
int fib(0) {
return 1;
}
int fib(1) {
return 1;
}
int fib(int n) {
return fib(n - 2) + fib(n - 1);
}
What's the difference? The compiler implements branching for us.
What's the big deal? There isn't any. Someone noticed that a large number of functions contain very complicated switch statements (this is particularly true about functional programs) and decided that it's a good idea to abstract that away. We split the function definition into multiple ones, and put patterns in place of some arguments (sort of like overloading). When the function is called, the compiler compares the arguments with the definitions at runtime, and picks the correct one. This is usually done by picking the most specific definition available. For example, int fib(int n) can be called with n equal to 1, but it isn't because int fib(1) is more specific.
Pattern matching is usually more complex than our example reveals. For example, an advanced pattern matching system will allow us to do the following:
int f(int n < 10) { ... }
int f(int n) { ... }
When is pattern matching useful? In a surprisingly large number of cases! Every time you have a complex structure of nested ifs, pattern matching can do a better job with less code on your part. A good function that comes to mind is a standard WndProc function that all Win32 applications must provide (even though it's often abstracted away). Usually a pattern matching system can examine collections as well as simple values. For example, if you pass an array to your function you could pick out all arrays in which the first element is equal to 1 and the third element is greater than 3.
Another benefit of pattern matching is that if you need to add or modify conditions, you don't have to go into one huge function. You simply add (or modify) appropriate definitions. This eliminates the need for a whole range of design patterns from the GoF book. The more complex your conditions are, the more pattern matching will help you. Once you're used to it, you start wondering how you ever got through your day without it.
Closures
So far we've discussed features in the context of "pure" functional languages - languages that are implementations of lambda calculus and don't include features that conflict with Church's formalism. However, many of the features of functional languages are useful outside of lambda calculus framework. While an implementation of an axiomatic system is useful because it allows thinking about programs in terms of mathematical expressions, it may or may not always be practical. Many languages choose to incorporate functional elements without strictly adhering to functional doctrine. Many such languages (like Common Lisp) don't require variables to be final - you can modify things in place. They also don't require functions to depend only on their arguments - functions are allowed to access state outside of their boundaries. But they do include functional features - like higher order functions. Passing functions around in impure languages is a little bit different than doing it in the confines of lambda calculus and requires support for an interesting feature often referred to as lexical closure. Let's take a look at some sample code. Remember, in this case variables aren't final and functions can refer to variables outside of their scope:
Function makePowerFn(int power) {
int powerFn(int base) {
return pow(base, power);
}
return powerFn;
}
Function square = makePowerFn(2);
square(3); // returns 9
The function make-power-fn returns a function that takes a single argument and raises it to a certain power. What happens when we try to evaluate square(3)? The variable power isn't anywhere in scope of powerFn because makePowerFn has returned and its stack is long gone. How can square work, then? The language must, somehow, store the value of power somewhere for square to work. What if we create another function, cube, that raises something to the third power? The runtime must now store two copies of power, one for each function we generated using make-power-fn. The phenomenon of storing these values is called a closure. Closures don't only store arguments of a host function. For example, a closure can look like this:
Function makeIncrementer() {
int n = 0;
int increment() {
return ++n;
}
}
Function inc1 = makeIncrementer();
Function inc2 = makeIncrementer();
inc1(); // returns 1;
inc1(); // returns 2;
inc1(); // returns 3;
inc2(); // returns 1;
inc2(); // returns 2;
inc2(); // returns 3;
The runtime manages to store n, so incrementers can access it. Furthermore, it stores various copies, one for each incrementer, even though they're supposed to disappear when makeIncrementer returns. What does this code compile to? How do closures work behind the scenes? Fortunately, we have a back stage pass.
A little common sense goes a long way. The first observation is that local variables are no longer limited to simple scope rules and have an undefined lifetime. The obvious conclusion is that they're no longer stored on the stack - they must be stored on the heap instead8. A closure, then, is implemented just like a function we discussed earlier, except that it has an additional reference to the surrounding variables:
class some_function_t {
SymbolTable parentScope;
// ...
}
When a closure references a variable that's not in its local scope, it consults this reference for a parent scope. That's it! Closures bring functional and OO worlds closer together. Every time you create a class that holds some state and pass it to somewhere else, think of closures. A closure is just an object that creates "member variables" on the fly by grabbing them from the scope, so you don't have to!
What's next?
This article only scratches the surface of functional programming. Sometimes a small scratch can progress to something bigger and in our case it's a good thing. In the future I plan to write about category theory, monads, functional data structures, type systems in functional languages, functional concurrency, functional databases and much more. If I get to write (and in the process learn) about half of these topics my life will be complete. In the meantime, Google is our friend.
Comments?
If you have any questions, comments, or suggestions, please drop a note at coffeemug@gmail.com. I'll be glad to hear your feedback.
1When I was looking for a job in the fall of 2005 I often did ask this question. It's quite amusing how many blank stares I got. You would think that at about $300,000 a piece these people would at least have a good understanding of most tools available to them.
2This appears to be a controversial question. Physicists and mathematicians are forced to acknowledge that it isn't at all clear whether everything in the universe obeys the laws that can be described by mathematics.
3I've always hated history lessons that offer a dry chronology of dates, names, and events. To me history is about the lives of people who changed the world. It is about their private reasons behind their actions, and the mechanisms by which they affected millions of souls. For this reason this history section is hopelessly incomplete. Only very relevant people and events are discussed.
4When I was learning about functional programming I was very annoyed by the term "lambda" because I couldn't really understand what it really means. In this context lambda is a function, the single Greek letter was just easier to write in a mathematical notation. Every time you hear "lambda" when talking about functional programming just translate it in your mind to "function".
5Interestingly Java strings are immutable anyway. It's rather interesting to explore the reasons for this treachery, but that would distract us from our goal.
6Most functional language compilers optimize recursive functions by transforming them to their iterative alternatives whenever possible. This is called a tail call optimization.
7The opposite isn't always true. While it is sometimes possible to prove that two pieces of code are equivalent, it isn't possible in all situations.
8This is actually no slower than storing on the stack because once you introduce a garbage collector, memory allocation becomes an O(1) operation.
Subscribe to:
Posts (Atom)