5.6 KiB
5.6 KiB
title | author | date | wordtwit_post_info | categories | |||
---|---|---|---|---|---|---|---|
Lispの問題 1~3 from L-99: Ninety-Nine Lisp Problems | kazu634 | 2010-08-29 |
|
|
「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)