Answer the question
In order to leave comments, you need to log in
How can you modify a function for your own type?
simple types and a complex type structure are defined
, the following functions should have been implemented for them ... in particular, I had a problem with the latter: a list of all identifiers with a numeric type).
when you enter getByTypes [MyInt, MyString] base returns
["first","next","str"] , but if you set MyStruct as an argument, it gives an error, how to make the function work with this type?
data MyType = Null |
MyInt |
MyDouble |
MyString |
MyStruct [(String,MyType)] deriving (Show,Eq)
isStructured :: MyType -> Bool
isStructured (MyStruct a) = True
isStructured (_) = False
getType :: String -> [(String, MyType)] -> MyType
getType _ [] = Null
getType s (h:t) | fst(h) == s = snd(h)
| otherwise = getType s t
getFieldsFromStruct :: MyType -> [(String,MyType)]
getFieldsFromStruct (MyStruct a) = a
getFieldsFromStruct _ = []
getFields :: String -> [(String,MyType)] -> [(String,MyType)]
getFields _ [] = []
getFields s (h:t) | fst(h) == s && isStructured (snd h) = getFieldsFromStruct (snd h)
| otherwise = getFields s t
getByType :: MyType -> [(String,MyType)] -> [String]
getByType _ [] = []
getByType a (h:t) | a == (snd h) = (fst h) : getByType a t
| otherwise = getByType a t
getByTypes :: [MyType] -> [(String,MyType)] -> [String]
getByTypes _ [] = []
getByTypes [] _ = []
getByTypes (h:t) a = (getByType h a) ++ getByTypes t a
base = [("first",MyInt), ("second",MyDouble), ("str",MyString), ("next",MyInt) ,("s",MyStruct[("s1",MyInt),("s2",MyDouble)])]
Answer the question
In order to leave comments, you need to log in
when you type getByTypes [MyInt, MyString] base returns ["first","next","str"]So the function is working.
if you set MyStruct as an argument, it gives an error, how to make the function work with this type?How is the call going?
getByTypes [MyStruct[("s1",MyInt),("s2",MyDouble)]] base
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question