【问题标题】:How to join multiple functions to apply to a file? Haskell如何加入多个功能以应用于文件?哈斯克尔
【发布时间】:2017-10-27 17:01:03
【问题描述】:

继续我在 haskell 中的学习,我需要将几个函数应用于一个文件。我研究并找到了一些提示,但它不起作用,我的类型系统有问题,可以帮助我吗?我做这个:

import Prelude hiding (Word, lines)
import Data.Char
import System.IO
import Data.List hiding (lines)

type Doc = String
type Line = String
type Word = String

makeIndex :: Doc -> [([Int], Word)]
makeIndex = lines . numLines . allNumWords . sortLs . makelists . almalgamate . shorten

lines :: Doc -> [Line]
lines [] = []
lines texto = takeWhile(/='\n') texto:(lines.dropWhile(=='\n').dropWhile(/='\n')) texto

numLines :: [Line] -> [(Int, Line)]
numLines texto = zip [1.. length texto] texto

numWord :: (Int, Line) -> [(Int, Word)]
numWord (number, line) = [(number, word) | word <- divide line]

divide :: String -> [Word]
divide st = split (dropSpace st)

split :: String -> [Word]
split [] = []
split st = (getWord st) : split (dropSpace (dropWord st))

getWord :: String -> String
getWord [] = []
getWord (x:xs) | elem x whitespace = (x:xs)
               | otherwise = dropWord xs

whitespace :: String
whitespace = ['\n', '\t', ' ']

dropSpace :: String -> String
dropSpace [] = []
dropSpace (x:xs) | elem x whitespace = dropSpace xs
                 | otherwise = (x:xs)

dropWord :: String -> String
dropWord [] = []
dropWord (x:xs) | elem x whitespace = (x:xs)
                | otherwise = dropWord xs          

allNumWords :: [(Int, Line)] -> [(Int, Word)]
allNumWords = concat.map numPalavra

sortLs :: [(Int, Word)] -> [(Int, Word)]
sortLs [] = []
sortLs (a:x) = sortLs [b | b <- x, compare b a] ++ [a] ++ sortLs [b | b <- x, compare a b]
                where compare (n1, w1) (n2, w2) = (w1 < w2) || (w1 == w2 && n1 < n2)

makelist :: [(Int, Word)] -> [([Int], Word)]
makelist = map mklis 
             where mklis (num, word) = ([num], word)

almalgamate :: [([Int], Word)] -> [([Int], Word)]
almalgamate [] = []
almalgamate [a] = [a]
almalgamate ((n1, w1) : (n2, w2) : rest) | w1 /= w2 = (n1, w1) : almalgamate ((n2, w2) : rest)
                                         | otherwise = almalgamate ((n1++n2, w1) : rest)

shorten :: [([Int], Word)] -> [([Int], Word)]
shorten = filter long
           where long (num, palavra) = length palavra > 4

在编译器中的错误是:

