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 7851105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:05:07+00:00 2026-06-02T19:05:07+00:00

I have to define a function called zeros which takes input of two lists

  • 0

I have to define a function called zeros which takes input of two lists and returns a boolean which returns True if the number 0 appears the same amount of times in each list and false otherwise.

This is the last question in my homework and and I have managed to solve the question get it to work but I wondered if anybody can spot ways in which to reduce the amount of code. Any ideas are appreciated.
My code so far is as follows:

x :: Int
x = 0 
instances::[Int]->Int
instances [] = 0
instances (y:ys)
    | x==y = 1+(instances ys)
    | otherwise = instances ys



zeros :: [Int] -> [Int] -> Bool
zeros [] [] = False
zeros x y
       | ((instances x) == (instances y)) = True
       | otherwise = False
  • 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-06-02T19:05:09+00:00Added an answer on June 2, 2026 at 7:05 pm

    Without giving too much away, since this is homework, here are a few hints.

    Do you know about list comprehensions yet? They would be useful in this case. For example, you could combine them with an if expression to do something like this:

    *Main> let starS s = [if c == 's' then '*' else ' ' | c <- s]
    *Main> starS "schooners"
    "*       *"
    

    You can even use them to do filtering. For example:

    *Main> let findFives xs = [x | x <- xs, x == 5]
    *Main> findFives [3,7,5,6,3,4,5,7,5,5]
    [5,5,5,5]
    

    Neither of these is a complete answer, but it shouldn’t be hard to see how to adapt these structures to your situation.

    You should also think about whether you actually need a guard here! For example, here’s a function written with a guard in the same style as yours:

    lensMatch [] [] = True
    lensMatch xs ys
                 | ((length xs) == (length ys)) = True
                 | otherwise = False
    

    Here’s a function that does the same thing!

    lensMatch' xs ys = length xs == length ys
    

    You can see that they are the same; testing the first:

    *Main> lensMatch [1..4] [1..4]
    True
    *Main> lensMatch [1..4] [1..5]
    False
    *Main> lensMatch [] [1..5]
    False
    *Main> lensMatch [] []
    True
    

    And testing the second:

    *Main> lensMatch' [1..4] [1..4]
    True
    *Main> lensMatch' [1..4] [1..5]
    False
    *Main> lensMatch' [] [1..5]
    False
    *Main> lensMatch' [] []
    True
    

    Finally, I agree very strongly with sblom’s comment above; zeros [] [] should be True! Think about the following statement: “For each item x in set s, x > 0”. If set s is empty, then the statement is true! It’s true because there are no items in s at all. This seems to me like a similar situation.

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

Sidebar

Related Questions

Define a function called symcount that takes a symbol and a list and returns
I have a function called DrawPlaybook which listens to two events, one mouseclick event
Say I have an object called FieldEdit . I define the function constructor for
So i have this txt file called Students.txt, I want to define a function
I have a function, where if a variable is not defined, exit; is called,
So I have this function: (define (try try-block catch-block finally-block) ; Implements try/catch/finally like
I have a small sample function: #define VALUE 0 int test(unsigned char x) {
in header file I have defined the following function #ifndef OS_H #define OS_H #include
I have grid something like this: Ext.define('Exp.view.dashboard.Tv', { extend: 'Ext.grid.Panel', initComponent: function() { this.columns
I have defined a function in which i want to open a page on

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.