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: Peter Bex
Subject: Re: [Chicken-users] Review my Caesar Cipher?
Date: Mon, 10 Mar 2014 16:31:49 +0100
User-agent: Mutt/1.4.2.3i

On Mon, Mar 10, 2014 at 10:26:56AM -0500, Phil Bewig wrote:
> I would use an auxiliary function char-plus to add or subtract an offset to
> a character:
> 
> (define (caesar str n)
>   (define (char-plus c)
>     (let ((alpha "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
>       (if (not (char-alphabetic? c)) c
>         (let ((i (- (char->integer (char-upcase c)) 65)))
>           (string-ref alpha (modulo (+ i n) 26))))))
>   (list->string (map char-plus (string->list str))))

If you're using srfi-13, you might as well change the final line to use
string-map:  (string-map char-plus str)

Cheers,
Peter
-- 
http://www.more-magic.net



reply via email to

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