Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell too many where clauses, any alternate suggestions

I am totally new in Haskell and when writing small programs i normally end up with too many where clauses to check many things in the function, so it is good practice to write where clauses or is there any other good alternatives for this ?

for example in the code below i tried to find if there is ant duplicate elements in each row of the two dimensional list, it works and every thing is content in same function but i am not satisfied with how code looks and i find it more imperative style of approaching to the problem so i am looking for any suggestion or thought on this from the experienced people out there.

noDups :: [[a]] -> Bool
noDups du = and (checkSu du)
       where
       checkDup []     = []
       checkDup (x:xs) = checkRow x ++ checkDup xs
             where
             checkRow []     = [] 
             checkRow (x:xs) = [x /= y | y <- xs] ++ checkRow xs

once again this code is just to illustrate one problem, i am looking for approach to formulate problem in functional style. your suggestions or articles, links would be much helpful.

Thanks

like image 473
dinsim Avatar asked Nov 22 '09 18:11

dinsim


1 Answers

Try to write abstract, reusable functions You'll be able to compose them much easier

isUnique :: Eq a => [a] -> Bool
isUnique [] = True
isUnique (x:xs) = all (/= x) xs && isUnique xs

noDups :: Eq a => [[a]] -> Bool
noDups = all isUnique
like image 157
Dario Avatar answered Oct 19 '22 00:10

Dario