[Haskell-beginners] Multiple type numeric data

iæfai iaefai at me.com
Mon Nov 2 00:04:03 EST 2009


Didier, here is some code that improves your one function, it will  
almost compile with ghc.

module Main where

data Figure = Carre Int Int Int
         | Rond (Int, Int, Integer)    -- i hope Integer allows  
fromInteger function for r

surface_carre :: Figure -> Int
surface_carre (Carre x y c)= c * c

surface_rond :: Figure -> Float
surface_rond  (Rond (x, y, r))= 3.14 * (fromInteger (r * r))

surface x = case x of
   Carre a b c     -> surface_carre x
   Rond  (a, b, c) -> surface_rond x
a::Figure
a=Carre 10 10 5
b::Figure
b=Rond (20, 20, 3)

main :: IO ()
main = do
     putStrLn "Test"
     return ()

Note that surface has a problem because surface_carre returns an Int  
and surface_rond returns a Float. These types are incompatible. I  
don't think there is a more general type you can use. Can you get away  
with just using all the same type?

- iæfai

On 2009-11-01, at 12:53 PM, Didier Jacquemart wrote:

> data Figure = Carre Int Int Int
>         | Rond (Int, Int, Integer)    -- i hope Integer allows  
> fromInteger function for r
>
> surface_carre :: Figure -> Int
> surface_carre (Carre x y c)= c * c
>
> surface_rond :: Figure -> Float
> surface_rond  (Rond (x, y, r))= 3.14  * r * r
>
> surface x = case x of
>   Carre a b c     -> surface_carre x
>   Rond  (a, b, c) -> surface_rond x
> a::Figure
> a=Carre 10 10 5
> b::Figure
> b=Rond (20, 20, 3)



More information about the Beginners mailing list