[Haskell-beginners] Nested folds
Francesco Ariis
fa-ml at ariis.it
Sun Dec 13 17:25:03 UTC 2020
Hello Pietro,
Il 13 dicembre 2020 alle 10:39 Pietro Grandinetti ha scritto:
> Hello,
> I have a piece of code to represents Sentences, Paragraphs and the Content of an article. I added functions to count the words, code below. My questions:
>
> […]
>
> I also have two more practical questions on the following two functions:
>
> makeSentence :: String -> Sentence
> makeSentence x = x::Sentence
You can omit the `:: Sentence` part, since it is specified in the
signature above. You can omit the whole function itself to be fair,
Sentence is a type synonym!
> sentCharCount :: Sentence -> Int
> sentCharCount x = length $ filter (/= ' ') x
You can write this point-free like this
sentCharCount :: Sentence -> Int
sentCharCount = length . filter (/= ' ')
In this example you can regard `$` as «evaluate everything on the right
before anything else», so
length $ filter (/= ' ')
^^^^^^ ^^^^^^^^^^^^^^^
| |
| |
| +-- this has type :: [Char] -> [Char]
|
+-- length does not work on `[Char] -> [Char]`
`.` instead is appropriate
λ> :t (.)
(.) :: (b -> c) -> (a -> b) -> a -> c
Does this clear your doubts?
—F
More information about the Beginners
mailing list