上は再帰で、歯抜けがあったらその歯抜けも追加したリストを次の再帰に渡しています。歯抜けは acc に保存。
下は iota で完全な集合(完全な連番のリスト)を作って、引数との差集合を求めています。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (pick-toothless ls) | |
(let rec ((ls ls)(acc '())) | |
(if (or (null? ls) | |
(null? (cdr ls))) | |
(reverse acc) | |
(let1 next (+ (car ls) 1) | |
(if (= next (cadr ls)) | |
(rec (cdr ls) acc) | |
(rec (cons next (cdr ls)) | |
(cons next acc))))))) | |
(pick-toothless '(1 2 3 5 6 8 9)) | |
;; (4 7) | |
(pick-toothless '(1 2 4 5 10)) | |
;; (3 6 7 8 9) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use srfi-1) | |
(define (pick-toothless ls) | |
(if (null? ls) | |
'() | |
(let ((min (car ls))(max (last ls))) | |
(let1 pls (iota (+ (- max min) 1) min) | |
(lset-difference = pls ls))))) | |
(pick-toothless '(1 2 3 5 6 8 9)) | |
;; (4 7) | |
(pick-toothless '(1 2 4 5 10)) | |
;; (3 6 7 8 9) |
0 件のコメント:
コメントを投稿