Rumen Hristov
Rumen Hristov

Reputation: 887

Why am I unsuccessful in passing an argument to a function in Haskell

I have the following very simple program that takes a list of integers and a single integer. It then checks the entire list using map to see how many member of the list are the same as the passed integer. then it returns the number of those elements by finding the length of the list that map generated. However, I am not able to pass my first integer-argument in my first function (Integer-Bool). Why is that and how can I fix it? Thanks

import Data.List (genericLength)

count::(Integer->Bool)->[Integer]->Integer
count op xs = genericLength (filter(True ==) (map op xs))

main = do
print $ count 3 [3, 4, 5, 3, 5, 3]

This code should run as follows:

-first, it applies map to the list and returns a new list: [True, False, False, True, False, True]

-second, it applies filter to filter out all the True's and we get a new list: [True, True, True]

-third, it applies genericLength to figure out the length of this last list, thus returning: 3

Upvotes: 0

Views: 178

Answers (1)

Rumen Hristov
Rumen Hristov

Reputation: 887

I figured it. it needs to be called like this:

print $ count (==3) [3, 4, 5, 3, 5, 3]

Upvotes: 4

Related Questions