Skip to content

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

@maueroats

Description

@maueroats

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions