[Haskell-beginners] Collapsing multiple case branches?

Colin Paul Adams colin at colina.demon.co.uk
Sat Jan 3 14:56:07 EST 2009


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


More information about the Beginners mailing list