我的程序中从素数获取素数的一个错误

时间:2019-04-23 13:53:55

标签: haskell

通过虚部下的程序部分,计算并输出素数的所有素数。虚线上的程序部分从2到无穷大的素数。

错误发生在下一行

   |npf m primnumber == True = head primnumber ++ (primfactor (m / head primnumber):[])

警告显示为:

E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs:17:33: error:
    * Couldn't match expected type `[[Int]]' with actual type `Int'
    * In the first argument of `(++)', namely `head primnumber'
      In the expression:
        head primnumber ++ (primfactor (m / head primnumber) : [])
      In an equation for `primfactor':
          primfactor m
            | npf m primnumber == True
            = head primnumber ++ (primfactor (m / head primnumber) : [])
   |
17 |     |npf m primnumber == True = head primnumber ++ (primfactor (m / head primnumber):[])

   |                                 ^^^^^^^^^^^^^^^

E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs:17:33: error:
    * Couldn't match type `[Int]' with `Int'
      Expected type: [Int]
        Actual type: [[Int]]
    * In the expression:
        head primnumber ++ (primfactor (m / head primnumber) : [])
      In an equation for `primfactor':
          primfactor m
            | npf m primnumber == True
            = head primnumber ++ (primfactor (m / head primnumber) : [])
   |
17 |     |npf m primnumber == True = head primnumber ++ (primfactor (m / head primnumber):[])

   |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[Finished in 2.8s]

我已经检查了该行中的表达式很多次了。但是我不知道原因。


import Data.List

main :: IO ()    -- This says that main is an IO action.
main = return () -- This tells main to do nothing

--the second primnumer Function from leture
primnumber = 2: [n| n<-[3..], prim n primnumber]
prim n (p:xs) 
    |p*p > n = True
    |n `mod` p == 0 = False
    |otherwise = prim n xs

-------------------------------------------------------

primfactor :: Int -> [Int]
primfactor m 
    |npf m primnumber == True = head primnumber ++ (primfactor (m / head primnumber):[])

npf n (x:xs)
    |n `mod` x == 0 = True
    |otherwise = npf n xs 


校正后

import Data.List

main :: IO ()    -- This says that main is an IO action.
main = return () -- This tells main to do nothing

--the second primnumer Function from leture

primnumber = 2: [n| n<-[3..], prim n primnumber]
prim n (p:xs) 
    |p*p > n = True
    |n `mod` p == 0 = False
    |otherwise = prim n xs

-------------------------------------------------------
npf :: Integer->[Integer]->Integer
npf n (x:xs)
    |n `mod` x == 0 = x 
    |n `mod` x == 1 = 1
    |otherwise = npf n xs 

primfactor :: Integer -> [Integer]
primfactor 1 = []
primfactor m = [npf m primnumber] ++ ((primfactor (m `div` (npf m primnumber)):[])

错误


E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs:23:83: error:
    parse error (possibly incorrect indentation or mismatched brackets)
   |
23 | primfactor m = [npf m primnumber] ++ ((primfactor (m `div` (npf m primnumber)):[])
   |                                                                                   ^
[Finished in 0.5s with exit code 1]
[shell_cmd: runhaskell "E:\Haskell\Uebungsblatt_2_Aufgabe_1.hs"]
[dir: E:\Haskell]
[path: C:\Program Files\Haskell\bin;E:\8.6.3\lib\extralibs\bin;E:\8.6.3\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;E:\matlab\bin;E:\8.6.3\mingw\bin;C:\Users\HuFengXiang\AppData\Roaming\cabal\bin;C:\Users\HuFengXiang\AppData\Roaming\local\bin]

1 个答案:

答案 0 :(得分:3)

primnumber :: [Int]     -- a list of numbers
head primnumber :: Int  -- a single number
(++) :: [Int] -> [Int] -> [Int]

head primnumber不能是运算符(++)的第一个(左)参数,因为它的类型错误:运算符需要一个列表(在这种情况下为[Int]),但是您需要正在提供Int

一开始,您的代码尚不完全清楚应该执行的操作,因此我无法告诉您如何准确地对其进行修复。如果您打算将head primfactor“加”到列表中,则可以将其包装在这样的列表中:

[head primnumber] ++ (primfactor (m / head primnumber):[])

或者更好的是,直接使用(:)构造函数:

(head primnumber) : (primfactor (m / head primnumber):[])

如果您想做其他事情,请指定它是什么。