I have the following Haskell program I wrote, the purpose of which is to function like a Caesar cipher:
1 import System.IO
2 import System.Environment
3 import System.Exit
4 import Data.Char
5
6 shiftRight :: Int -> Char -> Char
7 shiftRight shift char = do
8 if isAsciiLower char
9 then if (toEnum (fromEnum char + shift) :: Char) > 'z'
10 then shiftRight (shift - 26) char
11 else toEnum (fromEnum char + shift) :: Char
12 else if isAsciiUpper char
13 then if (toEnum (fromEnum char + shift) :: Char) > 'Z'
14 then shiftRight (shift - 26) char
15 else toEnum (fromEnum char + shift) :: Char
16 else char
17
18 shiftLeft :: Int -> Char -> Char
19 shiftLeft shift char = do
20 if isAsciiLower char
21 then if (toEnum (fromEnum char - shift) :: Char) < 'a'
22 then shiftLeft (shift + 26) char
23 else toEnum (fromEnum char - shift) :: Char
24 else if isAsciiUpper char
25 then if (toEnum (fromEnum char - shift) :: Char) < 'A'
26 then shiftLeft (shift + 26) char
27 else toEnum (fromEnum char - shift) :: Char
28 else char
29
30 main = do
31 args <- getArgs
32 message <- getLine
33 case args of
34 [aString, aInt] ->
35 if aString == "-encode"
36 -- `read` converts aInt from string to int
37 -- `map` is used to apply `shiftRight` to each char in the string `message`
38 then putStrLn $ show $ map (shiftRight $ read $ aInt) message
39 else
40 if aString == "-decode"
41 then putStrLn $ show $ map (shiftLeft $ read $ aInt) message
42 else do
43 putStrLn ("Second argument should be either '-decode' or '-encode'!")
44 exitFailure
45 _ -> do
46 progName <- getProgName
47 putStrLn ("Usage: " ++ progName ++ " [-encode|-decode] [0-9]")
48 exitFailure
My ghc
version is as follows:
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.6.5
I compile the Haskell on macos (Catalina):
$ ghc Prog1d.hs -o Prog1d
Loaded package environment from $HOME/.ghc/x86_64-darwin-8.6.5/environments/default
[1 of 1] Compiling Main ( Prog1d.hs, Prog1d.o )
Linking Prog1d ...
Then I run my code:
$ echo "ABCXYZabcxyz" | ./Prog1d -encode 1
"BCDYZAbcdyza"
$ echo "ABCXYZabcxyz" | ./Prog1d -encode 2
"CDEZABcdezab"
$ echo "ABCXYZabcxyz" | ./Prog1d -encode 4
"EFGBCDefgbcd"
$ echo "ABCXYZabcxyz" | ./Prog1d -encode 100
"WXYTUVwxytuv"
$ echo "ABCXYZabcxyz" | ./Prog1d -decode 1
Prog1d: Prelude.chr: bad argument: (-14)
$ echo "ABCXYZabcxyz" | ./Prog1d -decode 2
Prog1d: Prelude.chr: bad argument: (-15)
$ echo "ABCXYZabcxyz" | ./Prog1d -decode 4
Prog1d: Prelude.chr: bad argument: (-17)
$ echo "ABCXYZabcxyz" | ./Prog1d -decode 100
Prog1d: Prelude.chr: bad argument: (-35)
Why do I get Prelude.chr: bad argument
? What causes this, and what can I do to fix the problem?
I have read about others who have had this error, but in their case, deleting the *.hi
files solved the problem. I have deleted Prog1d.hi
(as well as Prog1d.o
and Prog1d
), but to no effect. I feel this may be caused by something in my code, maybe with line 41:
then putStrLn $ show $ map (shiftLeft $ read $ aInt) message
But this line is just like line 38, which works just fine for the -encode
use-case. I must be missing something obvious.
I am new to Haskell, so please help me along. I am mostly used to writing code in imperative languages like C++, python, Java, etc. and I am not yet familiar with the ideas and syntax of Haskell and other functional languages.
Thanks for reading this!
I found the problem.
In my code, I convert a character into its corresponding ASCII code.
For example,
fromEnum 'A'
will give you65
. The problem is that, if you shift the value off the table of ASCII values, e.g.-1
, you cause a bad argument error for thetoEnum
I use to compare for checking if the shift went too far.For example:
This will take me to line 25 in my program:
fromEnum char
will befromEnum 'A'
which will give me65
.Then I subtract from
65
theshift
value:66
. So,65 - 66 = -1
.Then
toEnum -1
causesPrelude.chr: bad argument: (-1)
.That's because there is no ASCII character for
-1
and that value is out of bounds.This means I simply have to compare int values instead of chars to check whether my shift is out of bounds or not.
Here is the corrected code:
And here is the proper, desired output:
EDIT (2020-09-01): John Purdy gave me some feedback, which prompted me to radically refactor my code. Here is the improved version: