Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4622912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:54:38+00:00 2026-05-22T02:54:38+00:00

I am trying to filter by an item in a list and print them

  • 0

I am trying to filter by an item in a list and print them line by line. Here’s my 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 a b c d) =  show a ++ " | " ++ concat [i ++ ", " | 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] -- This is the code not working i think

Why isnt this working?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:54:39+00:00Added an answer on May 22, 2026 at 2:54 am

    If you wish to filter a list, I recommend using the 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 -- you can implement this how you like
    
    displayCarsByYear :: Int -> IO ()
    displayCarsByYear chosenYear = mapM_ (putStrLn . showCar) filteredCars
        where filteredCars = filterByYear chosenYear testDatabase
    

    It seems wise to explain a few things here:

    Anonymous Functions: (\car -> year car == chosenYear) is an anonymous function. It takes one argument and calls it car. Then it determines whether that car’s year is equal to the chosenYear. I didn’t explicitly write this function’s type signature, but it’s Car -> Bool.

    Filtering: I gave that function to filter, so that it would look through the list of Cars. When filter finds cars for which that function returns True, it puts them in the result list. A False result means that a car doesn’t make it through the filter.

    Function composition: (putStrLn . showCar) This is a function that first performs showCar, and then uses putStrLn on the result of showCar.

    Where: You’ll notice the where statement at the end of my code. It should be fairly self-explanatory, you can use either let or where statements to define “local variables”. As a matter of taste, I prefer where over let.

    List comprenensions vs filter: List comprehensions can filter a list just like the filter function. For a function f :: a -> Bool, and a list xs :: [a]

    filter f xs is the same as [x | x <- xs, f x]. As a matter of taste, I prefer spelling out filter in such cases, since it makes it very clear that I’m filtering the list.

    See also LYAH # Maps and 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 really tell what your last list of Strings was)

    This way, you can construct a Film like this:

    lotr :: Film
    lotr = Film { name = "Lord of the Rings"
                , actors = ["Elijah Wood", "Ian McKellen", "Orlando Bloom"]
                , released = 2001
                , characters = ["Frodo", "Sam", "Pippin", "Merry"]
                }
    

    And you automatically have accessor functions

    • released :: Film -> Int
    • name :: Film -> String
    • and so forth

    See also LYAH # Record syntax

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to filter a datatable with following code private void Filter(string text) {
I'm using below jscript code to filter unordered list. I'm trying to remove <h3>
i'm trying to filter the $routeParams of the data returned from the json file.
I am trying to filter result from JSON string based on status selection. I
I'm trying to filter by a list of values using the criteria API. I
I am trying to filter a list of items using the .Where method, and
New to Haskell and have a stumbling block. I'm trying to filter a list
I am trying to filter a list of Chord objects (class) in C# using
I have following HTML <div id=finalTree> <ul> <li class=last style=display: list-item;> <a id=DataSheets href=#>Data
I'm trying to list all data on a page from table 'points_points' where user_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.