Wednesday, April 7, 2010

Validation control prevent page from saving on SharePoint

If you have a web part containing some validation controls, when you Edit Page->Modify Shared Web Part->Save, the validation controls would prevent page being saved. You need fill all fields to complete the operation. There are two solution:

  1. Check the SPContext.Current.FormContext.FormMode == SPControlMode.Edit in you code, when the page mode is Edit, you can disable the validation controls. But there is still  a problem for this solution, if you Edit Page->Modify Shared Web Part->Save, then everything is fine. But if you Modify Shared Web Part->Save, the validation controls still work.
  2. The best method is using the ValidationGroup property. You can assign the your Validator controls and the controls that would trigger validation a same validation group name.Validation groups allow you to assign validation controls on a page to a specific category. Each validation group can be validated independently of other validation groups on the page. Use the ValidationGroup property to specify the name of the validation group for which the Button control causes validation when it posts back to the server.
  3. The third solution is not mine. I have not tried it yet. You can refer it

SICP Series-chapter 2.2.2

2.26

2.27

(defun deep-reverse(list)
    (cond ((null list) nil)
          ((not (listp list)) list)
          (t (append (deep-reverse (cdr list)) (list (deep-reverse (car list)))))))

2.28
(defun fringe(list)
    (cond ((null list) nil)
          ((not (consp list)) (list list))
          (t (append (fringe (car list))
                     (fringe (cdr list))))))


2.29

