Question 1: Is there an easier, more elegant way to write this code? output a b c d e = println "Hello, this is " ++ show a ++ " a really hard " "to write function that " ++ show b ++ " would be easier to write with " "a printf " ++ show c ++ show d ++ show e Question 2: Is there a way to express the following relationship? I want to have a set of symbols with ordering and another set that is part of that ordering but with a different parent. For exammple, data Player = Detective | Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum) I want (succ Blue) == MrX and (pred MrX) == Blue. I could do this by putting them all in the same enumeration but I want to be able to test that the symbol is a Detective or Fugitive. By the way, how do I test that it's a detective? Do I pattern match it? foo Detective(x) = if x == Purple then ... else ... -- ? Thanks for the help. -mike
Question 1: Is there an easier, more elegant way to write this code?
For the most part, no.
Question 2: Is there a way to express the following relationship? I want to have a set of symbols with ordering and another set that is part of that ordering but with a different parent. For exammple,
data Player = Detective | Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
How about something like:
data Player = Detective Detective | Fugitive Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
(I'm not sure if the deriving Enum on Player will be exactly what you want -- I think so though. I don't derive this class very often.) Then you can test detectiveness by:
isDetective (Detective _) = True isDetective _ = False
HTH - Hal
I tried this. It doesn't work. succ Blue is an exception. Anybody else know how this should be done? Thanks, -mike On Thu, Feb 20, 2003 at 08:00:04AM -0800, Hal Daume III wrote:
Question 1: Is there an easier, more elegant way to write this code?
For the most part, no.
Question 2: Is there a way to express the following relationship? I want to have a set of symbols with ordering and another set that is part of that ordering but with a different parent. For exammple,
data Player = Detective | Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
How about something like:
data Player = Detective Detective | Fugitive Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
(I'm not sure if the deriving Enum on Player will be exactly what you want -- I think so though. I don't derive this class very often.)
Then you can test detectiveness by:
isDetective (Detective _) = True isDetective _ = False
HTH
- Hal
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
succ Blue cannot return MrX unless Blue and MrX have the same type. What I meant was that you would say: 'succ (Detective Red)' ==> Detective Green 'succ (Detective Green)' ==> Detective Blue 'succ (Detective Blue)' ==> Fugitive MrX -- Hal Daume III "Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume On Thu, 20 Feb 2003, Mike T. Machenry wrote:
I tried this. It doesn't work. succ Blue is an exception. Anybody else know how this should be done?
Thanks, -mike
On Thu, Feb 20, 2003 at 08:00:04AM -0800, Hal Daume III wrote:
Question 1: Is there an easier, more elegant way to write this code?
For the most part, no.
Question 2: Is there a way to express the following relationship? I want to have a set of symbols with ordering and another set that is part of that ordering but with a different parent. For exammple,
data Player = Detective | Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
How about something like:
data Player = Detective Detective | Fugitive Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
(I'm not sure if the deriving Enum on Player will be exactly what you want -- I think so though. I don't derive this class very often.)
Then you can test detectiveness by:
isDetective (Detective _) = True isDetective _ = False
HTH
- Hal
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
You can't derive Enum Player automatically, but you can program it. Here's one way how, using shorter example names. -- Dean data E1 = E1a | E1b | E1c deriving (Enum, Bounded, Show, Read) data E2 = E2a | E2b | E2c deriving (Enum, Bounded, Show, Read) data E = E1 E1 | E2 E2 deriving (Show, Read) instance Enum E where fromEnum (E1 e1) = fromEnum e1 fromEnum (E2 e2) = size_E1 + fromEnum e2 toEnum i | i < size_E1 = E1 (toEnum i) | otherwise = E2 (toEnum (i - size_E1)) instance Bounded E where minBound = toEnum 0 maxBound = toEnum (size_E1 + size_E2) size_E1 = fromEnum (maxBound :: E1) + 1 size_E2 = fromEnum (maxBound :: E2) + 1 Hal Daume III wrote:
succ Blue cannot return MrX unless Blue and MrX have the same type. What I meant was that you would say:
'succ (Detective Red)' ==> Detective Green 'succ (Detective Green)' ==> Detective Blue 'succ (Detective Blue)' ==> Fugitive MrX
-- Hal Daume III
"Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume
On Thu, 20 Feb 2003, Mike T. Machenry wrote:
I tried this. It doesn't work. succ Blue is an exception. Anybody else know how this should be done?
Thanks, -mike
On Thu, Feb 20, 2003 at 08:00:04AM -0800, Hal Daume III wrote:
Question 1: Is there an easier, more elegant way to write this code?
For the most part, no.
Question 2: Is there a way to express the following relationship? I want to have a set of symbols with ordering and another set that is part of that ordering but with a different parent. For exammple,
data Player = Detective | Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
How about something like:
data Player = Detective Detective | Fugitive Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
(I'm not sure if the deriving Enum on Player will be exactly what you want -- I think so though. I don't derive this class very often.)
Then you can test detectiveness by:
isDetective (Detective _) = True isDetective _ = False
HTH
- Hal
Oops! Small bug. See below. Dean Herington wrote:
You can't derive Enum Player automatically, but you can program it. Here's one way how, using shorter example names.
-- Dean
data E1 = E1a | E1b | E1c deriving (Enum, Bounded, Show, Read) data E2 = E2a | E2b | E2c deriving (Enum, Bounded, Show, Read)
data E = E1 E1 | E2 E2 deriving (Show, Read)
instance Enum E where fromEnum (E1 e1) = fromEnum e1 fromEnum (E2 e2) = size_E1 + fromEnum e2 toEnum i | i < size_E1 = E1 (toEnum i) | otherwise = E2 (toEnum (i - size_E1))
instance Bounded E where minBound = toEnum 0 maxBound = toEnum (size_E1 + size_E2)
Should be: maxBound = toEnum (size_E1 + size_E2 - 1)
size_E1 = fromEnum (maxBound :: E1) + 1 size_E2 = fromEnum (maxBound :: E2) + 1
Hmm, that does seem like alot of code to say such a little thing. Is it possible to come at the problem from the other direction? By this I mean I am trying to have two sets of symbols be enumerated together. This solution I asked for tries to impose the enumeration over the data. Can I define data Player = Red | Green | Blue | MrX deriving (Enum) and then use type classes or something else to say that MrX is a fugitive and the others are detectives such that I can pattern match on them? I have functions that recur over the Player argument and terminate at MrX, but I also want to be able to formally say that some functions cannot take a player that is Mrx, or can only take Mrx. -mike On Thu, Feb 20, 2003 at 12:35:44PM -0500, Dean Herington wrote:
You can't derive Enum Player automatically, but you can program it. Here's one way how, using shorter example names.
-- Dean
data E1 = E1a | E1b | E1c deriving (Enum, Bounded, Show, Read) data E2 = E2a | E2b | E2c deriving (Enum, Bounded, Show, Read)
data E = E1 E1 | E2 E2 deriving (Show, Read)
instance Enum E where fromEnum (E1 e1) = fromEnum e1 fromEnum (E2 e2) = size_E1 + fromEnum e2 toEnum i | i < size_E1 = E1 (toEnum i) | otherwise = E2 (toEnum (i - size_E1))
instance Bounded E where minBound = toEnum 0 maxBound = toEnum (size_E1 + size_E2)
size_E1 = fromEnum (maxBound :: E1) + 1 size_E2 = fromEnum (maxBound :: E2) + 1
Hal Daume III wrote:
succ Blue cannot return MrX unless Blue and MrX have the same type. What I meant was that you would say:
'succ (Detective Red)' ==> Detective Green 'succ (Detective Green)' ==> Detective Blue 'succ (Detective Blue)' ==> Fugitive MrX
-- Hal Daume III
"Computer science is no more about computers | hdaume@isi.edu than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume
On Thu, 20 Feb 2003, Mike T. Machenry wrote:
I tried this. It doesn't work. succ Blue is an exception. Anybody else know how this should be done?
Thanks, -mike
On Thu, Feb 20, 2003 at 08:00:04AM -0800, Hal Daume III wrote:
Question 1: Is there an easier, more elegant way to write this code?
For the most part, no.
Question 2: Is there a way to express the following relationship? I want to have a set of symbols with ordering and another set that is part of that ordering but with a different parent. For exammple,
data Player = Detective | Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
How about something like:
data Player = Detective Detective | Fugitive Fugitive deriving (Enum) data Detective = Red | Green | Blue deriving (Enum) data Fugitive = MrX deriving (Enum)
(I'm not sure if the deriving Enum on Player will be exactly what you want -- I think so though. I don't derive this class very often.)
Then you can test detectiveness by:
isDetective (Detective _) = True isDetective _ = False
HTH
- Hal
_______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
On Fri, Feb 21, 2003 at 12:14:04AM -0500, Mike T. Machenry wrote:
Hmm, that does seem like alot of code to say such a little thing. Is it possible to come at the problem from the other direction? By this I mean I am trying to have two sets of symbols be enumerated together. This solution I asked for tries to impose the enumeration over the data. Can I define
data Player = Red | Green | Blue | MrX deriving (Enum)
and then use type classes or something else to say that MrX is a fugitive and the others are detectives such that I can pattern match on them? I have functions that recur over the Player argument and terminate at MrX, but I also want to be able to formally say that some functions cannot take a player that is Mrx, or can only take Mrx.
I think type classes would be overkill here. What's wrong with isDetective :: Player -> Bool isDetective MrX = False isDetective _ = True ? (The pattern matching doesn't work quite the same way, but you can use guards to acheive the same effect, especially with ghc's pattern guards extension.) Best, Dylan Thurston
Mike T. Machenry (Wed, Feb 19, 2003 at 10:23:45PM -0500):
Question 1: Is there an easier, more elegant way to write this code?
output a b c d e = println "Hello, this is " ++ show a ++ " a really hard " "to write function that " ++ show b ++ " would be easier to write with " "a printf " ++ show c ++ show d ++ show e You can write a printf-like function in Haskell, s.t. output = <construct-printf.hs>
google it, since I do not remember the location of the paper.
-- ?
Thanks for the help. -mike
-- Stefan Karrmann
On Fri, Feb 21, 2003 at 06:33:10PM +0100, Stefan Karrmann wrote:
Mike T. Machenry (Wed, Feb 19, 2003 at 10:23:45PM -0500):
Question 1: Is there an easier, more elegant way to write this code?
output a b c d e = println "Hello, this is " ++ show a ++ " a really hard " "to write function that " ++ show b ++ " would be easier to write with " "a printf " ++ show c ++ show d ++ show e You can write a printf-like function in Haskell, s.t. output = <construct-printf.hs>
google it, since I do not remember the location of the paper.
I don't remember the paper's URL either, but here is a sample implementation: data End = End data L fmt = L String fmt data S fmt = S fmt data I fmt = I fmt class Format fmt x | fmt->x where format' :: fmt -> String -> x instance Format End String where format' (End) out = out instance (Format fmt x) => Format (L fmt) x where format' (L s fmt) out = format' fmt (out++s) instance (Format fmt x) => Format (S fmt) (String->x) where format' (S fmt) out = \s -> format' fmt (out++s) instance (Format fmt x) => Format (I fmt) (Int->x) where format' (I fmt) out = \i -> format' fmt (out++ show i) format :: (Format fmt x) => (End -> fmt) -> x format f = format' (f End) "" lit = L str = S int = I main = putStrLn (format (int.lit " is ".str) 5 "five") So you see the part (int.lit " is ".str) is to construct something like "%d is %s" in printf. You may extend the idea to support other types too. Regards, .paul.
participants (6)
-
Dean Herington -
Dylan Thurston -
Hal Daume III -
Mike T. Machenry -
paul@theV.net -
Stefan Karrmann