Created
August 23, 2012 18:57
-
-
Save alcidesfp/3440296 to your computer and use it in GitHub Desktop.
FizzBuzz en Scheme/Kawa
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
;; -*- coding: utf-8; mode: Scheme -*- | |
;; FizzBuzz en Scheme R5 (Kawa, Chicken) sin utilizar funciones de bibliotecas extras | |
;;------------------------------------------------------------------------ | |
(define (my-iota n #!optional (i 0)) | |
"Regresa una lista de N numeros naturales consecutivos que van desde | |
I=0 hasta N-1 por default y opcionalmente desde I hasta N si se | |
proporciona I >= 0" | |
(if (and (>= n 0) (integer? n)) ;; Definida solo para los Naturales | |
;; Versión recursiva con NAMED LET es efectivamente iterativa | |
(let loop ((j i) | |
(retval '())) | |
(if (= j (+ n i)) | |
(reverse retval) | |
(loop (+ j 1) (cons j retval)) ))) ) | |
;;------------------------------------------------------------------------ | |
(define (fizzbuzz num) | |
(cond ((= 0 (modulo num 15)) "FizzBuzz") | |
((= 0 (modulo num 5)) "Buzz") | |
((= 0 (modulo num 3)) "Fizz") | |
(else num) )) | |
;;------------------------------------------------------------------------ | |
;; Imprime fizzbuzz con números del 1 al 100 | |
(for-each (lambda (num) (display (fizzbuzz num))(newline)) (my-iota 100 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment