haskell - How to write a function that controls the data end decide if a data type exists? -
i wondering able control data type , decide whether entered data exists in haskell?
for example:
data ruler =ruler length price deriving(eq,show) data wallet = wallet colour ruler [pencil] deriving(eq,show) data pencil =pencil penciltype colour price deriving(eq,show) data colour =black | blue | green | red deriving(eq,show) data penciltype =leadpencil | pen | fountainpen | feltpen deriving(eq,show) type price =double type length =int
so ideas?
i want define function that:
isruleravailable :: wallet-> bool if ruler available in wallet true else false
i think you're misunderstanding how data types work in haskell.
what wallet
data type says is
i store 1 ruler, colour, , pencils under tag wallet.
this means wallet
has 1 ruler
in , can ever have 1 ruler
in it.
if wanted allow possibility of not storing ruler
you'd use maybe ruler
in data declaration, not ruler
.
then function becomes:
isruleravailable (wallet _ ruler _) = isjust ruler
which requires import data.maybe
.
for explanation of maybe
, can here
Comments
Post a Comment