Saturday, April 3, 2010

SICP Series-chapter 1.3.1

In some languages, the function is a first class object.High order function means that you can use a function as a parameter or return value. Actually, high-order function has been existed in  many languages for long time, like Lisp,Python,Perl and Javascript. Now the C# also supports the high-order function and lambda expression. High order function is a powerful arm that makes your code more beautiful and shorter.
(defun cube (x)
 
(* x x x))
(defun sum (term a next b)
 
(if (> a b)
   
0
   
(+  (funcall term a)
       
(sum term (funcall next a) next b))))
(defun sum-integers (a b)
 
(sum #'identity a #'1+ b))
(defun pi-sum (a b)
 
(defun pi-term (x)
   
(/ 1.0 (* x (+ x 2))))
  (defun pi-next (x)
    (+ x 4))
  (sum #'pi-term a #'pi-next b))
(defun integral (f a b dx)
  (defun add-dx (x)
    (+ x dx))
  (* (sum f (+ a (/
dx 2.0)) #'add-dx b)
    dx
))
There is one thing that I have mentioned before. When we use a function as a parameter, you may need call the function in the function body. But you can’t just call it by placing the function name at the leftmost in a form. You need use funcall to tell the interpreter that this is a function.
1.29
(defun simpson-integral(f a b n)
 
(let ((h (float (/ (- b a) n))))
       (defun simpson-term(i)
              (let ((temp (funcall f (+ a (* h i)))))
                   (cond ((or (= i 0)
                              (= i n))
                          temp)
                         ((evenp i) (* 2 temp))
                         (t (* 4 temp)))))
       (* (/
h 3)
         
(sum #'simpson-term 0 #'1+ n))))



1.30
We have already meet this problem many time in previous posts, like the Fibonacci function we have implemented before.The basic algorithm is similar.
(defun sumv2(term a next b)
    (defun iter(a result)    
        (if (> a b)
            result
            (iter (funcall next a) (+ result (funcall term a)))))
(iter a 0))

1.31
It’s very easy to implement the product function, which is similar to sum function.
(defun product(term a next b)
 
(if (> a b)
     
1
     
(* (funcall term a)
         
(product term (funcall next a) next b))))
(defun productv2(term a next b)
 
(defun iter(a result)
         
(if (> a b)
             result
             
(iter (funcall next a) (* result (funcall term a)))))
 
(iter a 1))
(defun factorial(n)
 
(defun fact-aux(i)
         
(let ((deno 0)
             
(neo 0))
         
(cond ((evenp i) (progn (setq deno (+ i 1)) (setq neo (+ i 2))))
               
((oddp i) (progn (setq deno (+ i 2)) (setq neo (+ i 1)))))
         
(float (/ neo deno))))
 
(product #'fact-aux 1 #'1+ n))
1.32
(defun accumulator(combiner null-value term a next b)
 
(if (> a b)
     
null-value
     
(funcall combiner (funcall term a)
               
(combiner null-value term (funcall next a) next b))))
(defun accumulatorv2(combiner null-value term a next b)
 
(defun accumulator-iter (a null-value)
         
(if (> a b)
             
null-value
             
(accumulator-iter (funcall next a) (funcall combiner (funcall term a) null-value))))
 
(accumulator-iter a null-value))
(defun productv3(term a next b)
 
(accumulatorv2 #'* 1 term a next b))
Abstract is a very important concept in the programming. If you understand the core of the abstract, you get a powerful tool.Use the abstract, you can separate one complex function to many parts.Each part becomes easier and smaller. The powerful of Lisp makes this concept clear for many people.

SSH的生涯开始了

今天花了45元购买了一个半年的SSH帐号,速度很快,上了之后,发现世界是如此的美好。唯一遗憾的是,配了许久的AutoProxy,还是没有成功。不过还好,国外国内的网站都很快。

Friday, April 2, 2010

SharePoint Web Part and JavaScript--Add script to web part

ASP.NET controls are not limited to the server controls, you also can insert JavaScript in the ASPX page, just like we do in the HTML file. In order to improve user experiment in richer interface, JavaScript is your indispensable tool.In the Web 2.0 age, good user experiment is essential to your site. Users don’t like refreshing the pages again and again.You also can use the ASP.NET AJAX if you are on the MS side, but you also has other choices, like your own script or other three party library. In ASP.NET,
there is a ClientScript object in Page class, it manage the script on the page. We can use it to manage our scripts too.
Add a block of script
string ScriptKey = "myScript";
string EmbeddedScript ="<script language=javascript>function fun(){alert('hello world'); }</script> ";
if(!Page.IsClientScriptBlockRegistered(ScriptKey ))
    Page.RegisterClientScriptBlock(ScriptKey ,EmbeddedScript );

Link a external file
You also can link a external file to your web part. Linking a external file to the web part is very similar to embed a block of code to web part. First, you need put your file under the 12 hive folder, like layout. The only think you need notice is the location of your file. If you do not what doe the location look like in a SharePoint ASPX page, you can just open a page , right click, view source, find the default script’s location.
string ScriptKey = "myScript";
string EmbeddedScript ="<script language='javascript' src='your file'></script> ";
if(!Page.IsClientScriptBlockRegistered(ScriptKey ))
     Page.RegisterClientScriptBlock(ScriptKey ,EmbeddedScript );

Embed a file to DLL file
Sometimes, if you want to prevent your resources, such as images,css,Javascript, from being touched. You can embed them into a DLL. A ASP.NET provides the special URL of “WebResource.axd” to provide these files.
At first, you need add your script file to your project, then change the the Build Action like below:

Next, open the AssemblyInfo.cs file (usually appears under the ‘properties’ folder). In here we need to add a reference to the file and tell the compiler what it’s MIME type is. You can check the exactly location using the Reflector.

[assembly: WebResource("***.MyScript.js", "text/javascript")]
hen you add the script file like below.
string scriptUrl=this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "Location of your file"));
this.Page.ClientScript.RegisterClientScriptInclude("MyScript", scriptUrl)
The only issue that need to be mentioned about embedding the file to the DLL is the performance. Getting the file from the file system should be faster than exacting from the DLL file.

SICP Series-chapter 1.2.6

This section is about prime detection which is a old subject in the history of math.Even in the modern math,prime is still also a popular and interesting subject. But in a long time, number theory is considered as a pure math, which means there is no or little practice value.After entering the information age, the importance of security becomes more and more important.
Here is the code for fast-fermat:
(defun expmod(base e m)
    (cond ((= e 0) 1)
          ((evenp e)
           (rem (expt (expmod base (/ e 2) m) 2) m))
          (t
           (rem (* base (expmod base (- e 1) m)) m))))
 
(defun fermat-test(n)
    (defun try-it(a)
        (= (expmod a n n) a))
  (try-it (+ 1 (random (- n 1)))))
 
(defun fast-fermat(n times)
    (cond ((= times 0) T)
          ((fermat-test n) (fast-fermat n (- times 1)))
          (T NIL)))

1.21


1.22

1.25
Alyssa’s idea is wrong. If we calculate the number first, the number would be very huge, and program would become slow.
1.26
We can use the trace to monitor the process of a function.

1,27

 

我们需要什么样的爱国主义

爱国主义教育,历来都是小学,中学,大学教育中的重点,经过数十年的来的不断洗脑,成效显著。即使在今天资讯发达的今天,依然有许多人无法正确的将政党,政府与国家区分开来。改革开放以来,由于前三十年把中国人搞的半死不活,而面对着西方世界的各种思想与繁荣的经济,再大谈艰苦奋斗和马列主义,已经不合时宜。中国人虽然笨,但还没有笨到家,看到罪恶的美帝国主义的人们过的比我们这些长在红旗下的花朵还好,心里自然会有比较。八十年代中期,自由主义的思想开始在社会上流传,当一部分人先富起来的时候,另一部分人开始了思考。64,东欧剧变,苏联解体,统治中国人数十年来的意识形态轰然倒塌。再让中国人去卖命的理论基础已经不复存在了。然而多年以来的爱国主义教育放长线工程终于是开始收网的时候了。你可以不信任党,不信任政府,但是不能不爱国吧。多年来的洗脑和道德绑架,将无数的中国人绑在了爱国十字架上。

爱国主义,本身是无害的。在国家,民族遭受到危机的时候,爱国主义在帮助国家民族度过危难,团结民众的上可以起到很大的作用。可是天朝政府,利用爱国主义,煽动中国民众的爱国主义和民族主义情绪,四处树敌,今天是美国,明天是法国,后天是日本,用各种各样的事件来转移国内各种各样迫在眉睫的社会矛盾。08年奥运会前夕,奥运圣火在巴黎传递的时候的事件,使得国内民众有这么一股反法的情绪,原本只是企业和个人的行为,却被国内无良媒体鼓动,形成一股浪潮。而巴黎市长授予老和尚某称号与萨科奇见老和尚的事件,又被用来煽动民族情绪,似乎他们一见老和尚,中国版图就少了一块似的。在众多的假想敌之中,反美和反日是天朝政府最得心应手的。美国作为唯一的超级大国,长期以来是“亡我之心”不死,国内“反华势力”猖獗,似乎明天他们就要将自由女神搬到天安门。日本,由于历史原因,民众连煽动都不要。哪些反日的民众可能忘了,解放战争的时候,我们可是自相残杀了数年。六十年代初,全国饿死两千万人以上。

当爱国主义和民族主义变成了政府控制民众的一个手段之后,被绑在爱国十字架上的中国人就会不由自主的被政府操控,而那些不愿意被操控的人,就会被无数失去理智的中国人所唾弃,成为了爱国主义的牺牲品。一个例子,就是512时候王石的例子。做为一个国民,他并没有捐款的义务。他捐款,完全是因为他的怜悯同情之心,捐多捐少或是不捐,没有任何可以指责的地方。虽然说企业有自己的社会责任,要回馈社会,可王石的回馈是在一群失去理智的国人的压力下回馈的。可是大家都忘了一件事情,我们是有交税的。既然政府收了我们那么多的税,你就应当承担起责任,而不是还要号召民众捐款。捐款,有民间组织和慈善机构在做,不需要政府越俎代庖。而民众在悲伤与爱国的双重情感下,也失去理智,像疯狗一样的咬每一个名人。不捐少捐就是不爱国,不爱国就是卖国,就应该遭到所有人的唾弃与鄙视。这次西南干旱,中央只下拨了1.55亿,而给白俄的援助达十亿美金。团中央也作秀般的号召全国青少年捐款。而另外一个更为典型的例子,则是王千源的例子。在国内,她的家人受到许多“爱国青年”的恐吓,她以前的老底被挖出来,网络上对她是疯狂的谩骂,CCTV主页上称其为最丑陋的留学生。这些恶毒的言语和行为,就是那些自以为正义,标榜高尚的“爱国者”们做出来的。他们不听王千源的解释,不看王千源接受自由亚洲的采访,甚至不去思考这中间究竟出了什么事情,只是任由肾上腺素刺激他们的大脑,做出这一件件丑恶的事情。在西藏问题上,那些爱国青年连西藏都没有去过,不了解西藏历史,不清楚藏族文化,连藏民们想要什么都不知道,就凭一句“自古以来是中国的领土”,就像疯狗的一样咬人,用网络暴力去伤害他人,这样的爱国主义,不要也罢。

与许多国家的爱国主义不同,我们的爱国主义是天生的,你一出生,你就要感谢国家,无论这个国家给你带来了什么,不管是苦难还是幸福,你都要无条件的爱这个国家。我们常常淡忘我们做为个体的独立性,我们应当是一个天然的自由的个体,生于此,长于此,不能成为我应当爱这个国家的唯一理由。更为可笑的是,许多国人认为那些生在国外,长在国外的华人,也应当爱中国,甚于他生长的那片土地,而理由只不过因为他是华人。寻根溯源本没有错,可是这种毫无理由的绑架,如何能让人爱的起来。

中华民族的崛起,又是政府经常提的一个口号,用各种各样的幻象,让民众生活在制造出来的各种虚假的繁荣之中。让民众有一种不知名的所谓自豪感,既而生出一种所谓的爱国情怀,当社会矛盾不断凸现,就用这种爱国情感去冲淡他。让民众放弃自己的利益与自由,去成全所谓的国家强大。奥运会就是个很好的例子。记得有次看个视频,有个嘉宾,说了几句奥运会的坏话,另外一个嘉宾拍案而起,愤然离席,口中飙出了“两会”新闻稿。作为一个人,我们连反对奥运会的权利都被剥夺了。不仅仅被政府剥夺了,也被那些“爱国者”给剥夺了。

肯尼迪有句名言:“不要问国家可以为你做什么,你应该要问自己可以为国家做什么。”很多时候被“爱国者”们拿来教育那些“落后分子”。在这片神奇的土地上,我们应当问问自己,这个国家什么地方是值得我们自豪,值得我们爱的。

永远的未央歌与十亿个掌声

上周的时候,看了两场音乐会的视频,一个是邓丽君出道十五年的个人音乐会-“十亿个掌声”,另一个是由台湾中华音乐人交流协会主办民歌三十年纪念活动的“永远的未央歌”。什么时候,才能在这片近代文化荒年的土地上,什么时候才能有真正感动自己的歌?而不是“党啊,亲爱的母亲”这种下作的歌曲在横行。当看到叶佳修、苏来、李建复、邰肇玫、包美圣、施碧梧、齐豫、陈明韶、潘越云、许景淳、施孝荣、王海玲,侯德建、李泰祥,蔡琴  唱着那些几代人的歌曲,再想想春晚上的亚克西,你还能让我们说什么呢?

Thursday, April 1, 2010

SICP Series-chapter 1.2.4

The exercises of this section focus on the exponentiation, which you can use the recursive method, meanwhile you can also use the iterative method. In general, the technique of defining an invariant quantity that remains unchanged from state to state is a powerful way to think about the design of iterative algorithms.
1.16
The recursive version of fast-expt:
(defun fast-expt(b n)
    (cond ((= n 0) 1)
          ((evenp n) (expt (fast-expt b (/ n 1))
                            2))
          (t (* b (fast-expt b (- n 1))))))

The iterative version of fast-expt:
(defun fast-expt-v2(b n)
    (fast-expt-iter b n 1))
 
(defun fast-expt-iter(b n a)
    (cond ((= n 0) a)
          ((evenp n) (fast-expt-iter (* b b) (/ n 2) a))
          (t (fast-expt-iter b (- n 1) (* a b)))))

1.17
The recursive version of fast-add:
(defun double(x)
    (* 2 x))
(defun halve(x)
    (/ x 2))
 
(defun newmultip-rec(a b)
    (cond ((= b 0) 0)
          ((= b 1) a)
          ((evenp b) (double (newmultip-rec a (halve b))))
          (t (+ a (newmultip-rec a (- b 1))))))

The iterative version of fast-add:
(defun newmultip(a b &optional (addtion 0))
    (cond ((= b 0) addtion)
          ((evenp b) (newmultip (double a) (halve b) addtion))
          (t (newmultip a (- b 1) (+ a addtion)))))