blog/content/post/2010/08/29/2010-08-29-lispの問題-1~3-from...

5.7 KiB
Raw Blame History

title author date url wordtwit_post_info categories
Lispの問題 13 from L-99: Ninety-Nine Lisp Problems kazu634 2010-08-29 /2010/08/29/_1560/
O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5333;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}
gauche
Lisp

L-99: Ninety-Nine Lisp Problems」で掲載されている問題を解いてみました。この辺なら問題なさそう。

番号が増えていくと、手に負えなくなりそうだなぁ。。。

;; P01 (*) Find the last box of a list.
;;     Example:
;;     * (my-last '(a b c d))
;;     (D)
(define (my-last l)
(letrec ((temporary (lambda (a l)
(cond
[(null? l) a]
[else (temporary (car l) (cdr l))]))))
(temporary '() l)))
(my-last '(a b c d))
;; P02 (*) Find the last but one box of a list.
;;     Example:
;;     * (my-but-last '(a b c d))
;;     (C D)
(define (my-but-last l)
(cond
[(null? l) '()]
[(and (not (null? (cdr l)))
(null? (cdr (cdr l)))) l]
[else (my-but-last (cdr l))]))
(my-but-last '(a b c d e f g))
;; 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
(define (element-at l index)
(letrec ((temporary (lambda (l index count)
(cond
[(null? l) '()]
[(eq? index count) (car l)]
[else (temporary (cdr l)
index
(+ count 1))]))))
(temporary l index 1)))
(element-at '(a b c d e) -1)