module Examples where
import System.Random
import Data.List
cars = ["Ferrari", "Audi", "Honda","McLaren","Merc"]
cmp (x1,y1) (x2,y2) = compare y1 y2
[car | (car, n) <- sortBy cmp(zip cars (randoms (mkStdGen 123456) :: [Int]))]
我一直得到这个错误:
解析错误:模块头,导入声明或者期望的顶级声明。|7 | [car | (car, n) <- sortBy cmp(zip cars (random (mkStdGen 123456):: [Int]))]| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^:重载
有没有人对我有任何想法来修复这个,使这个函数可以运行
你的列表推导不是一个函数,它是一个在顶层定义的表达式,这没有多大意义。您可以定义一个输出结果的main
,例如:
module Examples where
import System.Random
import Data.List
cars :: [String]
cars = ["Ferrari", "Audi", "Honda","McLaren","Merc"]
cmp :: Ord b => (a, b) -> (a, b) -> Ordering
cmp (x1,y1) (x2,y2) = compare y1 y2
main :: IO ()
main = print[car | (car, n) <- sortBy cmp(zip cars (randoms (mkStdGen 123456) :: [Int]))]