Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern Guards example does not work out of the box #81

Open
maueroats opened this issue Dec 19, 2024 · 0 comments
Open

Pattern Guards example does not work out of the box #81

maueroats opened this issue Dec 19, 2024 · 0 comments

Comments

@maueroats
Copy link
Contributor

This refers to PR #59.

  1. The standard prelude does not include readMaybe. Text.Read needs to be imported.
  2. The type annotation was insufficient. I needed to add Read a to the Nothing case.
  3. These two issues make it seem too complicated for inclusion in a beginner's text.
    It should be revised or deleted ("feature creep" is the problem).

This code compiles:

densityTell :: String -> String  
densityTell input  
    | Just density <- readMaybe input, density < 1.2 = "Wow! You're going for a ride in the sky!"  
    | Just density <- readMaybe input, density <= 1000.0 = "Have fun swimming, but watch out for sharks!"  
    | Nothing <- readMaybe input :: ((Read a, RealFloat a) => Maybe a) = "You know I need a density, right?"  
    | otherwise   = "If it's sink or swim, you're going to sink."

Suggested revision (to be completed by me someday). I actually think this is ugly (and bad style), and the topic should be removed, but this (relatively cleanly) fixes the issue I noted.

densityRead:: String -> Maybe Double
densityRead input = readMaybe input

densityTell' :: String -> String
densityTell' input
   | Just density <- result, density < 1.2 = "Wow! You're going for a ride in the sky!"  
   | Just density <- result, density < 1000.0 = "Have fun swimming, but watch out for sharks!"  
   | Nothing <- result = "You know I need a density, right?"  
   | otherwise = "If it's sink or swim, you're going to sink."
   where result = densityRead input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant