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: Mon, 10 Mar 2014 16:59:06 +0100

On 10 March 2014 16:40, "Jörg F. Wittenberger" <address@hidden> wrote:
A first alternative would be string-ref.  That would be O(1).  As long as your Scheme does not use something like UTF-8 as string representation.  So maybe it would be the best to resort to something like:

(define replacements (apply vector (string->list rotated)))
 
(define (caesar char)
  (let ((index (string-index alphabet char)))
    (if index
        (vector-ref replacements index)
        char))) ; Not found => Copy verbatim.

Notice: I replaced the second occurrence of "(string-index alphabet char)" with "index" - the variable the result was of the first call was already bound to.  I'm leaving it here as an exercise to you to figure out why.  ;-)

Ugh. Yeah, thanks. I see it. 

 
Your second version brings up a completely different consideration.  The task "implement a caesar chipher" is slightly underspecified.  That is, actually it's OK, since it would imply that you are supposed to produce a general solution.  And your first version does.

You second version depends on the mapping from characters to integers.  It will "only" work on such mappings, which are "accidentally" compatible to ASCII for upper case letters.  I'm using quotes here, because the days are long gone, when you had a reasonable chance to get your hands on a system using incompatible encodings like
http://en.wikipedia.org/wiki/DEC_Radix-50
Otherwise I'd prefer the second version for using less memory.

I didn't think of that. I hadn't even heard of DEC Radix-50.

 
Note however: if you wanted the your cipher to be easily adapted to more general mappings (e.g. be applicable to other character sets than upper case ASCII compatible - which would easily be parts of unicode or say HTML entities like &uuml;) then the argument is reversed and your first version would be the better fit.

That makes sense.
 
Cheers,
Daniel.

reply via email to

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