1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

逆FizzBuzz

Posted at

だいぶ前に(以下略

invfizzbuzz.scm
(use srfi-42)

(define strl '(fizz buzz fizz fizz buzz fizz fizzbuzz))
(define intl '(3 5 6 9 10 12 15))

(define (min-list lst)
  (let loop ((ls (map (lambda(x)(length x)) lst))
			 (n 0))
	(if (eq? (car ls) (apply min ls))
	  n
	  (loop (cdr ls) (+ n 1)))))

(define (fizzbuzz lst)
  (let loop ((n 1)
			 (ls lst)
			 (str strl)
			 (int intl))
	(cond
	  ((null? (cdr ls))(solve lst str int))
	  ((eq? (car ls) 'fizzbuzz)
	   (loop (+ n 1) (cdr ls) (append str strl) (append int (map (lambda(x)(* (+ 15 x) n)) intl))))
	  (else (loop n (cdr ls) str int)))))

(define (solve lst str int)
  (let loop ((str str)
			 (int int)
			 (result '()))
	(cond ((> (length lst) (length str))
		   (reverse (map (lambda(x)(list-ec (:range v (car x) (+ (last x) 1)) v)) result)))
		  ((equal? lst (take str (length lst)))
		   (loop (cdr str) (cdr int) (cons (take int (length lst)) result)))
		  (else (loop (cdr str) (cdr int) result)))))

(define (invfizzbuzz lst)
  (let ((result (fizzbuzz lst)))
	(if (null? result)
	  '()
	  (print (list-ref result (min-list result))))))
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?