Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Use a sum type:

   data Datum = S String | D Double
   
   lst :: [Datum]
   lst = [S "a", D 3.14]
In Python we'd have to write the logic to handle both cases and fail with an exception or do an instanceof check. In Haskell we'd do this by pattern matching on the sum type.

   case (lst !! 42) of
      S n -> -- handle string
      D n -> -- handle number


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: