Common Lisp

P24,P25

P22,P23の結果を使う。 P24では、1〜mまでの整数からn個の整数を重複無しに選択する。 P25では、リストをランダムに並べ替える。 ;; P24 (defun lotto-select (n m) (rnd-select (range 1 m) n)) (time (lotto-select 6 49)) ;; => (7 14 37 11 33 43) ;; P2…

P23: リストからランダムにN個の要素を取り出す

multiple-value-bindは使ってみたかっただけです。はい。 ;; P23 (defun nth-and-remove (n list &optional (acc nil)) (if (or (<= n 0) (null list)) (values (car list) (append (nreverse acc) (cdr list))) (nth-and-remove (1- n) (cdr list) (cons (c…

P22: 整数のリストを作成する

endからstartへ寄せることにより、reverseを省略してみた。 ;; P22 (defun range (start end) (do ((op (if (> end start) #'1- #'1+)) (i end (funcall op i)) (acc '())) ((= i start) (push i acc)) (push i acc))) (range 1 10) ;; => (1 2 3 4 5 6 7 8 …

P13 ランレングスエンコード

副作用ばりばりで。 ;; P13 (defun encode-direct (lis) (if (null lis) lis (do ((acc '()) (target (car lis)) (count 1) (l (cdr lis) (cdr l))) ((null l) (reverse (cons target acc))) (if (and (eql target (car l))) (incf count) (progn (push (if …

P14-P21: 細かい問題

だいたい同様の手順で解ける細かい問題。pushして最後にreverse。 ;; P14 (defun dupli (list) (let ((acc '())) (dolist (elm list) (dotimes (i 2) (push elm acc))) (reverse acc))) (dupli '(a b c d e)) ;; => (A A B B C C D D E E) ;; P15 (defun rep…

P09: 連続して出現したエレメントをまとめる。

pushが結構便利。 ;;; ;; P09 (**) Pack consecutive duplicates of list elements into sub-lists. ;; If a list contains repeated elements they should be placed in separate ;; sub-lists. ;; ;; Example: ;; * (pack '(a a a a b c c a a d e e e e )…

P08: 連続して出現したエレメントを一つにまとめる。

(car nil) (cdr nil)がそれぞれnilを返すのを忘れててはまりました。 ;;; ;; P08 (**) Eliminate consecutive duplicates of list elements ;; If a list contains repeated elements they should be replaced with a single ;; copy of the elemnt. The ord…

P07: ツリーを単純リストに展開する。

consだったら再帰で木を下る。 結果はaccに溜めていって、最後にreverse。 ;;; ;; P07 (**) Flatten a nested list structure. ;; Transform a list, possibly holding lists as elemnts into a 'flat' list by ;; Replacing each list with its elements (r…

P06:回文であるかどうかを判定する

少し悩んだけど、簡単な方法で。 ;;; ;; P06 (*) Find out whether a list a palindrome. ;; A palindrome can be read forward or backward; e.g. (x a m a x) ;; Example: ;; * (palindrome-p '(a b c b a)) ;; t ;; * (palindrome-p '(a b c b)) ;; nil (…

P12: P11で作った圧縮データの展開

;;; ;; P12 (**) Decode a run-length encoded list. ;; Given a run-length code list generated as specified in ;; problem P11. Construct its uncompressed version. (defun decode (lis) (let ((acc nil)) (dolist (elem lis) (cond ((atom elem) (pus…

P11: P10で(1 A)となるようなところは Aにする。

;;; ;; P11 (*) Modified run-length encoding. ;; Modify the result of problem P10 in such a way that if an element ;; has no duplicates it is simply copied into the result list. ;; Only elements with duplicates are transferred as (N E) list…

P10: 連続して出現する要素をまとめ、それぞれ出現回数を求める。

P09の成果を使う。 ;;; ;; P10 (*) Run-length encoding of a list. ;; Use the result of problem P09 to implement the so-called run-length ;; encoding data compression method. Consecutive duplicates of elements ;; are encoded as lists (N E) wh…

P05: list反転

まだ、dolistでいける。 ;;; ;;; P05 (*) Reverse a list. ;;; Example: ;;; * (my-reverse '(a b c)) ;;; (c b a) (defun my-reverse (list) (let ((result nil)) (dolist (element list) (push element result)) result))

P04: lengthを実装

PAIP本から借用。dolistで実装。 ;;; ;;; P04 (*) Find the number of elements of a list. ;;; (defun my-length (list) (let ((i 0)) (dolist (l list) (incf i)) i))

P03: index番目の要素を返す。ただし1基底

まだ、doで何とかなる。 ;;; ;;; 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 (list index) (do ((l list (cdr l)) (i 1 (1+ i))) …

P02: 最後の一つ手前のcons cellを見つける

doで実装。一つ手前のcons cellをresultへ保持。 ;;; ;;; P02 (*) Find the last but one box of a list. ;;; Example: ;;; * (my-but-last '(a b c d)) ;;; (C D) (defun my-but-last (list) (let ((result nil)) (do ((l list (cdr l))) ((not (consp (cdr…

P01: 最後のcons cellを見つける

PAIP読書会で知り合ったid:g000001さんから、L-99を勧められたので挑戦してみる。 取りあえず1問目から。doで実装してみる。 ;;; ;;; P01 (*) Find the last box of a list. ;;; Example: ;;; * (my-last '(a b c d)) ;;; (D) (defun my-last (list) (do ((l…