【问题标题】:How can I run this function when it can work on CMD当它可以在 CMD 上运行时,如何运行此功能
【发布时间】:2021-10-30 17:19:19
【问题描述】:
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, n)

有没有人知道我可以解决这个问题,以便这个函数可以运行

【问题讨论】:

  • 你在顶层写了一个表达式。这没有多大意义。例如,您可以定义一个打印结果的main

标签: haskell declaration


【解决方案1】:

您的列表推导式不是函数,它是在顶层定义的表达式,没有多大意义。您可以定义一个打印结果的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]))]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2022-06-12
    • 1970-01-01
    • 2017-01-11
    相关资源
    最近更新 更多