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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With