Reputation: 5925
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? 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 `=')
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
Upvotes: 1
Views: 233
Reputation: 89043
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.
The charfreq =
and such are keyword-based arguments for setting the various fields of the StrStat
, and need to be given the appropriate value (e.g. [(Char, Int)]
) on their RHS.
What I'm guessing you're trying to do is to construct a StrStat
value, which you could do by constructing appropriate values:
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
}
~
Upvotes: 5