是什么原因导致“ Prelude.chr:错误的论点”?

如何解决是什么原因导致“ Prelude.chr:错误的论点”?

我有以下编写的Haskell程序,其目的是像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

我的ghc版本如下:

$ ghc --version
The Glorious Glasgow Haskell Compilation System,version 8.6.5

我在macOS(Catalina)上编译了Haskell:

$ 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 ...

然后我运行我的代码:

$ 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)

为什么我得到Prelude.chr: bad argument?是什么原因导致的,我该怎么解决该问题?

我已经读到其他有此错误的人,但就他们而言,删除*.hi文件可以解决此问题。我删除了Prog1d.hi(以及Prog1d.oProg1d),但没有任何效果。我觉得这可能是我的代码中的某些内容引起的,也许与第41行有关:

then putStrLn $ show $ map (shiftLeft $ read $ aInt) message

但这行与第38行类似,在-encode用例中也可以正常工作。我肯定想念一些明显的东西。

我是Haskell的新手,所以请帮助我。 我主要习惯于使用命令性语言(例如C ++,python,Java等)编写代码,但我还不熟悉Haskell和其他功能语言的概念和语法。

感谢阅读!

解决方法

我发现了问题。

在我的代码中,我将一个字符转换为其相应的ASCII代码。

例如,fromEnum 'A'会给您65。问题是,如果您将值从ASCII值表中移出,例如-1,您为toEnum引起了一个错误的参数错误,我用来比较以检查移位是否太远。

例如:

echo "A" | ./Prog1d -decode 66

这将使我进入程序的第25行:

25     then if (toEnum (fromEnum char - shift) :: Char) < 'A'

fromEnum char将是fromEnum 'A',这将给我65

然后我从65中减去shift的值:66。因此,65 - 66 = -1

然后toEnum -1导致Prelude.chr: bad argument: (-1)

这是因为-1没有ASCII字符,并且该值超出范围。

这意味着我只需要比较int值而不是char即可检查我的移位是否超出范围。

这是更正的代码:

import System.IO
import System.Environment
import System.Exit
import Data.Char
 
shiftRight :: Int -> Char -> Char
shiftRight shift char = do
  if isAsciiLower char 
  then if (fromEnum char + shift) > 122 -- 122 is 'z' in ASCII
    then shiftRight (shift - 26) char
    else toEnum (fromEnum char + shift) :: Char
  else if isAsciiUpper char
    then if (fromEnum char + shift) > 90 -- 90 is `Z` in ASCII
      then shiftRight (shift - 26) char
      else toEnum (fromEnum char + shift) :: Char
    else char
 
shiftLeft :: Int -> Char -> Char
shiftLeft shift char = do
  if isAsciiLower char 
  then if (fromEnum char - shift) < 97 -- 97 is 'a' in ASCII
    then shiftLeft (shift - 26) char
    else toEnum (fromEnum char - shift) :: Char
  else if isAsciiUpper char
    then if (fromEnum char - shift) < 65 -- 65 is 'A' in ASCII
      then shiftLeft (shift - 26) char
      else toEnum (fromEnum char - shift) :: Char
    else char
 
main = do
  args <- getArgs
  message <- getLine
  case args of
    [aString,aInt] -> 
      if aString == "-encode"
      -- `read` converts aInt from string to int
      -- `map` is used to apply `shiftRight` to each char in the string `message`
      then putStrLn $ map (shiftRight $ read $ aInt) message
      else 
        if aString == "-decode"
        then putStrLn $ map (shiftLeft $ read $ aInt) message
        else do
          putStrLn ("Second argument should be either '-decode' or '-encode'!")
          exitFailure
    _ -> do 
      progName <- getProgName
      putStrLn ("Usage: " ++ progName ++ " [-encode|-decode] [0-9]")
      exitFailure

这是正确的所需输出:

$ 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
ZABWXYzabwxy
$ echo "ABCXYZabcxyz" | ./Prog1d -decode 2
YZAVWXyzavwx
$ echo "ABCXYZabcxyz" | ./Prog1d -decode 4
WXYTUVwxytuv
$ echo "ABCXYZabcxyz" | ./Prog1d -decode 100
EFGBCDefgbcd

编辑(2020-09-01):John Purdy给了我一些反馈,这促使我从根本上重构代码。这是改进的版本:

import System.IO
import System.Environment
import System.Exit
import Data.Char

shift :: Int -> Char -> Char
shift amount char
  -- `ord` is `fromEnum` but only for `Char` types; converts a Char to an Int,-- which gives us the ASCII number for that character
  -- see: https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Char.html#v:ord
  | isAsciiLower char && (shifted < ord 'a' || shifted > ord 'z') = shift cycled char
  -- `chr` is `toEnum` but only for `Char` types; it converts from an Int to Char
  -- see: https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-Char.html#v:chr
  | isAsciiLower char = chr shifted
  | isAsciiUpper char && (shifted < ord 'A' || shifted > ord 'Z') = shift cycled char
  | isAsciiUpper char = chr shifted 
  | otherwise = char
  where shifted = ord char + amount -- e.g. 'A' (ASCII: 66) shifted 1 = 'B' (ASCII: 67),so shifted would be 67. 
        -- cycled: the shift amount,but cycled by 26 to start over the alphabet,-- e.g. 'Z' (by ASCII: 90) shifted 1 is 91,so -26 to get 65,which is 'A'
        -- the amount `div` amount makes sure we add or subtract 26 as needed to cycle 
        -- e.g. 'A' (65) shifted -1 is 64,but if we subtracted 26 from 64,it wouldn't cycle us to Z,-- it is in the wrong direction
        -- so instead we multiply by the sign of the amount: -1 - (26 * (-1 / |-1|) to get +25,-- so 65 ('A') + 25 = 90 ('Z')
        cycled = amount - ((amount `div` abs (amount)) * 26) 

main = do
  args <- getArgs
  message <- getLine
  case args of
      -- `read` converts aInt from string to int
      -- `map` is used to apply `shiftRight` to each char in the string `message`
    ["-encode",aInt] -> putStrLn $ map (shift $ read $ aInt) message
    ["-decode",aInt] -> putStrLn $ map (shift $ negate $ read $ aInt) message
    _ -> do 
      progName <- getProgName
      putStrLn ("Usage: " ++ progName ++ " [-encode|-decode] [0-9]")
      exitFailure

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-