[Haskell-cafe] function unique

Alexteslin alexteslin at yahoo.co.uk
Wed Jul 11 11:59:33 EDT 2007


Thans Gregory,

But I sticked to the original solution because the exercise was about
primitive recursion and pattern matching over lists.



Gregory Propf wrote:
> 
> Well there's this approach.  Granted you need to split the input list into
> *both* args of the fold (i.e. the "input list" is really [1,4,5,3] but you
> can get this with head and tail).  I'm just learning about the fold family
> myself. - Greg
> 
> Prelude> foldl (\a b -> if (any (\x -> x == b) a) then a else b:a) [1]
> [4,5,3,3,4]
> [3,5,4,1]
> 
> 
> 
> ----- Original Message ----
> From: Alexteslin <alexteslin at yahoo.co.uk>
> To: haskell-cafe at haskell.org
> Sent: Tuesday, July 10, 2007 1:40:56 PM
> Subject: [Haskell-cafe] function unique
> 
> 
> Hi, i am a beginner to Haskell and i have a beginner's question to ask.
> 
> An exercise asks to define function unique :: [Int] -> [Int], which
> outputs
> a list with only elements that are unique to the input list (that appears
> no
> more than once).  I defined a function with list comprehension which works
> but trying to implement with pattern matching and primitive recursion with
> lists and doesn't work.
> 
> unique :: [Int] -> [Int]
> unique xs = [x | x <- xs, elemNum2 x xs == 1]
> 
> 
> elemNum2 :: Int -> [Int] -> Int
> elemNum2 el xs = length [x| x <- xs, x == el]
> 
> //This doesn't work, I know because the list shrinks and produces wrong
> result but can not get a right //thinking
> 
> unique2 :: [Int] -> [Int]
> unique2 [] = []
> unique2 (x:xs)
>     |elemNum2 x xs == 1 = x:unique2 xs
>     |otherwise = unique2 xs
> 
> 
> Any help to a right direction would be very appreciated, thanks.
> -- 
> View this message in context:
> http://www.nabble.com/function-unique-tf4058328.html#a11528933
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 
> 
> 
> 
> 
> 
>      
> ____________________________________________________________________________________
> Shape Yahoo! in your own image.  Join our Network Research Panel today!  
> http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7 
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 

-- 
View this message in context: http://www.nabble.com/function-unique-tf4058328.html#a11543175
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list