Common Lisp
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…
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…
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 (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 …
だいたい同様の手順で解ける細かい問題。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…
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 )…
(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…
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 (*) 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 (**) 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 (*) 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…
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…
まだ、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))
PAIP本から借用。dolistで実装。 ;;; ;;; P04 (*) Find the number of elements of a list. ;;; (defun my-length (list) (let ((i 0)) (dolist (l list) (incf i)) i))
まだ、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))) …
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…
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…