0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Lisp 食べ比べ (2) ループ処理編

Last updated at Posted at 2022-11-26

2024/02/23 更新

はじめに(環境説明)

私が Lisp を使った順番で言えば、Common Lisp ⇒ Emacs Lisp ⇒ Scheme (Racket) となります。
(今まで使ったことがありませんが Gauche Scheme も追加しました。)

$ sbcl --version
SBCL 2.2.10

$ emacs --version
GNU Emacs 28.2

$ racket --version
Welcome to Racket v8.6 [cs].

$ gosh -V
Gauche scheme shell, version 0.9.12 [utf-8,wthreads], x86_64-w64-mingw32

例によって、上記の環境を使って、「Lisp 食べ比べ」をしてみたいと思います。

さて今回は

整数回ループしたり、リストの要素数分ループしたりということをやります。
表示周りでは書式制御などがキモになります。

実行上の留意点

  • sbcl (Common Lisp) と racket (Scheme) は元々バッチ実行ができますから、ソースを実行ファイルに食わせるだけでいいのですが、emacs (Emacs Lisp) はどうするかといいますと「バッチモード実行」を使います。

  • また、emacs では、以下の記事に載せてある xprint.el がホームディレクトリに置かれてあることを前提とします。

Emacs Lisp でプリントデバッグをする (5) - Qiita

ソースと実行結果

以下に3つのソースを示します。
sbcl, emacs, racket, gosh (Gauche) の実行ファイルにパスが通っているのを前提にbashのshebangを使って実行しています。

Common Lisp [SBCL] (test02.lsp)
#! /usr/bin/env -S sbcl --script
(defparameter loop-count 5)
(dotimes (i loop-count)
  (format t "i+1=~S~%" (1+ i))
  )
(defparameter misc-list '(11 22 abc "def ghi"))
(dolist (x misc-list)
  (format t "x=~S~%" x)
  )

;;; Welcome to the Emacs shell
;;; 
;;; ~/racket $ cd "d:/Users/javac/Documents/.repo/env/racket/" && time "./test02.lsp"
;;; i+1=1
;;; i+1=2
;;; i+1=3
;;; i+1=4
;;; i+1=5
;;; x=11
;;; x=22
;;; x=ABC
;;; x="def ghi"
;;; 0.202 secs
;;; ~/racket $
Emacs Lisp (test02.el)
#! /usr/bin/env -S emacs -batch -l
(load "~/xprint.el")
(setq loop-count 5)
(dotimes (i loop-count)
  (xdump (1+ i))
  )
(setq misc-list '(11 22 abc "def ghi"))
(dolist (x misc-list)
  (xdump x)
  )

;;; Welcome to the Emacs shell
;;; 
;;; ~/racket $ cd "d:/Users/javac/Documents/.repo/env/racket/" && time "./test02.el"
;;; Loading d:/Users/javac/Documents/.repo/env/xprint.el (source)...
;;; (1+ i) := 1
;;; (1+ i) := 2
;;; (1+ i) := 3
;;; (1+ i) := 4
;;; (1+ i) := 5
;;; x := 11
;;; x := 22
;;; x := abc
;;; x := "def ghi"
;;; 0.295 secs
;;; ~/racket $
Scheme [Racket] (test02.rkt)
#! /usr/bin/env racket
#lang racket
(define loop-count 5)
(for ([i loop-count])
  (printf "i+1=~s\n" (+ i 1))
  )
(define misc-list '(11 22 abc "def ghi"))
(for ([x misc-list])
  (printf "x=~s\n" x)
  )

;;; Welcome to the Emacs shell
;;; 
;;; ~/racket $ cd "d:/Users/javac/Documents/.repo/env/racket/" && time "./test02.rkt"
;;; i+1=1
;;; i+1=2
;;; i+1=3
;;; i+1=4
;;; i+1=5
;;; x=11
;;; x=22
;;; x=abc
;;; x="def ghi"
;;; 0.655 secs
;;; ~/racket $
Scheme [Gauche] (test02.scm)
#! /usr/bin/env gosh
(define loop-count 5)
(dotimes (i loop-count)
  (format #t "i+1=~S~%" (+ i 1))
  )
(define misc-list '(11 22 abc "def ghi"))
(dolist (x misc-list)
  (format #t "x=~S~%" x)
  )

;;; Welcome to the Emacs shell
;;; 
;;; ~/racket $ cd "d:/Users/javac/Documents/.repo/env/racket/" && time "./test02.scm"
;;; i+1=1
;;; i+1=2
;;; i+1=3
;;; i+1=4
;;; i+1=5
;;; x=11
;;; x=22
;;; x=abc
;;; x="def ghi"
;;; 0.318 secs
;;; ~/racket $

最後に

今後も、Common Lisp、Emacs Lisp、Scheme のそれぞれの書き方で同じことをする・・・というのをアップしていきたいと思います。

もっと良い書き方あるよーて方はコメント等で教えてください!

それでは!

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?