2013/08/29 6

80컬럼 컨솔에 19단표 출력하기 예제 for Common Lisp

다음은 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을 변수명으로 사용할 수 없다. #!/use/bin/env c..

(최대공약수 구하기) while... 반복문 예제 for Common Lisp

소스 파일명: testWhile.lsp #!/usr/bin/env clisp ;; Filename: testWhile.lsp ;; ;; Purpose: Example using the while loop syntax ;; (loop while (cond) do .... ) ;; ;; Execute: clisp testWhile.lsp -200 300 ;; 사용법 표시 (defun printUsage() (format t "Usage: clisp testWhile.lsp [integer1] [integer2]~%") (format t "This finds the greatest common divisor of the given two integers.~%") ) ;; 스트링을 부동소수점수로 변환하는 함수 ..

if...else... 조건문 사용 예제 for Common Lisp

소스 파일명: testIf.lisp #!/usr/bin/env clisp #| Filename: testIf.lisp Purpose: Example using the conditional control structure syntax (if cond block1 block2) Execute: clisp testIf.lisp [number] |# ;; 사용법을 보여주는 함수 (defun printUsing() (format t "Using: clisp testIf.lisp [number]~%") (format t "This determines whether the number is positive or not.~%") ) ;; 스트링을 부동소수점수로 변환하는 함수 (defun parse-number (v) ..

명령행 인자 처리 예제 for Common Lisp

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* 라는 리스트..

구구단 출력 예제 for Common Lisp

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 (* ..

Hello 예제 for Common Lisp

컨솔에 문자 출력하는 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..