[Haskell-beginners] Nested folds
pietro.gra at hotmail.it
Sun Dec 13 19:04:08 UTC 2020
Yes, that helped. However, I believe I shouldn't remove the `makeSentence`. A user of the module is not supposed to know what a `Sentence` is, hence I must provide a function such as `makeSentence`. Right now, its implementation is just a type conversion, but may change later.
This would be my logic in different languages; does it make sense in Haskell?
Do you have any feedback on my questions 1,2 and 3?
From: Beginners <beginners-bounces at haskell.org> on behalf of Francesco Ariis <fa-ml at ariis.it>
Sent: Sunday, December 13, 2020 6:25 PM
To: beginners at haskell.org <beginners at haskell.org>
Subject: Re: [Haskell-beginners] Nested folds
Il 13 dicembre 2020 alle 10:39 Pietro Grandinetti ha scritto:
> 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?
Beginners mailing list
Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners