manywaypark's Blog
개발, 검색, 함수

초기화 파일을 응용하여 명령행에서 유닉스 스크립트처럼 lisp 파일을 실행하는 법에 대해 간단히 기술한다.

초기화 파일 위치:
  1. 시스템 수준: 기본값은 SBCL_HOME/sbclrc, 없으면, /etc/sbclrc이다. 명령행에서 --sysinit로 지정가능.
  2. 사용자 수준: 기본값은 HOME/.sbclrc이지만 명령행에서 --userinit로 지정가능.
상기 초기화 파일 중에 적절한 것을 선택해서 다음의 내용을 추가한다. 파일이 없으면 만든다.

shebang(#!) 형태의 lisp 프로그램 실행을 위한 초기화 파일 내용:
;;; If the first user-processable command-line argument is a filename,
;;; disable the debugger, load the file handling shebang-line and quit.
(let ((script (and (second *posix-argv*)
(probe-file (second *posix-argv*)))))
(when script
;; Handle shebang-line
(set-dispatch-macro-character #\# #\!
(lambda (stream char arg)
(declare (ignore char arg))
(read-line stream)))
;; Disable debugger
(setf *invoke-debugger-hook*
(lambda (condition hook)
(declare (ignore hook))
;; Uncomment to get backtraces on errors
;; (sb-debug:backtrace 20)
(format *error-output* "Error: ~A~%" condition)
(quit)))
(load script)
(quit)))

test program(hello.lisp):
#!/usr/bin/sbcl --noinform
(write-line "Hello, World!")

실행:
$ chmod +x hello.lisp
$ ./hello.lisp
Hello, World!
$ sbcl hello.lisp
This is SBCL 0.9.14, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
Hello, World!

참고: http://www.sbcl.org/manual/Initialization-Files.html

2007-10-24
cl-launch를 사용하면 sbcl을 포함한 다른 CL implementation에서도 쓸 수 있는 좀 더 일반적인 방법으로 common lisp 프로그램을 명령행에서 실행할 수 있다.

happy hackin'
Lisp 구현체에 따라 적절한 전역 변수를 읽어서 반환하게 하는 것이 기본이 된다.

환경 변수를 얻는 함수:
(defun my-getenv (name &optional default)
#+CMU
(let ((x (assoc name ext:*environment-list*
:test #'string=)))
(if x (cdr x) default))
#-CMU
(or
#+Allegro (sys:getenv name)
#+CLISP (ext:getenv name)
#+ECL (si:getenv name)
#+SBCL (sb-unix::posix-getenv name)
#+LISPWORKS (lispworks:environment-variable name)
default))

명령행 인수를 얻는 함수:
(defun my-command-line ()
(or
#+SBCL *posix-argv*
#+LISPWORKS system:*line-arguments-list*
#+CMU extensions:*command-line-words*
nil))

출처: http://cl-cookbook.sourceforge.net/os.html (상기 두개 말고도 참고 자료가 좀 더 있다.)

happy hackin'

ASDF brief

함수형 언어/Lisp 2007. 3. 23. 01:14 by manywaypark
ASDF는 "Another System Definition Facility"의 약자로서 간편하게 package들을 관리할 수 있게 해준다. 여기서는 간단한 사용법을 살펴본다.

package 설치(install):
자동 다운로드 및 설치가 되는 경우
;;; imports asdf
(require 'asdf)
;;; imports asdf-install
(require 'asdf-install)
;;; install some package
(asdf-install:install 'some-package)
참고 및 다운로드/설치 가능한 패키지 리스트
설치시 선택한 위치(system-wide 또는 personal)에 파일들이 다운로드 되고 설치된다.
경로는 system-wide는 /usr/lib/sbcl, personal은 ~/.sbcl이다.
일단 한번 설치한 후에는 간단히 (require 'some-package)로 불러서 쓸 수 있다.

자동 설치가 지원되지 않는 것들은 수동 다운로드 후에 파일을 로드하는 조금 성가신 작업이 필요하다.
package 로드(load):
;;; imports asdf
(require 'asdf)
;;; loads some package from disk (current dir. has some-package-on-disk.asd file).
(asdf:operate 'asdf:load-op :some-package-on-disk)

load path 추가: 패키지를 load할 경로 추가하기. 참고
;;; adds asdf load path
(pushnew "/path/to/your/registry/" asdf:*central-registry* :test #'equal)

패키지 삭제하기:
;;; removes a package
(asdf-install:uninstall :package-name)

참고

happy hackin'

ps. 사용된 lisp 구현은 sbcl이다.

slime 설치, 한글 출력

함수형 언어/Lisp 2007. 3. 21. 14:53 by manywaypark
slime은 emacs를 환상적인 lisp IDE로 변신시켜준다.
cvs로 최신 소스를 받거나, 안정버전을 다운로드 한다(최신 개발 소스는 가끔 불안정한 경우도 있다).

~/.emacs에 추가할 내용(밑줄 부분은 자신의 설정에 맞게 변경):
;;; for slime
(setq inferior-lisp-program "/usr/bin/sbcl")
(add-to-list 'load-path "~/hacking/lisp/slime-2.0")
(require 'slime)
(slime-setup)
;;; for utf-8 coding system.
(set-language-environment "UTF-8")
(setq slime-net-coding-system 'utf-8-unix)
상단부는 slime 설정이고 하단부는 utf-8 관련 설정인데, utf-8 관련 설정을 하지 않으면 한글을 출력하려 할 때 다음과 같은 에러 메시지를 뿌리며 정상적인 실행이 안된다.
;;; (format t "한글")
Coding system iso-latin-1-unix not suitable for "00005a(:emacs-rex (swank:listener-eval \"(format t \\\"한글\\\")
\") \"COMMON-LISP-USER\" :repl-thread 8)

이제 남은 것은 M-x slime!!

happy hackin'

관련링크: slime home,cliki slime tips
1 2 
분류 전체보기 (306)
잡담 (20)
함수형 언어 (65)
Scheme (5)
Lisp (14)
Erlang (31)
R (3)
Elixir (11)
emacs (16)
java (18)
tips & tricks (154)
사랑 (1)
가사 (0)
독서 (4)
mobile (6)
비함수형 언어 (2)

공지사항

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

04-26 02:01