[Haskell-beginners] Nested folds
Pietro Grandinetti
pietro.gra at hotmail.it
Sun Dec 13 19:04:08 UTC 2020
Hello Francesco,
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?
Thanks,
-P
________________________________
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
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
_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20201213/a216edbe/attachment.html>
More information about the Beginners
mailing list