[Haskell-cafe] Re: Best way to write endsWith (RESOLVED)
Hi,Thanks all. I learnt quite a few things:1. There was already an equivalent builtin function.2. That the best function argument order is the least surprising one.3. That I can choose my preferred function order by changing the name of the function. 4. That the most efficient way of doing something in Haskell can be so short.5. Operators really are awesome.-JohnOn 10/22/06, John Ky [EMAIL PROTECTED] wrote:Hello, I have this function here: endsWith :: Eq a = [a] - [a] - Bool endsWith suffix list | lengthDifference 0 = False | otherwise = (drop lengthDifference list) == suffix where lengthDifference = (length list) - (length suffix)Would this be the preferred function argument order? Or is the reverse (ie. endsWith list suffix) better?I like being able to say abc `endsWith` c, but I also like to be able to say map (endsWith 't') [cat, dog] but I can't have both. By the way, is there a better way to write this function to be clearer and more efficient?Thanks-John ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Best way to write endsWith
John Ky wrote: Hello, I have this function here: endsWith :: Eq a = [a] - [a] - Bool endsWith suffix list | lengthDifference 0 = False | otherwise = (drop lengthDifference list) == suffix where lengthDifference = (length list) - (length suffix) I thinks that's what List.isSuffixOf does. I like being able to say abc `endsWith` c, but I also like to be able to say map (endsWith 't') [cat, dog] but I can't have both. import List c `isSuffixOf` abc map (t `isSuffixOf`) [cat, dog] --Stephan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Best way to write endsWith
On 10/21/06, Stephan Walter [EMAIL PROTECTED] wrote: John Ky wrote: Hello, I have this function here: endsWith :: Eq a = [a] - [a] - Bool endsWith suffix list | lengthDifference 0 = False | otherwise = (drop lengthDifference list) == suffix where lengthDifference = (length list) - (length suffix) I thinks that's what List.isSuffixOf does. isSuffixOf x y = reverse x `isPrefixOf` reverse y -- Cheers, Lemmih ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe