Skip to content

Chapter 9 cipher code doesn't work #3

@ilostthegame

Description

@ilostthegame

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions