2010/06/19

Series で L-99 P01-P05

おとなしく、やってみることにする。

(eval-when (:compile-toplevel :load-toplevel :execute)
(require :series))
(eval-when (:compile-toplevel :load-toplevel :execute)
(series::install))

;; L-99: Ninety-Nine Lisp Problems
;; Based on a Prolog problem list by werner.hett@hti.bfh.ch
;; Working with lists
;; P01 (*) Find the last box of a list.
;; Example:
;; * (my-last '(a b c d))
;; (D)
(defun my-last (x)
(collect-last x))
(my-last #z(a b c d))


;; P02 (*) Find the last but one box of a list.
;; Example:
;; * (my-but-last '(a b c d))
;; (C D)
(defun my-but-last (x)
(subseries x (- (collect-length x) 1)))
(my-but-last #z(a b c d)) ;; => #Z(D)


;; P03 (*) Find the K'th element of a list.
;; The first element in the list is number 1.
;; Example:
;; * (element-at '(a b c d e) 3)
;; C
(defun element-at (x n)
(collect-nth (1- n) x))
(element-at #z(a b c d e) 3)


;; P04 (*) Find the number of elements of a list.
(collect-length #z(a b c d))


;; P05 (*) Reverse a list.
(defun my-reverse (x)
(scan (reverse (collect x)))) ; これはひどい
(my-reverse #z(a b c d))

Series で sort や reverse はどうするんだろう?ストリーム的なところになじまないということだろうか。

0 件のコメント: