简体   繁体   中英

Comparing the length of 2 lists in Haskell

im new to Haskell and currently making a few activities to test myself. The main reason i created this post is because im stuck in a situation that im not so sure its possible to solve the way im doing. The exercise that i've been trying to do asks to count how many repeated integers are in a list. Well, i tried to do it by recursion but i was not able to solve it like that (mainly because I'm inexperienced in the language and I couldn't follow the tutorials already present in the stackoverflow).

So I tried to copy my list using Set.fromList and having a copy generated without the duplicated elements in a way that I would only have to compare the size of the two lists and I would have my quantity.

import qualified Data.Set as Set

list :: [a] -> Int  
list xs = length xs
 
copyList = [1,2,2,4,4]

y =  Set.fromList copyLista




main::IO()

main = do

  print (list[1,2,3,3])

I implemented the code above but I don't know if there is any way for y to receive the values that were passed to list. It was only possible to put it manually, again as in the code above [1,2,2,4,4]

Could someone give me a hint on how to proceed?

First off, defining list = length like you did doesn't make any sense. Just use length right away.

But if what you actually want this to do is determining how many elements are in a set then there's a different function for that. Ask Hoogle what it's called: https://hoogle.haskell.org/?hoogle=Set a -> Int

Aha, it is Set.size . So what you probably want is

main = do
  print $ Set.size y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM