Send Beginners mailing list submissions to
[email protected]
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
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Nested folds (Francesco Ariis)
2. Re: Nested folds (Pietro Grandinetti)
3. Re: Nested folds (Francesco Ariis)
----------------------------------------------------------------------
Message: 1
Date: Sun, 13 Dec 2020 18:25:03 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Nested folds
Message-ID: <20201213172503.GA22875@extensa>
Content-Type: text/plain; charset=utf-8
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
------------------------------
Message: 2
Date: Sun, 13 Dec 2020 19:04:08 +0000
From: Pietro Grandinetti <[email protected]>
To: "[email protected]" <[email protected]>
Subject: Re: [Haskell-beginners] Nested folds
Message-ID:
<pr2pr09mb30031885a6f7e5d06cb40c27fc...@pr2pr09mb3003.eurprd09.prod.outlook.com>
Content-Type: text/plain; charset="cp1253"
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 <[email protected]> on behalf of Francesco Ariis
<[email protected]>
Sent: Sunday, December 13, 2020 6:25 PM
To: [email protected] <[email protected]>
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
[email protected]
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-0001.html>
------------------------------
Message: 3
Date: Sun, 13 Dec 2020 23:47:42 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Nested folds
Message-ID: <20201213224742.GB22522@extensa>
Content-Type: text/plain; charset=utf-8
Il 13 dicembre 2020 alle 19:04 Pietro Grandinetti ha scritto:
> 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`.
But they will know, a type synonym is just a way to make signatures
prettier/more informative, a program like this
foo :: String
foo = "foo"
k = sentCharCount foo
will _not_ be refused by the compiler. IF and when — in the future — you
decide to use newtype/data then you will get a compiler error (and in turn
would need a constructor with signature `:: String -> Sentence`.
> Do you have any feedback on my questions 1,2 and 3?
The functions are clearly written, there is some duplication because of
the type synonyms (i.e. `parWordCount` and `contWordCount` are the same
function).
Do not worry about it now and revisit the exercise once you start
using `newtype` and `data` + typeclasses
—F
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 149, Issue 7
*****************************************