How can I make a length of a list in haskell not tell me the repeated ones?

0

I need to solve a problem in haskell where it asks me that the function (let's call it func1) receives a list and a maximum price (which is related to the elements of the list), and counts the elements that are not repeated. . The list it receives is ['A', 'B', 'C', 'C', 'B', 'D', 'D']. I have already created the function that passes the values of the list in numbers

func5(x) 
     | x == 'A' = 111
     | x == 'B' = 222
     | x == 'C' = 333
     | x == 'D' = 444
     | otherwise = 0

func6(l) = map func5(l)

What it asks me is to show another list with the values that are not higher than the value received by parameter (for example: 350) and tell me how many values were left in the list. (in this example it would be only 3) but I can not think of how to solve it

Would someone help me solve it?

    
asked by keloo7 10.11.2018 в 20:00
source

1 answer

0

The question is a bit tricky to understand. I recommend you follow the haskell style to write code and follow the module documentation as Data.List where you have functions to operate with lists.

For example, in haskell parathons are not set to invoke a function as it is done in other languages:

func5 x 
     | x == 'A' = 111
     | x == 'B' = 222
     | x == 'C' = 333
     | x == 'D' = 444
     | otherwise = 0

func6 xs = map func5 xs

On the other hand, it is always advisable to add the signature of the functions to better understand what types of data you use.

So, all in all, what I think you're asking is summarized in that you want to "count how many non-repeating items are in a list that meet a condition (also known as a predicate)" . p>

Let's go in parts, we have to combine three functions:

  • the nub of Data.List to eliminate duplicates
  • filter to apply a condition
  • length to count items
  • And it would look like this:

    import Data.List (nub)
    
    count :: (a -> Bool) -> [a] -> Int
    count f xs = (length . filter f . nub) xs
    
        
    answered by 12.11.2018 в 02:37