Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 9 cipher code doesn't work #3

Open
ilostthegame opened this issue Sep 11, 2024 · 0 comments
Open

Chapter 9 cipher code doesn't work #3

ilostthegame opened this issue Sep 11, 2024 · 0 comments

Comments

@ilostthegame
Copy link

The cipher code doesn't cycle around the letters when for example "zZ" is inputted as the string: it instead goes to the next unicode letters.

While a function using ord/chr and mods is feasible, I instead just used a recursive function that used a large number of guard blocks:

enumCipher :: String -> Int -> String
enumCipher str shift
  | shift < 0  = enumCipher str (mod (26 - shift) 26)
  | shift > 26 = enumCipher str (mod shift 26)
  | shift == 0 = str
  | otherwise  = enumCipher (map shiftFunc str) (shift - 1)
  where shiftFunc chr
          | chr == 'z'  = 'a'
          | chr == 'Z'  = 'A'
          | otherwise   = succ chr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant