数字转换器不工作

时间:2017-07-11 17:19:51

标签: haskell typeerror

它应该是不同数字系统的转换器的开始

numCon :: [Int] -> Int -> Int -> Int -> Int
numCon a c d = numCon_1 (reverse a) (length a) c d

- >这应该只是一个预处理来获取输入的数字的长度,也可以反转列表,以便我可以从一开始就通过它 (它有时会破坏某些东西,这就是我把它放入的原因)

numCon_1 :: [Int] -> Int -> Int -> Int -> Int -> Int
numCon_1 [] _ _ _ = 0 0
numCon_1 (x:xs) len insys outsys = ( x * (insys^((len - length xs)-1) ) + (numCon_1 (xs) len insys outsys)) outsys

- >这是不起作用的部分

无论我尝试什么,我总是得到某种形式的错误,但主要是这个:

ZahlsysthemKonverter.hs:10:36: error:
• Couldn't match expected type ‘Int -> Int -> Int’
              with actual type ‘Int’
• The function ‘x * (insys ^ ((len - length xs) - 1))
                + (numCon_1 (xs) len insys outsys)’
  is applied to one argument,
  but its type ‘Int’ has none
  In the expression:
    (x * (insys ^ ((len - length xs) - 1))
     + (numCon_1 (xs) len insys outsys))
      outsys
  In an equation for ‘numCon_1’:
      numCon_1 (x : xs) len insys outsys
        = (x * (insys ^ ((len - length xs) - 1))
           + (numCon_1 (xs) len insys outsys))
            outsys

ZahlsysthemKonverter.hs:10:76: error:
• Couldn't match expected type ‘Int’ with actual type ‘Int -> Int’
• Probable cause: ‘numCon_1’ is applied to too few arguments
  In the second argument of ‘(+)’, namely
    ‘(numCon_1 (xs) len insys outsys)’
  In the expression:
    x * (insys ^ ((len - length xs) - 1))
    + (numCon_1 (xs) len insys outsys)
  In the expression:
    (x * (insys ^ ((len - length xs) - 1))
     + (numCon_1 (xs) len insys outsys))
      outsys

好的再次看一下我的主要问题似乎是,我在()中使用(x * (insys^...)创建了一个函数,这个outsys是它应用了,所以这个我可以解决,但另一个坚持。

有没有四个Aguments:(xs),len,insys,outsys?

或者是因为xs可以是[]我应该如何解决它?

1 个答案:

答案 0 :(得分:0)

评论已经解决了为什么你的程序没有编译。我将尝试猜测你想做什么,并提出一种更惯用的方式来编写它。

-- example: fromBase 10 [1,2,3] == 123
fromBase base digits = sum $ zipWith (*) (reverse digits) powers
  where powers = iterate (*base) 1

-- example: toBase 10 123 == [1,2,3]
toBase base number | number == 0 = [0]
                   | otherwise   = reverse $ toBase' base number
  where toBase' n | n == 0    = []
                  | otherwise = let (q,r) = n `divMod` base in
                                  r : toBase' q

changeBase from to digits = toBase to $ fromBase from digits