list - How to extract an element from a tuple in haskell -
list - How to extract an element from a tuple in haskell -
i have next tuples:
type id = int type name = string type fsk = int type film = (id, name, fsk)
now want define function extracts film given list that:
extract :: id -> [movie] -> (maybe movie, [movie]) extract = .... ??
so when give id , list of movies, extracts: 1) nil +the given list, if id found 2) + film , , new list without movie, if given id found, , film deleted list
example:
*main> extract 0 [(1,"matrix",16),(2,"gladiator",0)] (nothing,[(1,"matrix",16),(2,"gladiator",0)]) *main> extract 1 [(1,"matrix",16),(2,"gladiator",0)] (just (1,"matrix",16),[(2,"gladiator",0)])
how should define function?
extract :: id -> [movie] -> (maybe movie, [movie]) extract id [] = (nothing, []) extract id ((fid, n, f):list) | fid == id = (just (fid, n, f), list) | otherwise = (found, (fid, n, f):tail) (found, tail) = extract id list
list haskell tuples
Comments
Post a Comment