현재 시각을 컨솔에 보여주는 간단한 애플리케이션의 Python 언어 소스 코드이다.
UTC란 1970년 1월 1일 0시 0분 0초를 기준으로 하여 경과된 초 단위의 총 시간을 의미한다.
* UTC(Universal Time Coordinated, 협정세계시, 協定世界時)

 

Common Lisp 언어에서 (get-universal-time) 하여 얻은 값은 UTC(1970년 1월 1일 0시 0분 0초를 기준으로 하여 경과된 초)가 아니라, 1900년 1월 1일 0시 0분 0초를 기준으로 하여 경과된 초이다. 따라서 이 값을 기준으로 UTC를 구하려면 1900년 1월 1일 0시 0분 0초부터 1970년 1월 1일 0시 0분 0초까지 경과된 초를 구하여 빼 주어야 나온다. 그런데 Common Lisp 언어에서는 GMT 시각값을 초로 환산하여 보정해주어야 제대로 된 UTC 값을 구할 수 있다.


GMT 시각값을 초로 환산한 값이 (* (nth 8 cNow) 60 60) 이고, 이 만큼 보정하는 과정이 아래 소스의 20째 줄

      (setf utctime (+ (- univ-time *base*) (- *base2* *base*) (* (nth 8 cNow) 60 60)))

이다. 여기서 (* (nth 8 cNow) 는 GMT 시각값의 반대 부호이다.

 

  1. #!/usr/bin/env clisp
  2. ;;  Filename: testCTime.lsp
  3. ;;
  4. ;;  Execute: clisp testCTime.lsp
  5. ;; Common Lisp case
  6. (setf *weekNames* (list "월" "화" "수" "목" "금" "토" "일"))
  7. (setf univ-time (get-universal-time))
  8. (setf cNow (multiple-value-list (decode-universal-time univ-time)))
  9. (setq *base* (encode-universal-time 0 0 0 1 1 1971))
  10. (setq *base2* (encode-universal-time 0 0 0 1 1 1972))
  11. (setq *takeoff* (encode-universal-time 0 0 0 1 1 2013))
  12. (setq *takeoff2* (encode-universal-time 0 0 0 2 1 2013))
  13. (setq *one-day-secs* (- *takeoff2* *takeoff*))
  14. (setq *secs* univ-time)
  15. (setq *daysecs* (- *secs* *takeoff*))
  16. (setf utctime (+ (- univ-time *base*) (- *base2* *base*) (* (nth 8 cNow) 60 60)))
  17. (format t "UTC: ~D초~%" utctime)
  18. ;; 현재 시각 표시: 200x년 x월 xx일 (x요일) xx시 xx분 xx초 (GMT nn)
  19. (format t "~D년 ~D월 ~D일 (~A요일) ~D시 ~D분 ~D초 (GMT~@D)~%"
  20.         (nth 5 cNow)
  21.         (nth 4 cNow)
  22.         (nth 3 cNow)
  23.         (nth (nth 6 cNow) *weekNames*)
  24.         (nth 2 cNow)
  25.         (nth 1 cNow)
  26.         (nth 0 cNow)
  27.         (- (nth 8 cNow)) )
  28. ;; 1월 1일은 1, 1월 2일은 2
  29. (setf strIsDST (if (equal (nth 7 cNow) nil)
  30.                     "안함"
  31.                     "함" ) )
  32. (setq *days* (1+ (floor *daysecs* *one-day-secs*)))
  33. (format t "올해 몇 번째 날: ~D, 서머타임 적용 여부: ~A~%" *days* strIsDST)



실행> clisp testCTime.lsp
UTC: 1378044909초
2013년 9월 1일 (일요일) 23시 15분 9초 (GMT+9)
올해 몇 번째 날: 244, 서머타임 적용 여부: 안함



 

Posted by Scripter
,

다항식 p(x) 를 1차 다항식 x - a 로 나눌 때의 몫과 나머지를 구하는 조립제법을
Common Lisp 언어로 구현해 보았다. 조립제법은 일명 Horner의 방법이라고도 불리우는데, 이는 x = a 에서 다항식 p(x)의 값 p(a)을 계산하는 가장 빠른 알고리즘이기도 하다.

         p(x) = (x - a)q(x) + r

여기서 r은 나머지이며 r = p(a) 이다. 또 q(x)는 몫이다.

[참고]
    * 온라인으로 조립제법 표 만들기 손으로 계산하는 조립제법 표 
    * 온라인으로 구하는 다항식의 도함수: 조립제법을 이용한 다항식의 도함수


아래의 소스파일은 Python용 소스파일 testSyntheticDivision.py를 Common Lisp용으로 수정한 것이다. 

실행은 CLisp을 사용한다.


  1. #!/usr/bin/env clisp
  2. ;;  Filename: testSyntheticDivision.lsp
  3. ;;
  4. ;;  Purpose:  Find the quotient and remainder when some polynomial is
  5. ;;            divided by a monic polynomial of the first degree.
  6. ;;
  7. ;;  Execute:  clisp testSyntheticDivision.lsp -2 1 3 3 1
  8. ;;
  9. ;;  Date: 201. 8. 31.
  10. ;; 사용법 표시
  11. (defun printUsage()
  12.     (format t "사용법: python testSyntheticDivision.py [수] [피제식의 계수들]~%")
  13.     (format t "조립제법(synthetic method)에 의한 다항식 나눗셈 결과를 보여준다.~%"))
  14. ;; 스트링을 부동소수점수로 변환하는 함수
  15. (defun parse-number (v)
  16.      (with-input-from-string (s v) (read s)))
  17. ;; 부동소수점수의 표현이 .0 으로 끝나는 경우 이를 잘라낸다.
  18. ;; 전체 문자열 표시 너비는 매개변수 width 로 전달받아 처리한다.
  19. (defun simplify(v width)
  20.     (setf tt (format nil "~F" v))
  21.     (setf tlen (length tt))
  22.     (if (> tlen 1) (progn
  23.         (if (equal (substring tt (- tlen 2) tlen) ".0") (progn
  24.             (setf tt (substring tt 0 (- tlen 2)))
  25.             (setf tlen (length tt))
  26.             )
  27.           )
  28.     ))
  29.     (if (not (equal width nil)) (progn
  30.         (if (< tlen width) (progn
  31.             (setf tt (concatenate 'string (substring "              " 0 (- width tlen)) tt)) ))
  32.         )
  33.     )
  34.     tt )
  35. ;; 다항식을 내림차순의 스트링 표현으로 반환
  36. (defun toPolyString(c)
  37.     (setf ttt "")
  38.     (setf sc0 (simplify (nth 0 c) nil ))
  39.     (setf sc0 (simplify (nth 0 c) nil ))
  40.     (if (> (length c) 2)
  41.         (cond
  42.            ((equal sc0 "1") (setf ttt (concatenate 'string ttt (format nil "x^~D" (- (length c) 1)))))
  43.            ((equal sc0 "-1") (setf ttt (concatenate 'string ttt (format nil "-x^~D" (- (length c) 1)))))
  44.            (t (setf ttt (concatenate 'string ttt sc0 (format nil " x^~D" (- (length c) 1)))))
  45.         )
  46.     )
  47.     (if (= (length c) 2)
  48.         (cond
  49.            ((equal sc0 "1") (setf ttt (concatenate 'string ttt "x")))
  50.            ((equal sc0 "-1") (setf ttt (concatenate 'string ttt "-x")))
  51.            (t (setf ttt (concatenate 'string ttt sc0 " x")))
  52.         )
  53.     )
  54.     (if (= (length c) 1)
  55.            (setf ttt (concatenate 'string ttt sc0))
  56.     )
  57.     (loop for i from 1 below (length c) do
  58.         (setf k (- (length c) 1 i))
  59.         (setf sc (simplify (nth i c) nil))
  60.         (if (> k 1) (progn
  61.             (cond
  62.                 ((> (nth i c) 0.0)
  63.                     (if (equal sc "1")
  64.                         (setf ttt (concatenate 'string ttt " + " (format nil "x^~D" k)))
  65.                         (setf ttt (concatenate 'string ttt " + " sc (format nil " x^~D" k)))
  66.                     ) )
  67.                 ((< (nth i c) 0.0)
  68.                     (if (equal sc "-1")
  69.                         (setf ttt (concatenate 'string ttt " - " (format nil "x^~D" k)))
  70.                         (setf ttt (concatenate 'string ttt " - " (simplify (abs (nth i c)) nil) (format nil " x^~D" k)))
  71.                     ) )
  72.                 )
  73.             )
  74.         )
  75.         (if (= k 1)
  76.             (cond
  77.                 ((> (nth i c) 0.0)
  78.                     (if (equal sc "1")
  79.                         (setf ttt (concatenate 'string ttt " + " "x"))
  80.                         (setf ttt (concatenate 'string ttt " + " sc " x"))
  81.                     ) )
  82.                 ((< (nth i c) 0.0)
  83.                     (if (equal sc "-1")
  84.                         (setf ttt (concatenate 'string ttt " - " "x"))
  85.                         (setf ttt (concatenate 'string ttt " - " (simplify (abs (nth i c)) nil) " x"))
  86.                     ) )
  87.             )
  88.         )
  89.         (if (= k 0)
  90.             (cond
  91.                 ( (> (nth i c) 0.0) (setf ttt (concatenate 'string ttt " + " sc)) )
  92.                 ( (< (nth i c) 0.0) (setf ttt (concatenate 'string ttt " - " (simplify (abs (nth i c)) nil) )) )
  93.             )
  94.         )
  95.     )
  96. ;; 다항식 나눗셈 결과를
  97. ;;     (피제식) = (제식)(몫) + (나머지)
  98. ;; 형태로 출력
  99. (defun printDivisionResult(a c b)
  100.     (setf strLine (concatenate 'string "  " (toPolyString c)))
  101.     (format t "~A~%" strLine)
  102.     (setf strLine (concatenate 'string "    = ( " (toPolyString (list 1.0 (- a))) " )"))
  103.     (setf tmp (make-list (- (length b) 1) :initial-element 0.0)) 
  104.     (loop for i from 0 below (length tmp) do
  105.         (setf (nth i tmp) (nth i b)) )
  106.     (setf strLine (concatenate 'string strLine "( " (toPolyString tmp) " )"))
  107.     (setf r (nth (- (length b) 1) b))
  108.     (cond
  109.        ((> r 0.0)  (setf strLine (concatenate 'string strLine " + " (simplify r nil) )))
  110.        ((< r 0.0)  (setf strLine (concatenate 'string strLine " - " (simplify (abs r) nil) )))
  111.     )
  112.     (format t "~A~%" strLine)
  113. )
  114. ;; 조립제법 계산표 출력 함수
  115. (defun printSyntheticTable(a c s q)
  116.     (setf strLine "       | ")
  117.     (setf strLine (concatenate 'string strLine (simplify (nth 0 c) 6) ))
  118.     (loop for i from 1 below (length c) do
  119.         (setf strLine (concatenate 'string strLine "  " (simplify (nth i c) 6) )))
  120.     (format t "~A~%" strLine)
  121.     (setf strLine (concatenate 'string (simplify a 6) " |"))
  122.     (setf strLine (concatenate 'string strLine "         "))
  123.     (setf strLine (concatenate 'string strLine (simplify (nth 1 s) 6)))
  124.     (loop for i from 2 below (length s) do
  125.         (setf strLine (concatenate 'string strLine "  " (simplify (nth i s) 6) )))
  126.     (format t "~A~%" strLine)
  127.     (setf strLine "       |")
  128.     (loop for i from 0 below (length q) do
  129.         (setf strLine (concatenate 'string strLine "--------")))
  130.     (format t "~A~%" strLine)
  131.     (setf strLine "         ")
  132.     (setf strLine (concatenate 'string strLine (simplify (nth 0 q) 6)))
  133.     (loop for i from 1 below (length q) do
  134.         (setf strLine (concatenate 'string strLine "  " (simplify (nth i q) 6) )))
  135.     (format t "~A~%" strLine)
  136. )
  137. ;; 실행 시작 지점
  138. (if (< (length ext:*args*) 3) (progn
  139.     (printUsage)
  140.     (quit) ))
  141. ;; ----------------------------------------------------
  142. ;; 피제식은 c_0 x^n +  c_1 x^(n -1) + ... + c_n
  143. ;; 제식은 x -  a
  144. (setf a (parse-number (nth 0 ext:*args*)) )
  145. (setf c (make-list (- (length ext:*args*) 1) :initial-element 0.0) )
  146. (setf s (make-list (- (length ext:*args*) 1) :initial-element 0.0) )
  147. (setf b (make-list (- (length ext:*args*) 1) :initial-element 0.0) )
  148. (loop for i from 0 below (length c) do
  149.     (setf (nth i c) (parse-number (nth (+ i 1) ext:*args*))))
  150. ;; ----------------------------------------------------
  151. ;; 조립제법의 주요 부분
  152. (setf (nth 0 s) 0.0)
  153. (setf (nth 0 b) (nth 0 c))
  154. (loop for i from 1 below (length c) do
  155.     (setf (nth i s) (* (nth (- i 1) b) a))
  156.     (setf (nth i b) (+ (nth i c) (nth i s))) )
  157. ;; ----------------------------------------------------
  158. ;; 몫의 계수와 나머지를 출력한다.
  159. (format t "몫의 계수는 ")
  160. (loop for i from 0 below (- (length b) 2) do
  161.     (format t (concatenate 'string (simplify (nth i b) nil) ",  ")))
  162. (format t (concatenate 'string (simplify (nth (- (length b) 2) b) nil) " "))
  163. (format t (concatenate 'string "이고, 나머지는 " (simplify (nth (- (length b) 1) b) nil) " 이다.~%"))
  164. (terpri)
  165. ;; ----------------------------------------------------
  166. ;; 조립제법 표를 출력한다.
  167. (printSyntheticTable a c s b)
  168. (terpri)
  169. ;; ----------------------------------------------------
  170. ;; (피제식) = (제식) x (몫) + (나머지)
  171. (printDivisionResult a c b)




실행> clisp testSyntheticDivision.lsp 1 2 3 4 5
몫의 계수는 2, 5, 9 이고, 나머지는 14 이다.

       |      2       3       4       5
     1 |              2       5       9
       |---------------------------------
              2       5       9      14

  2 x^3 + 3 x^2 + 4 x + 5
    = ( x - 1 )( 2 x^2 + 5 x + 9 ) + 14



 

Posted by Scripter
,

다음은 Python용 소스파일 testForFor.py를 Common Lisp용으로 수정한 것이다.
Common Lisp 언어에서 format 함수로 출력할 때 ~% 는 개행문자를 의미한다.

Python 언어에서 쓰이는 조건 분기 구문

        if 조건식1:
            블럭1
        elif 조건식2:
            블럭2
        elif 조건식3:
            블럭3
        else:
            블럭4

에 해딩하는 Common Lisp 언어의 구문은

        (cond
            ((조건식1) 블럭1) 
            (조건식2) 블럭2) 
            (조건식3) 블럭3)
            (t 블럭4) )


이다. Comon Lisp 언어에서 t와 nil은 각각 Java 언어의 true와 false에 해당하는 불리란 타입의 상수이다. 그러므로 Comon Lisp 언어에서 t와 nil을 변수명으로 사용할 수 없다. 

  1. #!/use/bin/env clisp
  2. ;; Filename: testForFor.lsp
  3. ;;
  4. ;; Execute: clisp testForFor.lsp
  5. ;;
  6. ;; Date: 20013. 8. 30.
  7. (defun getDan(dan)
  8.     (setf tt (make-list 19 :initial-element "" )
  9.     (loop for j from 0 below 19 do
  10.         (setf sa (format nil "~D" dan))
  11.         (if (< (length sa) 2)
  12.             (setf sa (format nil " ~D" dan)))
  13.         (setf sb (format nil "~D" (+ j 1)))
  14.         (if (< (length sb) 2)
  15.             (setf sb (format nil " ~D" (+ j 1))))
  16.         (setf sval (format nil "~D" (* dan (+ j 1))))
  17.         (cond ((< (length sval) 2) (setf sval (format nil "  ~D" (* dan (+ j 1)))))
  18.               ((< (length sval) 3) (setf sval (format nil " ~D" (* dan (+ j 1))))))
  19.         (setf (nth j tt) (format nil "~A x ~A = ~A" sa sb sval )))
  20.     tt )
  21. ;; 19단표를 모두 80컬럼 컨솔에 출력한다.
  22. (defun printAllNineteenDan()
  23.     (setf arr (make-list 18 :initial-element '() ))
  24.     (loop for j from 0 below 18 do
  25.         (setf (nth j arr) (make-list 19 :initial-element '() )))
  26.     (loop for i from 2 below 20 do
  27.         (setf (nth (- i 2) arr) (getDan i)))
  28.     ;; Common Lisp 소스 코드에서도 배열 대신 리스트를 사용한다.
  29.     (setf d (list 2 7 11 16))        ;; 각 줄단위 블럭의 첫단
  30.     (setf counter (list 5 4 5 4 ))   ;; 각 줄단위 블럭에 속한 단의 개수
  31.     (setf lines (make-list 19 :initial-element "" ))
  32.     (loop for k from 0 below (length d) do
  33.         ;; 8-바이트 길이의 한 줄씩 완성
  34.         (loop for i from 0 below 19 do
  35.             (setf (nth i lines) (nth i (nth (- (nth k d) 2) arr)))
  36.             (loop for j from 1 below (nth k counter) do
  37.                 (setf (nth i lines) (concatenate  'string (nth i lines) "   " (nth i (nth (+ (- (nth k d) 2) j) arr)))) ))
  38.         ;; 80 바이트 길이의 한 줄씩 출력
  39.         (loop for i from 0 below 19 do
  40.             (format t "~A~%" (nth i lines)))
  41.         (terpri) ))    ;; same to (format t "~%") ))
  42. (terpri)    ;; (format t "~%")
  43. (printAllNineteenDan)


실행> clisp ForFor.sp

 2 x  1 =   2    3 x  1 =   3    4 x  1 =   4    5 x  1 =   5    6 x  1 =   6
 2 x  2 =   4    3 x  2 =   6    4 x  2 =   8    5 x  2 =  10    6 x  2 =  12
 2 x  3 =   6    3 x  3 =   9    4 x  3 =  12    5 x  3 =  15    6 x  3 =  18
 2 x  4 =   8    3 x  4 =  12    4 x  4 =  16    5 x  4 =  20    6 x  4 =  24
 2 x  5 =  10    3 x  5 =  15    4 x  5 =  20    5 x  5 =  25    6 x  5 =  30
 2 x  6 =  12    3 x  6 =  18    4 x  6 =  24    5 x  6 =  30    6 x  6 =  36
 2 x  7 =  14    3 x  7 =  21    4 x  7 =  28    5 x  7 =  35    6 x  7 =  42
 2 x  8 =  16    3 x  8 =  24    4 x  8 =  32    5 x  8 =  40    6 x  8 =  48
 2 x  9 =  18    3 x  9 =  27    4 x  9 =  36    5 x  9 =  45    6 x  9 =  54
 2 x 10 =  20    3 x 10 =  30    4 x 10 =  40    5 x 10 =  50    6 x 10 =  60
 2 x 11 =  22    3 x 11 =  33    4 x 11 =  44    5 x 11 =  55    6 x 11 =  66
 2 x 12 =  24    3 x 12 =  36    4 x 12 =  48    5 x 12 =  60    6 x 12 =  72
 2 x 13 =  26    3 x 13 =  39    4 x 13 =  52    5 x 13 =  65    6 x 13 =  78
 2 x 14 =  28    3 x 14 =  42    4 x 14 =  56    5 x 14 =  70    6 x 14 =  84
 2 x 15 =  30    3 x 15 =  45    4 x 15 =  60    5 x 15 =  75    6 x 15 =  90
 2 x 16 =  32    3 x 16 =  48    4 x 16 =  64    5 x 16 =  80    6 x 16 =  96
 2 x 17 =  34    3 x 17 =  51    4 x 17 =  68    5 x 17 =  85    6 x 17 = 102
 2 x 18 =  36    3 x 18 =  54    4 x 18 =  72    5 x 18 =  90    6 x 18 = 108
 2 x 19 =  38    3 x 19 =  57    4 x 19 =  76    5 x 19 =  95    6 x 19 = 114

 7 x  1 =   7    8 x  1 =   8    9 x  1 =   9   10 x  1 =  10
 7 x  2 =  14    8 x  2 =  16    9 x  2 =  18   10 x  2 =  20
 7 x  3 =  21    8 x  3 =  24    9 x  3 =  27   10 x  3 =  30
 7 x  4 =  28    8 x  4 =  32    9 x  4 =  36   10 x  4 =  40
 7 x  5 =  35    8 x  5 =  40    9 x  5 =  45   10 x  5 =  50
 7 x  6 =  42    8 x  6 =  48    9 x  6 =  54   10 x  6 =  60
 7 x  7 =  49    8 x  7 =  56    9 x  7 =  63   10 x  7 =  70
 7 x  8 =  56    8 x  8 =  64    9 x  8 =  72   10 x  8 =  80
 7 x  9 =  63    8 x  9 =  72    9 x  9 =  81   10 x  9 =  90
 7 x 10 =  70    8 x 10 =  80    9 x 10 =  90   10 x 10 = 100
 7 x 11 =  77    8 x 11 =  88    9 x 11 =  99   10 x 11 = 110
 7 x 12 =  84    8 x 12 =  96    9 x 12 = 108   10 x 12 = 120
 7 x 13 =  91    8 x 13 = 104    9 x 13 = 117   10 x 13 = 130
 7 x 14 =  98    8 x 14 = 112    9 x 14 = 126   10 x 14 = 140
 7 x 15 = 105    8 x 15 = 120    9 x 15 = 135   10 x 15 = 150
 7 x 16 = 112    8 x 16 = 128    9 x 16 = 144   10 x 16 = 160
 7 x 17 = 119    8 x 17 = 136    9 x 17 = 153   10 x 17 = 170
 7 x 18 = 126    8 x 18 = 144    9 x 18 = 162   10 x 18 = 180
 7 x 19 = 133    8 x 19 = 152    9 x 19 = 171   10 x 19 = 190

11 x  1 =  11   12 x  1 =  12   13 x  1 =  13   14 x  1 =  14   15 x  1 =  15
11 x  2 =  22   12 x  2 =  24   13 x  2 =  26   14 x  2 =  28   15 x  2 =  30
11 x  3 =  33   12 x  3 =  36   13 x  3 =  39   14 x  3 =  42   15 x  3 =  45
11 x  4 =  44   12 x  4 =  48   13 x  4 =  52   14 x  4 =  56   15 x  4 =  60
11 x  5 =  55   12 x  5 =  60   13 x  5 =  65   14 x  5 =  70   15 x  5 =  75
11 x  6 =  66   12 x  6 =  72   13 x  6 =  78   14 x  6 =  84   15 x  6 =  90
11 x  7 =  77   12 x  7 =  84   13 x  7 =  91   14 x  7 =  98   15 x  7 = 105
11 x  8 =  88   12 x  8 =  96   13 x  8 = 104   14 x  8 = 112   15 x  8 = 120
11 x  9 =  99   12 x  9 = 108   13 x  9 = 117   14 x  9 = 126   15 x  9 = 135
11 x 10 = 110   12 x 10 = 120   13 x 10 = 130   14 x 10 = 140   15 x 10 = 150
11 x 11 = 121   12 x 11 = 132   13 x 11 = 143   14 x 11 = 154   15 x 11 = 165
11 x 12 = 132   12 x 12 = 144   13 x 12 = 156   14 x 12 = 168   15 x 12 = 180
11 x 13 = 143   12 x 13 = 156   13 x 13 = 169   14 x 13 = 182   15 x 13 = 195
11 x 14 = 154   12 x 14 = 168   13 x 14 = 182   14 x 14 = 196   15 x 14 = 210
11 x 15 = 165   12 x 15 = 180   13 x 15 = 195   14 x 15 = 210   15 x 15 = 225
11 x 16 = 176   12 x 16 = 192   13 x 16 = 208   14 x 16 = 224   15 x 16 = 240
11 x 17 = 187   12 x 17 = 204   13 x 17 = 221   14 x 17 = 238   15 x 17 = 255
11 x 18 = 198   12 x 18 = 216   13 x 18 = 234   14 x 18 = 252   15 x 18 = 270
11 x 19 = 209   12 x 19 = 228   13 x 19 = 247   14 x 19 = 266   15 x 19 = 285

16 x  1 =  16   17 x  1 =  17   18 x  1 =  18   19 x  1 =  19
16 x  2 =  32   17 x  2 =  34   18 x  2 =  36   19 x  2 =  38
16 x  3 =  48   17 x  3 =  51   18 x  3 =  54   19 x  3 =  57
16 x  4 =  64   17 x  4 =  68   18 x  4 =  72   19 x  4 =  76
16 x  5 =  80   17 x  5 =  85   18 x  5 =  90   19 x  5 =  95
16 x  6 =  96   17 x  6 = 102   18 x  6 = 108   19 x  6 = 114
16 x  7 = 112   17 x  7 = 119   18 x  7 = 126   19 x  7 = 133
16 x  8 = 128   17 x  8 = 136   18 x  8 = 144   19 x  8 = 152
16 x  9 = 144   17 x  9 = 153   18 x  9 = 162   19 x  9 = 171
16 x 10 = 160   17 x 10 = 170   18 x 10 = 180   19 x 10 = 190
16 x 11 = 176   17 x 11 = 187   18 x 11 = 198   19 x 11 = 209
16 x 12 = 192   17 x 12 = 204   18 x 12 = 216   19 x 12 = 228
16 x 13 = 208   17 x 13 = 221   18 x 13 = 234   19 x 13 = 247
16 x 14 = 224   17 x 14 = 238   18 x 14 = 252   19 x 14 = 266
16 x 15 = 240   17 x 15 = 255   18 x 15 = 270   19 x 15 = 285
16 x 16 = 256   17 x 16 = 272   18 x 16 = 288   19 x 16 = 304
16 x 17 = 272   17 x 17 = 289   18 x 17 = 306   19 x 17 = 323
16 x 18 = 288   17 x 18 = 306   18 x 18 = 324   19 x 18 = 342
16 x 19 = 304   17 x 19 = 323   18 x 19 = 342   19 x 19 = 361



 

Posted by Scripter
,

소스 파일명: testWhile.lsp

  1. #!/usr/bin/env clisp
  2. ;; Filename: testWhile.lsp
  3. ;;
  4. ;;  Purpose:  Example using the while loop syntax
  5. ;;                (loop while (cond) do .... )
  6. ;;
  7. ;; Execute: clisp testWhile.lsp -200 300
  8. ;; 사용법 표시
  9. (defun printUsage()
  10.     (format t "Usage: clisp testWhile.lsp [integer1] [integer2]~%")
  11.     (format t "This finds the greatest common divisor of the given two integers.~%") )
  12. ;; 스트링을 부동소수점수로 변환하는 함수
  13. (defun parse-number (v)
  14.      (with-input-from-string (s v) (read s)))
  15. (if (not(= (length ext:*args*) 2)) (printUsage))
  16. (if (not(= (length ext:*args*) 2)) (quit))
  17. ;; --------------------------------------
  18. ;; 명령행 인자의 두 스트링을 가져와서
  19. ;; 정수 타입으로 변환하여
  20. ;; 변수 val1과 val2에 저장한다.
  21. (setf val1 (parse-integer (nth 0 ext:*args*)))
  22. (setf val2 (parse-integer (nth 1 ext:*args*)))
  23. ;; a는 |val1|, |val2| 중 큰 값
  24. (setf a (abs val1))
  25. (setf b (abs val2))
  26. (setf once 1)
  27. (if (< a b) (loop while (= once 1) do
  28.     (setf a (abs val2))
  29.     (setf b (abs val1))
  30.     (format t "a = ~D, b = ~D~%" a b)
  31.     (setf once 0) ))
  32. ;; 블럭문이 두 개 이상의 문으로 구성되어 있으면 progn을 사용한다.
  33. (if (zerop b) (progn
  34.     (format t "GCD(~D, ~D) = ~D~%" val1 val2 a)
  35.     (quit) ))
  36. ;; --------------------------------------
  37. ;; Euclidean 알고리즘의 시작
  38. ;;
  39. ;; a를 b로 나누어 몫은 q에, 나머지는 r에 저장
  40. (setf q (floor (/ a b)))
  41. (setf r (mod a b))
  42. ; (format t "q = ~D, r = ~D~%" q r)
  43. ;; --------------------------------------
  44. ;; Euclidean 알고리즘의 반복 (나머지 r이 0이 될 때 까지)
  45. (loop while (not(zerop r)) do
  46.     (setf a b)
  47.     (setf b r)
  48.     (setf q (floor (/ a b)))
  49.     (setf r (mod a b)))
  50. ;; 나머지가 0이면 그 때 나눈 수(제수) b가 최대공약수(GCD)이다.
  51. (setf gcd b)
  52. ;; 최대공약수(GCD)를 출력한다.
  53. (format t "GCD(~D, ~D) = ~D~%" val1 val2 gcd)
  54. (quit)


실행:

Command> clisp testWhile.lsp
Usage: clisp testWhile.lsp [integer1] [integer2]
This finds the greatest common divisor of the given two integers.


Command> clisp testWhile.lsp -200 300
GCD(-200, 300) = 100

Command> clisp testWhile.lsp -200 0
GCD(-200, 0) = 200

Command> clisp testWhile.lsp 125 100
GCD(125, 100) = 25

Command> clisp testWhile.lsp 23 25
GCD(23, 25) = 1







Posted by Scripter
,

소스 파일명: testIf.lisp

  1. #!/usr/bin/env clisp
  2. #|
  3.    Filename: testIf.lisp
  4.    Purpose:  Example using the conditional control structure syntax
  5.                  (if cond block1 block2)
  6.    Execute: clisp testIf.lisp [number]
  7. |#
  8. ;; 사용법을 보여주는 함수
  9. (defun printUsing()
  10.    (format t "Using: clisp testIf.lisp [number]~%")
  11.    (format t "This determines whether the number is positive or not.~%") )
  12. ;; 스트링을 부동소수점수로 변환하는 함수
  13. (defun parse-number (v)
  14.      (with-input-from-string (s v) (read s)))
  15. ;; 명령행 인자의 개수가 1이 아니면 사용법을 보여준다.
  16. (if (< (length ext:*args*) 1)
  17.     (printUsing) )
  18. (if (< (length ext:*args*) 1)
  19.     (quit) )
  20. ; 명령행 인자의 스트링을 가져와서
  21. ; 배정밀도 부동소수점수로 변환하여
  22. ; 변수 val에 저장한다.
  23. (setf val (parse-number (nth 0 ext:*args*)) )
  24. ; 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  25. ; 판단하는 (if cond block1 block2) 조건문
  26. (if (> val 0.0)
  27.     (format t "~F is a positive number.~%" val)
  28.     (if (< val 0.0)
  29.         (format t "~F is a negative number.~%" val)
  30.         (format t "~F is zero." val)))


 

실행> clisp testIf.lisp
Using: clisp testIf.lisp [number]
This determines whether the number is positive or not.

실행> clisp testIf.lisp 1.02
1.02 is a positive number.

실행> clisp testIf.lisp -1.02
-1.02 is a negative number.

실행> clisp testIf.lisp 0
0.0 is zero.




Posted by Scripter
,

Common Lisp 언어 의 명령행 인자 처리 방법은 Common Lisp 의 구현체 마다 각각 그 방법이 다르다. 아래의 에제는 CLISP 에서 동작하는 명령행 처리 예제이다.

CLISP 에서는 ext:*args* 라는 리스트(list)형 전역변수로 받는다.
(length ext:*args*) 는 명령문에서 인자의 개수이다.

parse-number 는 대부분의 Coomon Lisp 구현체에서 동작하는 (매우 짧은) 함수로서 스트링(string)을 부동소수점수(floating point number)로 변환하는 일을 한다.

mapcar 와 apply 함수를 사용하여 (반복문 없이) 그 합을 구할 수 있게 하였다.

GNU CLisp 에서는 명령행에서 실행시 명령행 인자들은 ext:*args* 라는 리스트 타입 변수로 받을 수 있다.  예를 들어,

          프롬프트> clisp lest.lisp 1 2 3 4 5

라고 실행하면, (lengh ext:*args*) 의 값은 5 이고,  변수 ext:*args* 는 '(1 2 3 4 5) 라는 리스트를 참조하게 된다. 




 (CLisp 용) 소스파일명: testArguments.lisp
  1. #!/usr/bin/env clisp
  2. (defun parse-number (v)
  3.      (with-input-from-string (s v) (read s)))
  4. (defun main()
  5.     "명령행 인자(command-line argument) 개수 출력"
  6.     (format t "Count of arguments: ~D~%" (length ext:*args*))
  7.     (setf sum (apply #'+ 0 (mapcar #'parse-number ext:*args*)))
  8.     (format t "The sum of arguments is ~F~%" sum))
  9. (main)

 

실행> clisp testArguments.lsp 1 2 3 4
Count of arguments: 4
The sum of arguments is 10


실행> clisp testArguments.lsp 1 2 3 4.1
Count of arguments: 4
The sum of arguments is 10.1


실행> clisp  testArguments.lsp 2.5 3.1E1 -1.5e-4
Count of arguments: 3
he sum of arguments is 33.49985



Closure CL 에서는 명령행에서 실행시 명령행 인자들은 *unprocessed-command-line-arguments* 라는 리스트 타입의 전역 변수로 받을 수 있다.  예를 들어,

          프롬프트> wx86cl64 -l test.lisp -- 1 2 3 4 5

라고 실행하면, (lengh *unprocessed-command-line-arguments* ) 의 값은 5 이고,  전역변수 *unprocessed-command-line-arguments* 는 '("1" "2" "3" "4" "5") 라는 리스트를 참조하게 된다. 

wx86cl64, wx86cl, ccl, ccl64, dx86cl64, dx86cl 등의 명령으로 Common Lisp 소스파일을 싱행시킬 때는 그 소스파일명 직전에 반드시 옵션 -l 또는 --load 를 추가해야 하며, 명령행 인자들은 반드시 두 개의 마이너스 기호 -- 다음에 입력하여야 한다. 



(Clozure CL 용) 소스파일명: testArguments-2.lisp

  1.  ; #!/usr/bin/env clisp    ;  clozure cl 의 wx86cl64 명령은 이를 이식하지 못함
  2.  
  3. (defun parse-number (v)
  4.       (with-input-from-string (s v) (read s)))
  5. (defun main()
  6.       ;; 명령행 인자(command-line argument) 개수 출력
  7.     (let ((arr (list))
  8.            (sum 0))
  9.         (setf arr *unprocessed-command-line-arguments*)
  10.         (format t "Count of arguments: ~D~%" (length arr))
  11.         (setf sum (apply #'+ 0 (mapcar #'parse-number arr)))
  12.         (format t "The sum of arguments is ~F~%" sum)
  13.         (quit) ))
  14. (main)


실행> wx86cl64 -l testArguments-2.lisp -- 2 3 4 5 6
Count of arguments: 5
The sum of arguments is 20.0



Closure CL 에서는 명령행에서 실행시 명령행 인자들을 프로그램 소스에서 받을 때 전역변수  *unprocessed-command-line-arguments* 대신  전역변수  *command-line-argument-lst* 로도 받을 수 있다.  예를 들어,

          프롬프트> wx86cl64 -l test.lisp -- 1 2 3 4 5

라고 실행하면, (lengh *command-line-argument-list* ) 의 값은 9 이고,  전역변수 *unprocessed-command-line-arguments* 는 '("wx86cl64" "-l"  " test.lisp"  "--" "1" "2" "3" "4" "5") 라는 리스트를 참조하게 된다. 


(Clozure CL 용) 소스파일명: testArguments-3.lisp

  1. (defun parse-number (v)
  2.     (with-input-from-string (s v) (read s)))
  3. (defun main()
  4.     ;; 명령행 인자(command-line argument) 개수 출력
  5.      (let ((arr (list))
  6.             (sum 0))
  7.         (setf arr (last *command-line-argument-list* (- (length *command-line-argument-list*) 4)))
  8.         (format t "Count of arguments: ~D~%" (length arr))
  9.         (setf sum (apply #'+ 0 (mapcar #'parse-number arr)))
  10.         (format t "The sum of arguments is ~F~%" sum)
  11.         (quit) ))
  12. (main)


실행> wx86cl64 -l testArguments-3.lisp -- 2 3 4 5 6
Count of arguments: 5
The sum of arguments is 20.0

 

Posted by Scripter
,

Common Lisp 언어의 함수 정의 구문 양식은

       (defun functionName(parameters)
             block )

이다.

Common Lisp 언어의 반복문 양식은

       (loop for varname from start to last by step do
             block )

이다. 여기서 by step 은 생략해도 되는데 이를 생력하면 반복될 때마다 varname 이 1씩 증가한다.



소스 파일명: forTest.lisp
------------------------------[소스 시작]
(defun printDan(dan)
    (loop for i from 1 to 9 do
        (format t "~D x ~D = ~D~%" dan i (* dan i))))

(printDan 2)
------------------------------[소스 끝]

;; CLisp으로 실행하는 경우
실행> clisp -i forTest.lsp
  i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
  I I I I I I I      8     8   8           8     8     o  8    8
  I  \ `+' /  I      8         8           8     8        8    8
   \  `-+-'  /       8         8           8      ooooo   8oooo
    `-__|__-'        8         8           8           8  8
        |            8     o   8           8     o     8  8
  ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2010

Type :h and hit Enter for context help.

;; Loading file forTest.lsp ...
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
;; Loaded file forTest.lsp
[1]> (quit)
Bye.


;; Clozure Common Lisp으로 실행하는 경우
실행> wx86cl64 -l forTest.lsp
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
Welcome to Clozure Common Lisp Version 1.9-r15765 (WindowsX8664)!
? (quit)



Posted by Scripter
,

컨솔에 문자 출력하는 Common Lisp 구문은

       (format f "문자열(스트링)~%")

이다. 여기서 ~%는 C 언어의 개행문자 "\n"에 해당한다.

이다. 여기서 개행문자 "\n"은 추가하지 않아도 된다.

 
소스 파일명: hello.lisp
------------------------------[소스 시작]
(format t "Hello, world!~%")
(quit)
------------------------------[소스 끝]

;; 윈도우 XP에 설치된 CLISP으로 실행하는 경우:

실행> clisp
  i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
  I I I I I I I      8     8   8           8     8     o  8    8
  I  \ `+' /  I      8         8           8     8        8    8
   \  `-+-'  /       8         8           8      ooooo   8oooo
    `-__|__-'        8         8           8           8  8
        |            8     o   8           8     o     8  8
  ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2010

Type :h and hit Enter for context help.

[1]> (load "hello.lisp")
;; Loading file hello.lisp ...
Hello, world!
Bye.

또는

실행> clisp -i hello.lisp
  i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
  I I I I I I I      8     8   8           8     8     o  8    8
  I  \ `+' /  I      8         8           8     8        8    8
   \  `-+-'  /       8         8           8      ooooo   8oooo
    `-__|__-'        8         8           8           8  8
        |            8     o   8           8     o     8  8
  ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2010

Type :h and hit Enter for context help.

;; Loading file hello.lisp ...
Hello, world!
Bye.


;; 윈도우 XP의 cygwin에 설치된 CLISP으로 실행하는 경우:

실행> clisp -i hello.lisp
  i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
  I I I I I I I      8     8   8           8     8     o  8    8
  I  \ `+' /  I      8         8           8     8        8    8
   \  `-+-'  /       8         8           8      ooooo   8oooo
    `-__|__-'        8         8           8           8  8
        |            8     o   8           8     o     8  8
  ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8

Welcome to GNU CLISP 2.48 (2009-07-28) <http://clisp.cons.org/>

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2009

Type :h and hit Enter for context help.

;; Loading file hello.lisp ...
Hello, world!
Bye.


 

;; 윈도우 7에 설치된 Clozure Common LISP으로 실행하는 경우:

실행> wx86cl -l hello.lisp
Hello, world!

또는

실행> wx86cl64 -l hello.lisp
Hello, world!


Posted by Scripter
,