Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Nested folds (Pietro Grandinetti)


----------------------------------------------------------------------

Message: 1
Date: Sun, 13 Dec 2020 10:39:36 +0000
From: Pietro Grandinetti <pietro....@hotmail.it>
To: "beginners@haskell.org" <beginners@haskell.org>
Subject: [Haskell-beginners] Nested folds
Message-ID:
        
<pr2pr09mb3003c776f4a3f6bb4b369967fc...@pr2pr09mb3003.eurprd09.prod.outlook.com>
        
Content-Type: text/plain; charset="iso-8859-1"

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:

1- Are these functions idiomatic?
2- Is this an efficient way to do the computation?
3- In different languages, I could (and would) give the same name `wordCount` 
to the three functions, because the type of the input would clarify the usage. 
But here GHC throws an error. What's the most idiomatic way to do this in 
Haskell?

type Sentence  = String
type Paragraph = [Sentence]
type Content   = [Paragraph]

sentWordCount :: Sentence -> Int
sentWordCount = length . words

parWordCount :: Paragraph -> Int
parWordCount = foldr ((+) . sentWordCount) 0

contWordCount :: Content -> Int
contWordCount = foldr ((+) . parWordCount) 0

I also have two more practical questions on the following two functions:

makeSentence :: String -> Sentence
makeSentence x = x::Sentence

sentCharCount :: Sentence -> Int
sentCharCount x = length $ filter (/= ' ') x

4- About `makeSentence` -- does it make sense to write a function like that 
just to encapsulate the String type?
5- About `sentCharCount` -- I cannot take the argument x off, the compilator 
complains. What's the reason?

Thanks,
-P


-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20201213/96dbb0a4/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 149, Issue 6
*****************************************

Reply via email to