ループ1

(for-each print
          (map
            (lambda (_) "Hello World!")
            (iota 5)))

ループ2

(define (foo n)
  (for-each print
            (map
              (lambda (i)
                (format #f "~a ~d" "Hello World!" (+ i 1)))
              (iota n))))
(define (main args)
  (foo (string->number (list-ref args 1))))

FizzBuzz

(print
  (string-join
    (map
      (lambda (x)
        (cond
          ((zero? (modulo x 15)) "FizzBuzz")
          ((zero? (modulo x  5)) "Buzz")
          ((zero? (modulo x  3)) "Fizz")
          (else (format #f "~d" x))))
      (iota 100 1))))

素数判定

(define (check-prime n d)
  (if (> d (sqrt n))
    #t
    (if (zero? (modulo n d))
      #f
      (check-prime n (+ d 1)))))
(define (is-prime? n)
  (if (< n 2)
    #f
    (check-prime n 2)))
(define prime?
  (lambda (n)
    (if (is-prime? n)
      (print (format #f "~dは素数です。" n))
      (print (format #f "~dは素数ではありません。" n)))))
(prime? 1)
(prime? 2)
(prime? 3)
(prime? 4)
(prime? 5)
(prime? 997)
(prime? 998)
(prime? 15485863)
(prime? 122949823)
(prime? 674506081)
(prime? 982451653)

うるう年判定

(define (is-leap-year? n)
  (cond
    ((zero? (modulo n 400)) #t)
    ((zero? (modulo n 100)) #f)
    ((zero? (modulo n   4)) #t)
    (else                   #f)))
(define (leap-year? n)
  (if (is-leap-year? n)
    (print (format #f "~d年はうるう年です。" n))
    (print (format #f "~d年はうるう年ではありません。" n))))
(leap-year? 1992)
(leap-year? 2000)
(leap-year? 2001)

累乗1

(define pow
  (lambda (a n)
    (if (zero? n)
      1
      (* a (pow a (- n 1))))))
(print (pow 2 10))

フィボナッチ数列1

(define fib
  (lambda (n)
    (if (< n 2)
      1
      (+ (fib (- n 1)) (fib (- n 2))))))
(for-each print
  (map
    (lambda (n) (fib n))
    (iota 10)))

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS