haskell - Using filter on an item in a list? -


i trying filter item in list , print them line line. here's code:

data car = car string [string] int [string]  testdatabase :: [car] testdatabase = [car"casino royale" ["daniel craig"] 2006 ["garry", "dave", "zoe", "kevin", "emma"],car"blade runner" ["harrison ford", "rutger hauer"] 1982 ["dave", "zoe", "amy", "bill", "ian", "kevin", "emma", "sam", "megan"]]    formatcarrow (car b c d) =  show ++ " | " ++ concat [i ++ ", " | <- init b] ++ last b ++ " | " ++ show c ++ " | " ++ concat [j ++ ", " | j <- init d] ++ last d    displayfilmsbyyear :: string -> io [()] displayfilmsbyyear chosenyear = mapm (putstrln.formatfilmrow) [putstrln(filter ((== chosenyear).y)) |  (w x y z) <- testdatabase] -- code not working think 

why isnt working?

if wish filter list, recommend using filter function :)

data car = car string [string] int [string]  year :: car -> int year (car _ _ y _) = y  filterbyyear :: int -> [car] -> [car] filterbyyear chosenyear cars = filter (\car -> year car == chosenyear) cars  showcar :: car -> string showcar car = undefined -- can implement how  displaycarsbyyear :: int -> io () displaycarsbyyear chosenyear = mapm_ (putstrln . showcar) filteredcars     filteredcars = filterbyyear chosenyear testdatabase 

it seems wise explain few things here:

anonymous functions: (\car -> year car == chosenyear) anonymous function. takes 1 argument , calls car. determines whether car's year equal chosenyear. didn't explicitly write function's type signature, it's car -> bool.

filtering: gave function filter, through list of cars. when filter finds cars function returns true, puts them in result list. false result means car doesn't make through filter.

function composition: (putstrln . showcar) function first performs showcar, , uses putstrln on result of showcar.

where: you'll notice where statement @ end of code. should self-explanatory, can use either let or where statements define "local variables". matter of taste, prefer on let.

list comprenensions vs filter: list comprehensions can filter list filter function. function f :: -> bool, , list xs :: [a]

filter f xs same [x | x <- xs, f x]. matter of taste, prefer spelling out filter in such cases, since makes clear i'm filtering list.

see lyah # maps , filters

--

further recommendation: use record syntax

instead of

data car = car string [string] int [string] 

why not

data film = film { name :: string                  , actors :: [string]                  , released :: int                  , characters :: [string]                  } 

(i couldn't tell last list of strings was)

this way, can construct film this:

lotr :: film lotr = film { name = "lord of rings"             , actors = ["elijah wood", "ian mckellen", "orlando bloom"]             , released = 2001             , characters = ["frodo", "sam", "pippin", "merry"]             } 

and automatically have accessor functions

  • released :: film -> int
  • name :: film -> string
  • and forth

see lyah # record syntax


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -