Skip to content

Instantly share code, notes, and snippets.

@y2q-actionman
Created January 31, 2019 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y2q-actionman/f93321823c9ce91a7a0f1126c3d7dcdf to your computer and use it in GitHub Desktop.
Save y2q-actionman/f93321823c9ce91a7a0f1126c3d7dcdf to your computer and use it in GitHub Desktop.
C と lisp で fizzbuzz 比較
#include <stdio.h>
/*
int main() {
int i;
for (i = 1; i <= 100; ++i) {
if (i % 15 == 0) {
printf("FizzBuzz\n");
} else if (i % 3 == 0) {
printf("Fizz\n");
} else if (i % 5 == 0) {
printf("Buzz\n");
} else {
printf("%d\n", i);
}
}
}
*/
int main () {
int i;
for (i = 1; i <= 100; ++i)
if (i % 15 == 0)
printf("FizzBuzz\n");
else if (i % 3 == 0)
printf("Fizz\n");
else if (i % 5 == 0)
printf("Buzz\n");
else
printf("%d\n", i);
}
(defun fizzbuzz1 ()
(loop for i from 1 to 100
if (= (mod i 15) 0)
do (print "FizzBuzz")
else if (= (mod i 3) 0)
do (print "Fizz")
else if (= (mod i 5) 0)
do (print "Buzz")
else
do (print i)))
(defun fizzbuzz2 ()
;; I use `do', because `dotimes' is 0-origin.
(do ((i 1 (1+ i)))
((> i 100))
(cond ((= (mod i 15) 0)
(print "FizzBuzz"))
((= (mod i 3) 0)
(print "Fizz"))
((= (mod i 5) 0)
(print "Buzz"))
(t
(print i)))))
(defun fizzbuzz2-2 ()
(loop for i from 1 to 100
do
(cond ((= (mod i 15) 0)
(print "FizzBuzz"))
((= (mod i 3) 0)
(print "Fizz"))
((= (mod i 5) 0)
(print "Buzz"))
(t
(print i)))))
#|
これはだめ
(set-syntax-from-char #\{ #\()
(set-syntax-from-char #\} #\))
これがいる:
CL-USER> (with-input-from-string (s "1 2 3 4 5 }")
(read-delimited-list #\} s))
(1 2 3 4 5)
(progn ... ) に展開される { } マクロとか、もしかしたら有用説ある??
|#
#+ignore
(defun fizzbuzz2-3 ()
;; I use `do', because `dotimes' is 0-origin.
(do ((i 1 (1+ i)))
((> i 100))
{cond ((= (mod i 15) 0)
(print "FizzBuzz"))
((= (mod i 3) 0)
(print "Fizz"))
((= (mod i 5) 0)
(print "Buzz"))
(t
(print i))
}
))
(named-readtables:in-readtable with-c-syntax:with-c-syntax-readtable)
(defun fizzbuzz3 ()
(do ((i 1 #{ i + 1 \; }#))
((> i 100))
(cond (#{ i % 15 == 0 \; }#
(print "FizzBuzz"))
(#{ i % 3 == 0 \; }#
(print "Fizz"))
(#{ i % 5 == 0 \; }#
(print "Buzz"))
(t
(print i)))))
(defun fizzbuzz4 ()
(do ((i 1 #{ i + 1 \; }#))
((> i 100))
#{
if (i % 15 == 0) {
print("FizzBuzz");
} else if (i % 3 == 0) {
print("Fizz");
} else if (i % 5 == 0) {
print("Buzz");
} else {
print(i);
}
}#
))
(defun fizzbuzz5 ()
#{
int i;
for (i = 1; i <= 100; ++ i){
if (i % 15 == 0) {
print("FizzBuzz");
} else if (i % 3 == 0) {
print("Fizz");
} else if (i % 5 == 0) {
print("Buzz");
} else {
print(i);
}
}
}#)
@y2q-actionman
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment