Re: [Chicken-users] Review my Caesar Cipher?
Bonjour, Just for fun, I tried running the current Rosetta code with the TinyScheme interpreter. It has case-insensitive symbols, so it failed interestingly. The solution is to change the variable names (because otherwise 'A' is the same as 'a'). The Scheme FAQ ( http://community.schemewiki.org/?scheme-faq-language ) states that this is defined in R5RS. Chicken is case-sensitive. Is this commonly ignored? Thanks. -- Claude Marinier ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
Claude Marinier scripsit: The Scheme FAQ ( http://community.schemewiki.org/?scheme-faq-language ) states that this is defined in R5RS. Chicken is case-sensitive. Is this commonly ignored? Very commonly, but not universally so. Case-sensitivity is required in R6RS and R7RS. See http://trac.sacrideo.us/wg/wiki/CaseInsensitivity for how various Schemes actually behave. -- Verbogeny is one of the pleasurettesJohn Cowan co...@ccil.org of a creatific thinkerizer. http://www.ccil.org/~cowan --Peter da Silva ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
On 12 March 2014 17:16, Alex Shinn alexsh...@gmail.com wrote: Then change your cond-expand to: (cond-expand ((or chicken gauche) ; compatibility (use srfi-13)) (else ; R7RS (import (scheme base) (scheme write That works. Thanks! I have updated the page on Rosetta Code. It now works correctly on Chicken, Gauche, Chibi and I also added Kawa. Thanks for the explanation. Cheers, Daniel. -- When an engineer says that something can't be done, it's a code phrase that means it's not fun to do. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
On Tue, Mar 11, 2014 at 9:20 PM, Daniel Carrera dcarr...@gmail.com wrote: On 11 March 2014 12:41, Alex Shinn alexsh...@gmail.com wrote: Chibi has string-map in (chibi string). But actually, if you're aiming for R7RS support then string-map is in (scheme base). Just replace the cond-expand with: (import (scheme base)) Hmm... sadly, (import (scheme base)) fails with Chicken and Gauche. I am also having a hard time figuring out how to print with Chibi. I tried the manual, and I tried (print), (printf) and (display). Then change your cond-expand to: (cond-expand ((or chicken gauche) ; compatibility (use srfi-13)) (else ; R7RS (import (scheme base) (scheme write R7RS puts display in (scheme write), because it typically falls back to write for non-char/strings, and because write is actually a fairly large and complicated procedure not needed by most libraries. However, write-char, write-string and newline are all in (scheme base) so you could just use write-string here. You can also (import (scheme r5rs)) to get all of the R5RS bindings except transcript-on/off. -- Alex ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
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. http://rosettacode.org/wiki/Caesar_cipher#Scheme Cheers, Daniel. On 11 March 2014 02:09, Alex Shinn alexsh...@gmail.com 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. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
On Tue, Mar 11, 2014 at 7:15 PM, Daniel Carrera dcarr...@gmail.com wrote: 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 Chibi has string-map in (chibi string). But actually, if you're aiming for R7RS support then string-map is in (scheme base). Just replace the cond-expand with: (import (scheme base)) -- Alex ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
On 11 March 2014 12:41, Alex Shinn alexsh...@gmail.com wrote: Chibi has string-map in (chibi string). But actually, if you're aiming for R7RS support then string-map is in (scheme base). Just replace the cond-expand with: (import (scheme base)) Hmm... sadly, (import (scheme base)) fails with Chicken and Gauche. I am also having a hard time figuring out how to print with Chibi. I tried the manual, and I tried (print), (printf) and (display). Cheers, Daniel. -- When an engineer says that something can't be done, it's a code phrase that means it's not fun to do. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
(Sorry for off-topic of ML) From: Daniel Carrera dcarr...@gmail.com Subject: Re: [Chicken-users] Review my Caesar Cipher? Date: Tue, 11 Mar 2014 13:20:15 +0100 Hmm... sadly, (import (scheme base)) fails with Chicken and Gauche. Development head of Gauche already supports r7rs, FYI. --shiro ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
Shiro Kawai scripsit: Development head of Gauche already supports r7rs, FYI. Excellent news! -- John Cowan co...@ccil.orghttp://ccil.org/~cowan No man is an island, entire of itself; every man is a piece of the continent, a part of the main. If a clod be washed away by the sea, Europe is the less, as well as if a promontory were, as well as if a manor of thy friends or of thine own were: any man's death diminishes me, because I am involved in mankind, and therefore never send to know for whom the bell tolls; it tolls for thee. --John Donne ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
[Chicken-users] Review my Caesar Cipher?
Hello, I found a Scheme implementation of the Caesar cipher on Rosetta Code. It said This was written by a novice, please review... So I reviewed it, and basically rewrote it. I think my version is much better (clearer) but since I too am a novice, I feel bad removing the novice warning. Could someone who has used Scheme longer than two weeks have a quick look at my work and tell me if I can remove the warning (or make corrections if my code is not idiomatic or something)? My version(s): http://rosettacode.org/wiki/Caesar_cipher#Scheme Previous version: http://rosettacode.org/mw/index.php?title=Caesar_cipheroldid=177675#scheme Cheers, Daniel. -- When an engineer says that something can't be done, it's a code phrase that means it's not fun to do. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
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 Then here are your two examples; decryption is just encryption by the negative: (caesar To craunch the marmoset. 1) UP DSBVODI UIF NBSNPTFU. (caesar UP DSBVODI UIF NBSNPTFU. -1) TO CRAUNCH THE MARMOSET. On Mon, Mar 10, 2014 at 9:51 AM, Daniel Carrera dcarr...@gmail.com wrote: Hello, I found a Scheme implementation of the Caesar cipher on Rosetta Code. It said This was written by a novice, please review... So I reviewed it, and basically rewrote it. I think my version is much better (clearer) but since I too am a novice, I feel bad removing the novice warning. Could someone who has used Scheme longer than two weeks have a quick look at my work and tell me if I can remove the warning (or make corrections if my code is not idiomatic or something)? My version(s): http://rosettacode.org/wiki/Caesar_cipher#Scheme Previous version: http://rosettacode.org/mw/index.php?title=Caesar_cipheroldid=177675#scheme Cheers, Daniel. -- When an engineer says that something can't be done, it's a code phrase that means it's not fun to do. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
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 ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
Am 10.03.2014 15:51, schrieb Daniel Carrera: Hello, I found a Scheme implementation of the Caesar cipher on Rosetta Code. It said This was written by a novice, please review... So I reviewed it, and basically rewrote it. I think my version is much better (clearer) I tend to agree that your version is clearer. Especially because it's shorter and uses fewer let-scopes. As this is an exercise, I'd take an issue with the use of list-ref in the replacements. List-ref is O(n) in the size of the list. Once should try to avoid that. 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. ;-) 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. 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. but since I too am a novice, I feel bad removing the novice warning. Could someone who has used Scheme longer than two weeks have a quick look at my work and tell me if I can remove the warning (or make corrections if my code is not idiomatic or something)? My version(s): http://rosettacode.org/wiki/Caesar_cipher#Scheme Previous version: http://rosettacode.org/mw/index.php?title=Caesar_cipheroldid=177675#scheme Cheers, Daniel. -- When an engineer says that something can't be done, it's a code phrase that means it's not fun to do. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
I used only the procedures provided by RnRS, instead of loading SRFI-13, but you could use string-map if you want to. For those who prefer to roll their own, here is a simple version of string-map! that mutates the string in place: (define (string-map! f str) (do ((i 0 (+ i 1))) ((= i (string-length str)) str) (string-set! str i (f (string-ref str i) Or you could convert to a list, perform the mapping, and convert back, as I did in my original version of the function. With string-map!, the caesar function changes to this: (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)) (string-map! char-plus str)) For purposes of Rosetta Code, it's probably better to avoid SRFI-13 and stay with RnRS, as in my first version of the function. On Mon, Mar 10, 2014 at 10:31 AM, Peter Bex peter@xs4all.nl wrote: 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 ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
Thanks. I think it's fair to use SRF-13. Now that I learned some character functions from Phil, I think the following is nice and compact: (define (caesar char) (if (not (char-alphabetic? char)) char ; Return other chars verbatim. (let ((i (- (char-integer (char-upcase char)) 65))) (integer-char (+ 65 (modulo (+ i key) 26)) (print (string-map caesar msg)) Cheers, Daniel. On 10 March 2014 16:31, Peter Bex peter@xs4all.nl wrote: 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 -- When an engineer says that something can't be done, it's a code phrase that means it's not fun to do. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
Daniel Carrera scripsit: (define (caesar char) (if (not (char-alphabetic? char)) char ; Return other chars verbatim. (let ((i (- (char-integer (char-upcase char)) 65))) (integer-char (+ 65 (modulo (+ i key) 26)) (print (string-map caesar msg)) This isn't i18n-safe, because char-alphabetic? can return #t on non-Latin letters. Convert to an integer first and make sure it's in the safe range. Then add a comment to the effect that this assumes a Scheme in which char-integer and integer-char preserve the ASCII range. (Almost all Schemes do so, but it's not required by R5RS.) General comments not relevant to this code: 1) Rolling your own string-map is tricky, because of the R7RS requirement that a call/cc from the mapping function work correctly: If multiple returns occur from `string-map`, the values returned by earlier returns are not mutated. 2) `Use` is Chicken-specific. There is no fully standard way to load/import a module prior to R6RS/R7RS, but at least `require-extension` (which is also implemented in Chicken) is the subject of SRFI 55. -- MEET US AT POINT ORANGE AT MIDNIGHT BRING YOUR DUCK OR PREPARE TO FACE WUGGUMS John Cowan co...@ccil.org http://www.ccil.org/~cowan ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
On 10 March 2014 17:10, John Cowan co...@mercury.ccil.org wrote: This isn't i18n-safe, because char-alphabetic? can return #t on non-Latin letters. Convert to an integer first and make sure it's in the safe range. Then add a comment to the effect that this assumes a Scheme in which char-integer and integer-char preserve the ASCII range. (Almost all Schemes do so, but it's not required by R5RS.) ... 2) `Use` is Chicken-specific. There is no fully standard way to load/import a module prior to R6RS/R7RS, but at least `require-extension` (which is also implemented in Chicken) is the subject of SRFI 55. I am trying to write an R7RS-compliant version. R7RS would give me import, as well as char-integer and integer-char. The problem I'm having is that my code does not work when I compile it, or when I use csi -s, but it works perfectly well when I paste it directly into the csi REPL. Here is what I have: ; ; Unicode-safe. Requires an R7RS-compliant Scheme. ; (import (srfi 13)) ; String library. (define msg The quick brown fox jumps over the lazy fox.) (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)) (cond ((and (= c A) (= c Z)) (integer-char (+ A (modulo (+ key (- c A)) 26 ((and (= c a) (= c z)) (integer-char (+ a (modulo (+ key (- c a)) 26 (else char))) ; Return other characters verbatim. (print (string-map caesar msg)) When I compile this and run it, I get an error saying that string-map is not defined. Same thing happens with csi -s. So clearly it is not loading SRFI-13... Any ideas? Cheers, Daniel. -- When an engineer says that something can't be done, it's a code phrase that means it's not fun to do. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
On 10 March 2014 20:04, Daniel Carrera dcarr...@gmail.com wrote: I am trying to write an R7RS-compliant version. R7RS would give me import, as well as char-integer and integer-char. The problem I'm having is that my code does not work when I compile it, or when I use csi -s, but it works perfectly well when I paste it directly into the csi REPL. After a tip from Erik, I have isolated the issue. The (import) only works correctly if you first run (use posix). My REPL was loading posix because I loaded readline. The following code compiles and runs correctly: (use posix) ; ; Unicode-safe. Requires an R7RS-compliant Scheme. ; (import (srfi 13)) ; String library. (define msg The quick brown fox jumps over the lazy fox.) (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)) (cond ((and (= c A) (= c Z)) (integer-char (+ A (modulo (+ key (- c A)) 26 ((and (= c a) (= c z)) (integer-char (+ a (modulo (+ key (- c a)) 26 (else char))) ; Return other characters verbatim. (print (string-map caesar msg)) Cheers, Daniel. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] Review my Caesar Cipher?
On Tue, Mar 11, 2014 at 6:16 AM, Daniel Carrera dcarr...@gmail.com wrote: On 10 March 2014 20:04, Daniel Carrera dcarr...@gmail.com wrote: I am trying to write an R7RS-compliant version. R7RS would give me import, as well as char-integer and integer-char. The problem I'm having is that my code does not work when I compile it, or when I use csi -s, but it works perfectly well when I paste it directly into the csi REPL. After a tip from Erik, I have isolated the issue. The (import) only works correctly if you first run (use posix). My REPL was loading posix because I loaded readline. The following code compiles and runs correctly: (use posix) ; ; Unicode-safe. Requires an R7RS-compliant Scheme. ; (import (srfi 13)) ; String library. (define msg The quick brown fox jumps over the lazy fox.) (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)) (cond ((and (= c A) (= c Z)) (integer-char (+ A (modulo (+ key (- c A)) 26 ((and (= c a) (= c z)) (integer-char (+ a (modulo (+ key (- c a)) 26 (else char))) ; Return other characters verbatim. (integer-char (cond ((= A c Z) (+ A (modulo (+ key (- c A)) 26))) ((= a c z) (+ a (modulo (+ key (- c A)) 26))) (else c))) (print (string-map caesar msg)) Cheers, Daniel. ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users ___ Chicken-users mailing list Chicken-users@nongnu.org https://lists.nongnu.org/mailman/listinfo/chicken-users