chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Review my Caesar Cipher?


From: Daniel Carrera
Subject: Re: [Chicken-users] Review my Caesar Cipher?
Date: Tue, 11 Mar 2014 11:15:12 +0100

With the last suggestion from Alex, and a tip to use cond-expand from Kon, I have settled on the following:

-----------------------------
;
; Works with Chicken Scheme and Gauche.
;
(cond-expand (chicken (use srfi-13))
             (gauche  (use srfi-13)))
 
(define msg "The quick brown fox jumps over the lazy dog.")
(define key 13)
 
(define (caesar char)
  (define A (char->integer #\A))
  (define Z (char->integer #\Z))
  (define a (char->integer #\a))
  (define z (char->integer #\z))
  (define c (char->integer char))
  (integer->char
    (cond ((<= A c Z) (+ A (modulo (+ key (- c A)) 26)))
          ((<= a c z) (+ a (modulo (+ key (- c a)) 26)))
          (else c)))) ; Return other characters verbatim.
 
(print (string-map caesar msg))
-----------------------------

I tried to include more Schemes, but Chibi doesn't seem to have SRFI-13, Racket doesn't support SRFI-0 (cond-expand), and Stklos is case-insensitive. There are other schemes that support both SRFI-0 and 13, but AFAICT they are not active. Even Stklos seems to have gone into a slumber 2.5 years ago. I have updated the Rosetta Code page. Since this code includes a lot of advice from experienced Schemers, I have removed the "novice" note.


Cheers,
Daniel.


On 11 March 2014 02:09, Alex Shinn <address@hidden> wrote:

(integer->char
 (cond ((<= A c Z) (+ A (modulo (+ key (- c A)) 26)))
          ((<= a c z) (+ a (modulo (+ key (- c A)) 26)))
          (else c)))

--
When an engineer says that something can't be done, it's a code phrase that means it's not fun to do.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]