Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

4 of 4
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/

Common Lisp, (削除) 636 (削除ここまで) 577

(ql:quickload'cl-ppcre)(lambda(z)(princ(subseq(ppcre:regex-replace-all" *([(')]) *"(with-output-to-string(@)(print`(lambda(n)(dotimes(i n)(loop for(m s)in ',z if(=(mod(1+ i)m)0)do(princ s))(do()((fresh-line))(princ (1+ i)))))@))"\1円")1)))

I took my other answer and wrapped it in quasiquotes while adding input parameters. I print the resulting form as a single-line and remove unnecessary whitespace characters. The compiler is a little longer than the previous version, but the resulting score is reduced.

Score

(let ((*standard-output* (make-broadcast-stream)))
 (loop
 for form in '(215 ; Compiler
 () ; Count
 ((1 "Golf")) ; Golf
 ((3 "Fizz")(5 "Buzz"))) ; FizzBuzz
 for length = (if (numberp form) form
 (length (funcall *fun* form)))
 collect length into lengths
 sum length into sum
 finally (return (values sum lengths))))

Returned values:

574
(215 111 119 129)

Pretty

(defun fizz-buzz-compiler (z)
 (princ (subseq
 (cl-ppcre:regex-replace-all
 " *([(')]) *"
 (with-output-to-string (stream)
 (print
 `(lambda (n)
 (dotimes(i n)
 (loop for (m s) in ',z
 if (=(mod(1+ i)m)0)
 do (princ s))
 (do () ((fresh-line))
 (princ (1+ i))))) stream))
 "\1円") 1)))

The input format is a list of (number string) couples. For example:

(fizz-buzz-compiler '((3 "Fizz")(5 "Buzz")))

... prints to standard output:

(LAMBDA(N)(DOTIMES(I N)(LOOP FOR(M S)IN'((3 "Fizz")(5 "Buzz"))IF(=(MOD(1+ I)M)0)DO(PRINC S))(DO NIL((FRESH-LINE))(PRINC(1+ I)))))

... which, pretty-printed, is:

(lambda (n)
 (dotimes (i n)
 (loop for (m s) in '((3 "Fizz") (5 "Buzz"))
 if (= (mod (1+ i) m) 0)
 do (princ s))
 (do () ((fresh-line)) (princ (1+ i)))))

Testing the resulting function:

CL-USER> ((lambda (n)
 (dotimes (i n)
 (loop for (m s) in '((3 "Fizz") (5 "Buzz"))
 if (= (mod (1+ i) m) 0)
 do (princ s))
 (do () ((fresh-line)) (princ (1+ i))))) 20)
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
coredump
  • 6.9k
  • 25
  • 49

AltStyle によって変換されたページ (->オリジナル) /