忍者ブログ

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

2024/04/19

lispでdat2html

書いてみた。

複雑な例外処理だとか怪しい処理はどうかわかりませんが、こういうフィルタ系の処理は関数型言語に向いていると思います。

そもそも関数というものがそういう物ものですからね。

Cだとサブルーチンも関数と呼びますが、Pascalだってfunctionとprocedureもありますが、このこたちは関数というよりもやはり手続きです。
古いBASICだと関数とステートメントは確かに別扱いで別なんですよね。
(defun split-string(str c)
  (split-string_ str c (length c) 0))

(defun split-string_(str c l idx)
  (cond
  ((null str)
  nil)
  ((<= (length str) (+ idx (- l 1)))
  (list str))
  ((string= str c :start1 idx :end1 (+ idx l))
  (cons (subseq str 0 idx) (split-string_ (subseq str (+ idx l)) c l 0)))
  (t
  (split-string_ str c l (+ idx 1)))))

(defun load-dat()
  (if (null (setq line (read-line *standard-input* nil nil)))
  nil
  (cons (split-string line "<>") (load-dat)))
)

(defun get-title(l)
  (car (cdr (cdr (cdr (cdr (car l))))))
)

(defun list2html(l n)
  (cond
  ((= (length (car (cdr l))) 0)
  (concatenate 'string "<dt>" (write-to-string n) " :<b>" (car l) " </b>:" (car (cdr (cdr l))) "<dd>" (car (cdr (cdr (cdr l)))) "<br><br>"))
  (t
  (concatenate 'string "<dt>" (write-to-string n) " :<a href=\"mailto:" (car (cdr l)) "\"><b>" (car l) " </b></a>:" (car (cdr (cdr l))) "<dd>" (car (cdr (cdr (cdr l)))) "<br><br>"))
  )
)

(setq lines (load-dat))
(format *standard-output* "<html>~%<head>~%<title>~A</title>~%</head>~%<body>~%<dl>~%" (get-title lines))
(setq cnt 1)
(dolist (line lines)
  (format *standard-output* "~A~%" (list2html line cnt))
  (setq cnt (+ cnt 1))
)
(format *standard-output* "</dl>~%</body>~%</html>~%")

でもlispっぽくないなあ。 手続き臭を抜き切れていません。

PR

2008/09/21 Programming Comment(0)

COMMENT

COMMENT FORM

NAME
MAIL
WEB
TITLE
COMMENT
PASSWORD

カテゴリー
 
 
 
最新記事
 
(05/20)
(08/13)
(06/15)
(03/28)
(05/31)
 
 
ブログ内検索
 
 
 
カレンダー
 
03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
 
 
アーカイブ
 
 
 
最新TB
 
 
 
最新CM
 
 
 
リンク
 
 
 
Feed
 
 
 
注意点
 

英字のみのコメントは拒否しています。スパムの排除のためご理解をお願いいたします。(でも結構スルーされてしまいます。たまに掃除していますがご容赦のほどを。)

 
 
カウンター
 
 
 
アクセス解析