<br>Hi folks,<br><br>While solving a puzzle, I was posed the problem of finding if there was no duplicates on a list. <br><br>First I used:<br><br>noneRepeated=null.(filter (>1)).(map length).group.sort<br><br clear="all">
<br>But this seemed very unneficient, so I thought that I could detect the duplicates while sorting, and devised this:<br><br>import Control.Monad<br>import Data.Maybe<br><br>noneRepeated=isNothing . (foldl merge (Just [])) . (map sort) . pairs<br>
<br>pairs []=[]<br>pairs [x]=[[x]]<br>pairs (x:y:xs)=[x,y]:pairs xs<br><br>sort []=Just []<br>sort [x]=Just [x]<br>sort [x,y] | x>y=Just [y,x]<br> | y>x=Just [x,y]<br> | x==y=Nothing<br><br>merge::(Eq a, Ord a) => Maybe [a]->Maybe [a]->Maybe[a]<br>
merge _ Nothing = Nothing<br>merge Nothing _ = Nothing<br>merge (Just []) (Just xs)=Just xs<br>merge (Just xs) (Just [])=Just xs<br>merge (Just (x:xs)) (Just (y:ys)) | x==y = Nothing<br> | x>y = (Just y) +? (merge (Just (x:xs)) (Just ys))<br>
| x<y = (Just x) +? (merge (Just xs) (Just (y:ys)))<br><br>(+?) = liftM2 (:)<br><br><br><br>My version of the merge sort returns Nothing whenever it finds two equal entries, aborting all subsequent comparisons.<br>
<br>I have a few questions for the friendly people at this cafe:<br><br>1) Is there any improvement I can make?<br>2) Can it be parallelized (par, pseq)?<br><br><br>Best regards,<br><br>Rafael<br>