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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -