使用标准的类型错误

发布于 2024-09-29 12:42:47 字数 692 浏览 2 评论 0原文

我阅读了文档和一些讨论该包的文章,但我是 Haskell 的新手,不太了解,但我尝试了......

以下是我所做的:

module Main where  
{-# LANGUAGE BangPatterns #-}   
import Control.Parallel(par,pseq)  
import Control.Exception  
import Data.List  
import IO  
import Data.Char  
import Criterion.Main (defaultMain, bench)  

learquivo :: FilePath -> IO ([[Int]])  
learquivo "mkList1.txt"  = do   
    conteudo <- readFile "mkList1.txt" 
    return (read conteudo) 


main = defaultMain [  
    bench "map sort learquivo" $ \n -> map sort learquivo
    ]

正如它所做的那样,发生了以下错误:

Couldn't match expected type [[a]]
       against inferred type FilePath -> IO [[Int]]

I read the documentation and some articles that talk about the package, but I'm new to Haskell and did not understand much but I tried ....

Below is what I did:

module Main where  
{-# LANGUAGE BangPatterns #-}   
import Control.Parallel(par,pseq)  
import Control.Exception  
import Data.List  
import IO  
import Data.Char  
import Criterion.Main (defaultMain, bench)  

learquivo :: FilePath -> IO ([[Int]])  
learquivo "mkList1.txt"  = do   
    conteudo <- readFile "mkList1.txt" 
    return (read conteudo) 


main = defaultMain [  
    bench "map sort learquivo" $ \n -> map sort learquivo
    ]

As it did the following error occurred:

Couldn't match expected type [[a]]
       against inferred type FilePath -> IO [[Int]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2024-10-06 12:42:47

为了让您了解我通常如何运行它,使用 nfwhnf 函数,我将给出我的代码:

import Data.List
import Criterion.Main

main :: IO ()
main = do
   -- content <- learquivo "mkList1.txt"  
   let content = [ [big, big - step.. 0] | big <- [1000..1010], step <- [1..5]] :: [[Int]]
   defaultMain
        [ bench "benchmark-name" (nf (map sort) content)]

编辑:如果您喜欢这个,那么也尝试一下绘图:

module Main where

import Data.List
import Criterion.Main
import Criterion.Config
import Criterion.MultiMap as M

main :: IO ()
main = do
   let myConfig = defaultConfig {
              -- Always display an 800x600 window with curves.
              cfgPlot = M.singleton KernelDensity (Window 800 600)
              }
   let content = [ [big, big-step.. 0] | big <- [1000..1010], step <- [1..5]] :: [[Int]]
   defaultMainWith myConfig (return ())
        [ bench "benchmark-name" (nf (map sort) content)]

Just so you have how I usually run it, using the nf or whnf functions, I'll give my code:

import Data.List
import Criterion.Main

main :: IO ()
main = do
   -- content <- learquivo "mkList1.txt"  
   let content = [ [big, big - step.. 0] | big <- [1000..1010], step <- [1..5]] :: [[Int]]
   defaultMain
        [ bench "benchmark-name" (nf (map sort) content)]

EDIT: If you like this then also give plotting a try:

module Main where

import Data.List
import Criterion.Main
import Criterion.Config
import Criterion.MultiMap as M

main :: IO ()
main = do
   let myConfig = defaultConfig {
              -- Always display an 800x600 window with curves.
              cfgPlot = M.singleton KernelDensity (Window 800 600)
              }
   let content = [ [big, big-step.. 0] | big <- [1000..1010], step <- [1..5]] :: [[Int]]
   defaultMainWith myConfig (return ())
        [ bench "benchmark-name" (nf (map sort) content)]
等风来 2024-10-06 12:42:47

问题是这样的:map sort learquivo

sort需要一个列表,因此map sort需要一个列表列表([[a ]]),而 learquivo 的类型是 FilePath ->; IO [[Int]]

您可能想要这样的东西:

main = do
    contents <- learquivo "mkList1.txt"
    defaultMain [
       bench "map sort learquivo" $ \n -> map sort contents
    ]

您的代码中有很多可以清理的东西,但这应该可以让您继续下去。

The problem is this: map sort learquivo

sort expects a list, and so map sort expects a list of lists ([[a]]), whereas the type of learquivo is of type FilePath -> IO [[Int]].

You probably want something like:

main = do
    contents <- learquivo "mkList1.txt"
    defaultMain [
       bench "map sort learquivo" $ \n -> map sort contents
    ]

There are various things in your code that could be cleaned up, but that should get you going.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文