简体   繁体   English

haskell中是否可能“调用数据结构中的函数”?

[英]is “calling functions in data structure” possible in haskell?

i have some functions(charfreq,wordfreq,charcount,wordcount,parerror) and i want to use it in dataStructure with the given string.But how can i do it? 我有一些函数(charfreq,wordfreq,charcount,wordcount,parerror),我想在给定string.dataStructure中使用它。但是我该怎么做? I'm trying in these and many way ,but i got all error .Thanks in advance . 我正在尝试这些方法,但是我犯了所有错误。

data StrStat = StrStat  { charfreq :: [( Char , Int )] 
                        , wordfreq :: [([ Char ] , Int )] 
                        , charcount :: Int 
                        , wordcount :: Int 
                        , parerror::Maybe Int 
                        }


analyze :: [Char] -> StrStat
analyze x = StrStat { charfreq = (charfreq x) {-this line gives error-}
                    , wordfreq = (wordfreq x)
                    , charcount = (charcount x)
                    , wordcount = (wordcount x)
                    , parerror = (parerror x) 
                    }

error message is : Syntax error in input (unexpected `=') 错误消息是: Syntax error in input (unexpected `=')

analyze :: [Char] -> StrStat
analyze x = StrStat { "charfreq" = (charfreq x)
                    , "wordfreq" = (wordfreq x)
                    , "charcount" = (charcount x)
                    , "wordcount" = (wordcount x)
                    , "parerror" = (parerror x) 
                    }

when i tried previous one , i got the same error at the same line 当我尝试上一个时,在同一行出现了相同的错误

The error I get for your first version is 我得到的第一个版本错误是

Couldn't match expected type `StrStat' with actual type `[Char]'
In the first argument of `charfreq', namely `x'
In the `charfreq' field of a record
In the expression:
  StrStat
    {charfreq = (charfreq x), wordfreq = (wordfreq x),
     charcount = (charcount x), wordcount = (wordcount x),
     parerror = (parerror x)}

Which makes sense to me, as you're applying your getters (all defined in your data StrStat declaration, for example, charfreq :: StrStat -> [( Char , Int )] ) are being called on data of type [Char] , rather than on a StrStat value. 这对我来说很有意义,因为您正在对类型为[Char]数据调用getter(所有这些都在data StrStat声明中定义,例如charfreq :: StrStat -> [( Char , Int )] ),而不是使用StrStat值。

The charfreq = and such are keyword-based arguments for setting the various fields of the StrStat , and need to be given the appropriate value (eg [(Char, Int)] ) on their RHS. charfreq =等是基于关键字的参数,用于设置StrStat的各个字段,并且需要在其RHS上赋予适当的值(例如[(Char, Int)] )。

What I'm guessing you're trying to do is to construct a StrStat value, which you could do by constructing appropriate values: 我猜您正在尝试构造一个StrStat值,可以通过构造适当的值来做到这一点:

import Control.Arrow
import Data.List

data StrStat = StrStat  { charfreq :: [( Char , Int )]
                        , wordfreq :: [([ Char ] , Int )]
                        , charcount :: Int
                        , wordcount :: Int
                        , parerror::Maybe Int
                        }

freq :: Ord a => [a] -> [(a, Int)]
freq = map (head &&& length) . group . sort

analyze :: [Char] -> StrStat
analyze x = StrStat { charfreq = freq x
                    , wordfreq = freq $ words x
                    , charcount = length x
                    , wordcount = length $ words x
                    , parerror = Nothing
                    }

~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM