School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,103 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,997 people online right now. Registration is fast and FREE... Join Now!




Lisp and caesar's cipher

 

Lisp and caesar's cipher

RaydPanda

1 Nov, 2009 - 07:01 AM
Post #1

New D.I.C Head
*

Joined: 1 Nov, 2009
Posts: 2


My Contributions
Hello all,

I am a big Lisp-Newb and am struggling with a program that is supposed to encrypt a string with Caesar's cipher. What I have in mind is a function that takes in 2 arguments: a string and a number to specify the modulus. Something along the lines:

(defun c-encrypt (string number)
("lots of helper functions"))

My thoughts so far are that it would be best to set the alphabet as a parameter and write a function that looks up a char's position in the parameter and then shifts it the wanted Caesar's modulo to the right e.g. A ->modulo 7 -> H

May look something along the lines:

CODE
(defparameter *alphabet* " abcdefghijklmnopqrstuvwxyz")
(defun alphabet-index (character) (...)) "looks up the index of the character in *alphabet*"
(defun shift (character x) ((alphabet-index (character)) (mod(+24 x"being the wanted modulo")27)))


Then I could take that function and mapcar it over the list of characters that are the input-string in single letters:


Well, I ran headfirst into a lot of problems. First of all, I can not find a function to split a string into its single chars (convert it to numbers fine, but simply split it...).
When trying to concatenate or looping there are always these darn #\ left which make it impossible to use the parameter.

As you see I already fail at the first obstacle of the whole project... blush.gif Not to speak about the rest...

Any help would be greatly appreciated since I really start to get frustrated about this project and Lisp itself. For some reason I find it extremely difficult to "think Lisp".

Thanks already for your answers!

User is offlineProfile CardPM
+Quote Post


Raynes

RE: Lisp And Caesar's Cipher

7 Nov, 2009 - 03:00 PM
Post #2

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
I'm afraid there is very little I can do for Scheme (I guess that's Scheme), but my implementation in Clojure might help you a little bit. http://github.com/Raynes/Cipher/blob/maste...pher/cipher.clj

Remember that Lisp is not a language. Lisp is a family of languages. You can't just say "I'm coding in Lisp." and expect people to know what you're talking about. wink2.gif
User is offlineProfile CardPM
+Quote Post

RaydPanda

RE: Lisp And Caesar's Cipher

8 Nov, 2009 - 12:46 PM
Post #3

New D.I.C Head
*

Joined: 1 Nov, 2009
Posts: 2


My Contributions
QUOTE(Raynes @ 7 Nov, 2009 - 03:00 PM) *

I'm afraid there is very little I can do for Scheme (I guess that's Scheme), but my implementation in Clojure might help you a little bit. http://github.com/Raynes/Cipher/blob/maste...pher/cipher.clj

Remember that Lisp is not a language. Lisp is a family of languages. You can't just say "I'm coding in Lisp." and expect people to know what you're talking about. wink2.gif


Thanks for the answer and you are of course right, I should have specified that I use Slime and Emacs (Lispbox) on a Windows machine. wink2.gif Anyway, by now I figured the problem out like this:

CODE
; 1) Program that takes in a word(given as a string) plus a number and encrypts the word using Caesar's Cipher in the modulo given as the number:

; Alphabet as parameter plus one white space at index 0

(defparameter *alphabet* " aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ,0.1?2!3;4:56789")

; Finds the index of char in  *alphabet*:

(defun alphabet-index (char)
  (position char *alphabet*))

; Finds the char shifted n places to the right in the alphabet:

(defun rotate (char n)
  (elt *alphabet* (mod (+ (alphabet-index char) n)(length *alphabet*))))

; makes the rotate function accessible for map

(defun e-helper (n)
  #'(lambda (char)
      (rotate char n)))


; Returns the encrypted string

(defun encrypt (str &optional (n 7))
  (map 'string (e-helper n) str))

User is offlineProfile CardPM
+Quote Post

Raynes

RE: Lisp And Caesar's Cipher

8 Nov, 2009 - 07:04 PM
Post #4

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
QUOTE(RaydPanda @ 8 Nov, 2009 - 12:46 PM) *

QUOTE(Raynes @ 7 Nov, 2009 - 03:00 PM) *

I'm afraid there is very little I can do for Scheme (I guess that's Scheme), but my implementation in Clojure might help you a little bit. http://github.com/Raynes/Cipher/blob/maste...pher/cipher.clj

Remember that Lisp is not a language. Lisp is a family of languages. You can't just say "I'm coding in Lisp." and expect people to know what you're talking about. wink2.gif


Thanks for the answer and you are of course right, I should have specified that I use Slime and Emacs (Lispbox) on a Windows machine. wink2.gif Anyway, by now I figured the problem out like this:

CODE
; 1) Program that takes in a word(given as a string) plus a number and encrypts the word using Caesar's Cipher in the modulo given as the number:

; Alphabet as parameter plus one white space at index 0

(defparameter *alphabet* " aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ,0.1?2!3;4:56789")

; Finds the index of char in  *alphabet*:

(defun alphabet-index (char)
  (position char *alphabet*))

; Finds the char shifted n places to the right in the alphabet:

(defun rotate (char n)
  (elt *alphabet* (mod (+ (alphabet-index char) n)(length *alphabet*))))

; makes the rotate function accessible for map

(defun e-helper (n)
  #'(lambda (char)
      (rotate char n)))


; Returns the encrypted string

(defun encrypt (str &optional (n 7))
  (map 'string (e-helper n) str))



That means you're using Common Lisp. It's one of the most, probably the most popular Lisp dialect right now. If you enjoy Lisp, and want to do some serious work with it, I suggest you check out Clojure sometime. It's teh awesome. Good luck, and have fun Lisping. anime1.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 12:26PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month