2.30
(defun square-tree(list)
  (cond ((null list) nil)
       
((not (listp list)) (list (* list list)))
       
(append (square-tree (car list)) (square-tree (cdr list)))))
(defun square-treev2(list)
 
(defun squarev2(x)
         
(cond ((null x) nil)
               
((not (listp list)) (list (* list list)))
               
(append (squarev2 (car list)) (squarev2 (cdr list)))))
 
(mapcar #'squarev2 list))
2.32
(defun subsets (s)
 
(if (null s)
   
(list nil)
   
(let ((rest (subsets (cdr s))))
     
(append
        rest
       
(mapcar (lambda (r)
                 
(cons (car s) r))
                rest
)))))


Monday, April 5, 2010

Building a simple compiler series(9)--Simple Context Free Grammar

Context free grammar using LISP
Peter Norvig has a great post about the LISP and Python, which compared the Python with LISP. In this article, he implements a context free grammar to generate random sentence using Lisp. I am interested in it, so I use Peter's most code to have a try.


SICP Series-chapter 2.2.1

List is one of the most important concepts in the LISP. There are already lots of function for manipulating the list in Lisp. But in the Schema, it only provides the most basic functions, like car, cdr. This textbook is based on the Schema, so there would be some exercises that are already implemented in the Common Lisp.
2.17
(defun last-pair(list)
    (if (null (cdr list))
        list
        (last-pair (cdr list))))
2.18
(defun jd-reverse(list)
    (if (null list)
        nil
        (append (jd-reverse (cdr list)) (list (car list)))))
2.20
(defun same-parity(item &rest others)
    (let ((temp (list item)))
        (dolist (other others)
            (if (= (rem item 2) (rem other 2))
                (setf temp (append temp (list other)))))
   temp))
2.21
In Common Lisp, there are a lot of mapping function.

(defun square-list(items)
    (if (null items)
        nil
        (cons (expt (car items) 2)
              (square-list (cdr items)))))
(mapcar #'square (list 1 2 3 4 5))
2.22
(defun for-each(f items)
    (dolist (item items)
        (funcall f item)))

Sunday, April 4, 2010

争取个人自由,便是争取国家自由

胡适在《介绍我自己的思想-《胡适文选》自序》中有这么一段话:“现在有人对你们说,‘牺牲你们个人的自由,去追求国家的自由’我要对你们说:‘争取你们个人的自由,便是为国家争自由!争你们自己的的人格,便是为国家争人格!自由平等的国家不是一群奴才建造起来的!’”
今天在Youtube上看了一部纪录片《八九点钟的太阳》,这是一部大概60分钟的记录文革始末的片子。从片子上,你看到的是无数的人,无数的红旗,无数的挥动着红宝书,踮着双脚,瞪着眼睛,扯着嗓子,红着眼圈的狂热的年轻人。四十多年前,这些人被虚幻的所谓的伟大革命理想,革命情怀所迷惑,鼓动,给这个国家和民族带来了数千年来少有的荒唐年代。个人的独立性不复存在,你的存在自由,与伟大的革命相比,显的是那么的微不足道。而从个人来讲,似乎也是一个正确的命题。覆巢之下,焉有完卵。唇亡齿寒的道理,被用在了国家与个人之间,只有国家强大了,个人才能过上幸福的生活。因此,为了国家的强大,个人常常会被要求放弃自己的自由与权利,去成全国家的自由与强大。这似乎是一个很说的过去的理由,除去战争年代这种特殊时期,如果在其他时期,政府,依然用国家主义和民族主义煽动民众,让民众放弃天然的权利与自由,即使依然有着包装起来很高尚的借口,这中间却是充满着阴谋与愚弄。政府常常将一些原本是政府的责任,用爱国主义和民族主义包装以后,让无数的民众去买单,而自己在收了民众无数的税之后,自己却做了一个看客。
在08年的时候,韩寒被网友指责“你怎么对得起你脚下自己的土地”,韩寒回答到“我没有自己的土地,你也没有自己的土地”。我们谈爱国,不能仅仅因为我们生长在这里,就成为了我们必须爱这个国家的唯一理由,这个理由是蛮横的,是不讲道理的,是强加在你的头上的。很多人会说,“是国家养育了你”。国家作为一个概念,他如何养育你?养育你的,是你的父母。这些山山水水,不因国家生,也不因国家亡。我们眷念的是故土,眷念的是故土上发生的点点滴滴。
有这么一句话,“人不为己,天诛地灭”。银幕上的反面形像常常将这句话挂在嘴边。如果从多年的教育来看,这是一句彻头彻尾的极端自私自利的话语。可这又是一句揭示人最真本性的一句话,人天生就是自私的,追求自身利益最大化。中国的历代文化,是以德治国,政治的清明,国家的强盛,是建立在统治阶级自身道德修养上的,是建立在那些海瑞,包拯之类的清官上的。可是人走茶凉,一朝天子一朝臣,人亡政息的剧情在中国历史的舞台上不断的上演。从一开始,我们就放错了着眼点。有了体制的保障,我们无所谓谁当政,没有了体制的保障,谁当政都有可能“一朝权在手,便把令来行”。
个人争取自己的自由与利益,就意味着自我意识的觉醒。每个人都是独一无二的个体,每个人都有着天然的不可分割的自由和权利。但由于在现代社会,我们有些天然的权利,因为现代文明,而不再受保障。但是我们依然有追求自由和幸福的权利。当个人知道了要追求自由和幸福的时候,就是个人对权力的监督的开始,对统治阶层的约束。个人的力量当然是微不足道的,当社会上越来越多的人,关注了自己的自由与权利,就渐渐形成了一股力量,推动了社会的前进,迫使政府做出符合大众的决策,就慢慢向满足民众需求的方向前进。当最大多数的民众的自由和权利都得到保障的时候,国家还会不自由吗?

SICP Series-chapter 1.3.3 and Chapter 1 review

In this section, the textbook focus on that make the function as return value.
The title for chapter 1 is Building Abstraction With Procedures, so procedure abstraction is the key on this chapter. If you are not familiar with some dynamic language like Python, JavaScript or Perl, you may feel surprised to see that we use the function as parameter or return value. Actually in some languages,function is called first-class object, which means function with the fewest restrictions, like the int, float. Some of the right of the first-class element are:

  • They may be named by variable
  • They may be passed as parameter
  • They may be returned as the result of procedures
  • They may be included in the data structure

Another important is the abstraction. In chapter one, we focus on the procedure, next chapter we will focus on the data abstraction.
Here is some functions that implemented in the textbook would be used in the exercise.

(defvar dx 0.00001)
 
(defun deriv(g)
    (lambda(x)
            (/ (- (funcall g (+ x dx)) (funcall g x))
               dx)))
 
(defun Newton-transform(g)
    (lambda(x)
           (- x (/ (funcall g x) (funcall (deriv g) x)))))
 
(defun newton-methond(g guess)
    (fixed-point (Newton-transform g) guess))
1.40
(defun cubic(a b c)
  (lambda(x) (+ (expt x 3)
                (* a (expt x 2))
                (* b x)
                c)))
1.42
(defun compose(g f)
    (lambda(x)
           (funcall g (funcall f x))))
1.43
(defun repeated(f n)
    (if (= n 0)
        f
        (compose f (repeated f (- n 1)))))
1.44
(defun smooth(f)
    (lambda(x)
        (/ (+ (funcall f x)
              (funcall f (+ x dx))
              (funcall f (- x dx)))
           3)))
 
(defun smoothN(f n)
    (funcall (repeated #'smooth n) f))

Saturday, April 3, 2010

SICP Series-chapter 1.3.3

Actually, I don't have much to say in this section, everything is straight. The code and screen shot make thing clear.
Here is the example on the textbook about fixed-point.
(defvar tolerance 0.00000001)
 
(defun average(x y)
    (/ (+ x y) 2))
 
(defun fixed-point (f first-guess)
    (defun close-enough(x y)
        (< (abs (- x y)) tolerance))
    (defun try(guess)
        (let ((next (funcall f guess)))
             (if (close-enough guess next)
                  next
                 (try next))))
    (try first-guess))
 
(defun f-sqrt(x)
    (fixed-point (lambda(y)(average y (/ x y))) 1.0))
1.35
(defun golden-ratio()
    (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0))


1.36
(defun fixed-pointv2 (f first-guess)
    (defun close-enough(x y)
        (< (abs (- x y)) tolerance))
    (defun try(guess)
        (let ((next (funcall f guess)))
             (if (close-enough guess next)
             next
            (progn
                (format t "The number is ~A~%" next)
                (try next)))))
    (try first-guess))
 
(defun log-fixed()
    (fixed-pointv2 (lambda(x)(/ (log 1000) (log x))) 2))

1.37
(defun cont-frac(n d k)
 
(defun cont-frac-aux(i)
         
(if (= i k)
             
0
             
(/ (funcall n i)
                (+ (funcall d i)
                   (cont-frac-aux (+ i 1))))))
  (cont-frac-aux 1))
(defun cont-fracv2(n d k)
  (defun cont-frac-iter(i result)
         (if (= i 0)
             result
             (cont-frac-iter (- i 1)
                             (/
(funcall n i)
                               
(+ (funcall d i)
                                   result
)))))
 
(cont-frac-iter k 0))

1.38
(defun Euler-e(i)
    (cont-fracv2 (lambda(x) 1.0)
                 (lambda(x)
                            (if (= 0 (rem (+ x 1) 3))
                                (* 2 (/ (+ x 1) 3))
                                1))
                 i))