Last active
December 14, 2015 23:09
-
-
Save fogus/5163546 to your computer and use it in GitHub Desktop.
How do I say this Haskell?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- THE SEARCH CONTINUES | |
data GenericThing = SpecificThing Char | |
| DifferentSpecificThing Double | |
| AnotherSpecificThing String | |
| YetAnotherDifferentSpecificThing Integer | |
foo :: [GenericThing] -> GenericThing | |
foo [] = return $ AnotherSpecificThing "" | |
foo [SpecificThing one] = -- do something | |
foo (SpecificThing c : cs) = -- do something else | |
foo badThings = -- throw if given any other kind of Thing | |
-- How can I express | |
-- - Do something for an array of one SpecificThing | |
-- - Do something else for an array of many SpecificSomethings | |
-- - Otherwise throw an error | |
-- The code above only checks the head of the lists, it does not type check the rest in the many case |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- My answer | |
data GenericThing = SpecificThing Char | |
| DifferentSpecificThing Double | |
| AnotherSpecificThing String | |
| YetAnotherDifferentSpecificThing Integer | |
extricate :: ThrowsError Char | |
extricate (SpecificThing c) = return c | |
extricate notChar = throwError $ FrobnicateError notChar | |
foo :: [GenericThing] -> GenericThing | |
foo [] = return $ AnotherSpecificThing "" | |
foo [SpecificThing one] = -- do something | |
foo things = mapM extricate things >>= return . AnotherSpecificThing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Q:
-- How can I express
-- - Do something for an array of one SpecificThing
-- - Do something else for an array of many SpecificSomethings
-- - Otherwise throw an error
A: