haskell - Representing data as a string -


i have following simple code:

data shape = circle float float float | rectangle float float float float deriving (show) surface :: shape -> float surface (circle _ _ r) = pi * r ^ 2  main = putstrln $ surface $ circle 10 20 30 

it complains:

couldn't match expected type `string' actual type `float' in second argument of `($)', namely `surface $ circle 10 20 30' 

how rid of error? "add" show method shape , override can represent shape on screen (printed) whatever want.

you need add show:

main = putstrln $ show $ surface $ circle 10 20 30 

if want own show method, don't derive show:

data shape = circle float float float            | rectangle float float float float  instance show shape      show (circle _ _ r) = show r      show (rectangle r _ _ _) = show r  main = putstrln $ show $ circle 10 20 30 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -