
Hi Colin, northernRange :: PieceType > Int -- note the camel case, that's traditional in Haskell circles northernRange p | p `elem` [Lance, ReverseChariot, VerticalMover ....] = 11 | p `elem` [Bishop, Kylin,....] = 0 | otherwise = 1 These are called "pattern guards" – you can put any boolean expression in them. Of note, if you provide an Enum instance for PieceType you may really be able to do this: northernRange p | p `elem` [Lance..SoaringEagle] = 11 | p `elem` [Bishop..HornedFalcon] = 0 | otherwise = 1 Finally, my guess is that you probably want a much more general type for PieceType that doesn't need extended every time you add a Piece to your game (and similarly doesn't need every function in your program extended at the same time). Perhaps something like this: data Piece = Piece { name :: String, northernRange :: Int } With elements like: lance = Piece "Lance" 11 or like: reverseChariot = Piece {name = "Reverse chariot", northernRange = 11} Bob On 3 Jan 2009, at 20:56, Colin Paul Adams wrote:
I have the following function:
northern_range:: Piece_type -> Int northern_range piece = case piece of Lance -> 11 Reverse_chariot -> 11 Vertical_mover -> 11 White_horse -> 11 Rook -> 11 Promoted_rook -> 11 Promoted_gold_general -> 11 Promoted_silver_general -> 11 Free_king -> 11 Promoted_phoenix -> 11 Flying_stag -> 11 Flying_ox -> 11 Whale -> 11 Dragon_king -> 11 Soaring_eagle -> 11 Bishop -> 0 Kylin -> 0 Lion -> 0 Promoted_kylin -> 0 Blind_tiger -> 0 Promoted_ferocious_leopard -> 0 Free_boar -> 0 Horned_falcon -> 0 _ -> 1
I'd prefer to write this as just three lines (one for each of the three resulting values), something like this:
northern_range:: Piece_type -> Int northern_range piece = case piece of Lance, Reverse_chariot, Vertical_mover, etc. -> 11 Bishop, Kylin, etc. -> 0 _ -> 1
Is there some syntax to do this sort of thing? -- Colin Adams Preston Lancashire _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners