
Hi, Happy to help so far. I see your question in the comments of the code, asking why ‘flip' is needed. isAlphabetic :: Char -> Bool isAlphabetic = flip elem alphabet Basically, it’s needed by elem here. Let’s look at the signature of elem Prelude> :t elem elem :: (Eq a, Foldable t) => a -> t a -> Bool Lists [ ] qualify as a Foldable t, so in the type signature let’s replace the ’t a’ with ‘[a]’ for clarity for you. elem :: Eq a => a -> [a] -> Bool So, elem takes an ‘a’ as the first parameter and a 'list of a’ as the second parameter and returns a boolean. But we already have our list, and we won’t have the character we want to check until later! So, flip let’s switch that around! That way we can give elem the second parameter first, so it becomes: Prelude> :t (flip elem) (flip elem) :: (Eq a, Foldable t) => t a -> a -> Bool Or to rewrite it like we did above so it’s easier to read. (flip elem) :: Eq a => [a] -> a -> Bool If you look at the function ‘flip’ you’ll see this. Prelude> :t flip flip :: (a -> b -> c) -> b -> a -> c As a beginner it might be easier for you to read if I make this small change. flip :: (a -> b -> c) -> b -> (a -> c) So flip takes a function which takes an ‘a’ and takes a ‘b’ and returns a ‘c’, it takes a ‘b’, puts that ‘b’ into the function you supplied, and then returns to you a function which takes an ‘a’ and returns a ‘c’. So, if you don’t have flip in there elem alphabet Then you are trying to give a list to elem in the position of it’s first parameter. Does that help? Best regards, Daniel van de Ghinste
On 21 May 2020, at 08:36, chrysaetos99
wrote: Hi,
thank you for your feedback Daniel an Toz. I now used (most of) your suggestions to improve the code.
You can find the result as an attachment.
Can I improve this even further? Also, I have put a question in the code (as a comment). Could anyone answer
that one also?
I would appreciate any answers, and thanks again for your effort.
chrysaetos99
On 21.05.20 02:04, 鲍凯文 wrote:
Hi,
For what it's worth, Daniel's suggestion of:
```haskell isAlphabetic char = elem char ['A'..'Z'] ```
does read nicer but would have to traverse `['A'..'Z']` every time. I think what you have is fine, and although relational and boolean operators are also in imperative languages, they behave as pure functions even in imperative languages if subexpressions don't cause side effects. I don't think it's unidiomatic, and especially in this case, "between A and Z" means the same thing as "is one of the letters A, B, C...Z", so the intent of the function is clear as written.
Best of all would probably be using `isAlpha` from `Data.Char` (in `base`).
Best,
toz
On Wed, May 20, 2020 at 2:41 PM
mailto:beginners-request@haskell.org> wrote: Send Beginners mailing list submissions to beginners@haskell.org mailto:beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-request@haskell.org mailto:beginners-request@haskell.org
You can reach the person managing the list at beginners-owner@haskell.org mailto:beginners-owner@haskell.org
When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..."
Today's Topics:
1. Code Review of Caesar-Cipher (chrysaetos99) 2. Re: Code Review of Caesar-Cipher (Daniel van de Ghinste)
----------------------------------------------------------------------
Message: 1 Date: Wed, 20 May 2020 20:26:37 +0200 From: chrysaetos99
mailto:chrysaetos99@posteo.de> To: beginners@haskell.org mailto:beginners@haskell.org Subject: [Haskell-beginners] Code Review of Caesar-Cipher Message-ID: mailto:cd08aa1e-1c9b-a609-15df-5bf37508cb96@posteo.de> Content-Type: text/plain; charset="utf-8"; Format="flowed" Background ---------- I am a total beginner in Haskell, so after reading the "Starting out"-chapter of "Learn you a Haskell", I wanted to create my first program that actually does something.
I decided to do the famous Caesar-Cipher.
Code ---- See attachment.
Question(s)
-----------
- How can this code be improved in general? - Do I follow the style guide of Haskell (indentation, etc.)? - I have a background in imperative languages. Did I do something that is untypical for functional programming languages?
I would appreciate any suggestions.
---
Please note: I also asked this question on https://codereview.stackexchange.com/questions/242529/caesar-cipher-implemen... https://codereview.stackexchange.com/questions/242529/caesar-cipher-implemen..., but didn't receive an answer that really answered all my questions.
Kind regards
chrysaetos99