in line 18
Coldn't match type `[Char]' -> ([Int], Word)'  
Expected type: Doc -> [([Int], Word)]  
Actual type: [([Int], Word] -> [Line]  
int the expression (repeat all the functions)  
in an equation for 'makeIndex' (repeat all the functions)  

这与 makeindex 中所有函数调用显示的错误相同。

GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( C:\Users\xxx\Desktop\makeindex.hs, interpreted )

C:\Users\xxx\Desktop\makeindex.hs:17:6: error:  
    * Couldn't match type `[Char]' with `([Int], Word)'  
      Expected type: Doc -> [([Int], Word)]  
        Actual type: [([Int], Word)] -> [String]  
    * In the expression:  
        lines  
        . numLines  
          . allNumWords . sortLs . converte . almalgamate . shorten
      In an equation for `makeIndex':  
          makeIndex  
            = lines  
              . numLines  
                . allNumWords . sortLs . makelist . almalgamate . shorten  

C:\Users\xxx\Desktop\makeindex.hs:18:6: error:  
    * Couldn't match type `(Int, Line)' with `Char'  
      Expected type: [([Int], Word)] -> String  
        Actual type: [([Int], Word)] -> [(Int, Line)]  
    * In the second argument of `(.)', namely  
        `numLines  
         . allNumWords . sortLs . makelist . almalgamate . shorten'
      In the expression:  
        lines  
        . numLines  
          . allNumWords . sortLs . converte . almalgamate . shorten
      In an equation for `makeIndex':  
          makeIndex  
            = lines  
              . numLines  
                . allNumWords . sortLs . makelist . almalgamate . shorten  

C:\Users\xxx\Desktop\makeindex.hs:19:6: error:  
    * Couldn't match type `(Int, Word)' with `[Char]'  
      Expected type: [([Int], Word)] -> [Line]  
        Actual type: [([Int], Word)] -> [(Int, Word)]  
    * In the second argument of `(.)', namely  
        `allNumWords . sortLs . makelist . almalgamate . shorten'  
      In the second argument of `(.)', namely  
        `numLines  
         . allNumWords . sortLs . makelist . almalgamate . shorten'  
      In the expression:  
        lines  
        . numLines  
          . allNumWords . sortLs . makelist . almalgamate . shorten  

C:\Users\xxx\Desktop\makeindex.hs:21:6: error:  
    * Couldn't match type `[Int]' with `Int'  
      Expected type: [([Int], Word)] -> [(Int, Word)]  
        Actual type: [([Int], Word)] -> [([Int], Word)]  
    * In the second argument of `(.)', namely  
        `converte . almalgamate . shorten'  
      In the second argument of `(.)', namely  
        `sortLs . converte . almalgamate . shorten'  
      In the second argument of `(.)', namely  
        `allNumWords . sortLs . makelist . almalgamate . shorten'  

C:\Users\xxx\Desktop\makeindex.hs:22:6: error:  
    * Couldn't match type `[Int]' with `Int'  
      Expected type: [([Int], Word)] -> [(Int, Word)]  
        Actual type: [([Int], Word)] -> [([Int], Word)]  
    * In the second argument of `(.)', namely `almalgamate . shorten'  
      In the second argument of `(.)', namely  
        `converte . almalgamate . shorten'  
      In the second argument of `(.)', namely  
        `sortLs . converte . almalgamate . shorten'  
Failed, modules loaded: none.  

这是问题,匹配类型问题?我很抱歉我在第一次发布时对问题的错误描述。

【问题讨论】:

  • 什么不起作用?类型系统有什么问题?什么错误?您定义的其他功能是什么?您可能可以在问题中提供更少的细节,但这会很困难。
  • 请编辑您的问题以包含完整的错误消息。还请提供 numLines、allNumWords、sortLs、makeList、amalgamate 和 Shortcut 的类型签名和简要说明(或完整代码)。没有这些信息,就不可能回答您的问题。还请在问题的正文中准确解释您打算执行的功能。对文件进行索引还不够精确。
  • 我更改了帖子,对此我很抱歉。

标签: haskell types


【解决方案1】:

问题似乎是您混淆了(.) 的参数顺序。它(本质上)定义为

(.) :: (b -> c) -> (a -> b) -> a -> c
(f . g) x = f (g x)

所以它首先应用g,然后将f 应用到结果。

如果您以相反的顺序将函数串起来,您的程序将进行类型检查:

makeIndex = shorten . almalgamate . converte . sortLs
              . allNumWords . numLines . lines

或者,您可以使用(&gt;&gt;&gt;),它是(.) 的翻转版本:

import Control.Category ((>>>))

makeIndex = lines >>> numLines >>> allNumWords >>> sortLs
              >>> converte >>> almalgamate >>> shorten

由于你还没有说出你希望程序做什么,我不能说修改后的版本是否真的正确,但它确实可以编译.

【讨论】:

  • 感谢您的帮助,我在此代码中的目标是根据它们在文件文本中的出现来建立单词索引。但是,我的问题是一些功能一起工作。我了解如何完成这项工作,再次感谢您的耐心等待。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 2011-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多