i’m newbie in haskell, and i have question:
i write code:
word_list = ["list", "lol", "wordword"]
check str = if head str == 'l' then tail str else str
average wl = (length $ concat $ map check wl) `div` length wl
this code must delete first “l” symbol in every word in word list, concat recieved words, get length of result string and div on words count.
so in this code i must recieve:
13 / 3 = 4,333… (“listlolwordword” = 15, “istolwordword” = 13)
but i recieve just 4.
average :: [[Char]] -> Float don’t work, i recieve error. where my mistake?
ps. sorry my english, please
The
lengthfunction returns anInt, and thedivfunction performs integer division, in other words, it drops the fractional part. If you want aFloatresult, you need to first convert the result oflengthto aFloat, then use(/)for division instead:While I’m at it, you should consider using pattern matching in
checkinstead, e.g.:This style is both more readable and less likely to have mistakes–for example, your version will fail if given an empty string.