Re: Using an accumulator, iterating...
Brett Kelly wrote: Hello all, I'm trying to write a function that takes a list and a element (same type) and returns the index of the first instance of the element in the list. like: getindex brett 'e' would return 2, etc. i'm trying to accomplish this using an accumulator, here's what i've got: pyindex :: Eq a = a - [a] - Maybe Int pyindex c l = pyindex' 0 chr (x:xs) where pyindex' count chr (x:xs) = do if x == chr then return count else pyindex' (count + 1) chr xs now, i know i've got a syntax problem, because i'm pretty sure my logic is correct (or at least MOSTLY correct). can anybody see what's wrong with my stuff? Sure. Three comments: 1) You don't need (or want) the `do' -- that's used for dealing with monads. 2) The function's signature indicates a return type of `Maybe Int', yet you're trying to return an Int. 3) What if you _don't_ find the target? Additional comments: The `if...then...else' form may not be the clearest way -- or the most `Haskell-ish' way of expressing this computation. You may want to look through the standard prelude to find how things like this are usually done. Good luck and HTH, --ag -- Artie Gold -- Austin, Texas ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: Using an accumulator, iterating...
On Saturday, 2003-06-21, 03:23, CEST, Brett Kelly wrote: Hello all, I'm trying to write a function that takes a list and a element (same type) and returns the index of the first instance of the element in the list. like: getindex brett 'e' would return 2, etc. i'm trying to accomplish this using an accumulator, here's what i've got: pyindex :: Eq a = a - [a] - Maybe Int pyindex c l = pyindex' 0 chr (x:xs) where pyindex' count chr (x:xs) = do if x == chr then return count else pyindex' (count + 1) chr xs now, i know i've got a syntax problem, because i'm pretty sure my logic is correct (or at least MOSTLY correct). can anybody see what's wrong with my stuff? thanks! Hello, I think, the following code, which uses certain prelude functions, would be clearer and more elegant: pyindex :: Eq a = a - [a] - Maybe Int pyindex c l = lookup c (zip l [0 ..]) I would it generally consider better style to not do recursion explicitely but use the recursion already provided by predefined functions like lookup. Wolfgang ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: Using an accumulator, iterating...
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Fri, Jun 20, 2003 at 06:23:42PM -0700, Brett Kelly wrote: now, i know i've got a syntax problem, because i'm pretty sure my logic is correct (or at least MOSTLY correct). Try: pyindex :: Eq a = a - [a] - Maybe Int pyindex c l = pyindex' 0 l where pyindex' _ [] = Nothing pyindex' n (x:xs) = if (x == c) then Just n else pyindex' (n+1) xs then, in ghci: Pyindex pyindex 'e' brett Just 2 Pyindex pyindex 'm' brett Nothing Pyindex pyindex 't' brett Just 3 hope this helps. mike - -- mike castleman / m at mlcastle dot net / http://mlcastle.net / (646) 382-7220 aolim: mlcastle / icq: 3520821 / yahoo: mlc000 please avoid sending me microsoft word, excel, or powerpoint documents. see http://www.gnu.org/philosophy/no-word-attachments.html for more info. -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQE+87lKrbXc6n5AevkRAtHwAJ9ARgtMHxWmlHWdgvOmIOFdi0W6FwCeOnLk TXyz94uSH3RufPx7xpZhtX4= =98Ym -END PGP SIGNATURE- ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: Using an accumulator, iterating...
Brett Kelly wrote: I'm trying to write a function that takes a list and a element (same type) and returns the index of the first instance of the element in the list. like: getindex brett 'e' would return 2, etc. i'm trying to accomplish this using an accumulator, here's what i've got: pyindex :: Eq a = a - [a] - Maybe Int pyindex c l = pyindex' 0 chr (x:xs) None of chr, x or xs are defined here; if you change the above line to: pyindex c l = pyindex' 0 c l it works. Except that you will get a match failure if the list doesn't contain the specified element, so you also need a base case: pyindex' _ _ [] = Nothing -- Glynn Clements [EMAIL PROTECTED] ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: Using an accumulator, iterating...
Awesome, thanks! Sometime around Fri, Jun 20, 2003 at 09:47:54PM -0400, mike castleman said: On Fri, Jun 20, 2003 at 06:23:42PM -0700, Brett Kelly wrote: now, i know i've got a syntax problem, because i'm pretty sure my logic is correct (or at least MOSTLY correct). Try: pyindex :: Eq a = a - [a] - Maybe Int pyindex c l = pyindex' 0 l where pyindex' _ [] = Nothing pyindex' n (x:xs) = if (x == c) then Just n else pyindex' (n+1) xs then, in ghci: Pyindex pyindex 'e' brett Just 2 Pyindex pyindex 'm' brett Nothing Pyindex pyindex 't' brett Just 3 hope this helps. mike -- mike castleman / m at mlcastle dot net / http://mlcastle.net / (646) 382-7220 aolim: mlcastle / icq: 3520821 / yahoo: mlc000 please avoid sending me microsoft word, excel, or powerpoint documents. see http://www.gnu.org/philosophy/no-word-attachments.html for more info. ___ Haskell-Cafe mailing list [EMAIL PROTECTED] http://www.haskell.org/mailman/listinfo/haskell-cafe -- Brett Kelly [EMAIL PROTECTED] This message has been digitally autographed using GnuPG. Open Source software will continue to change the world... http://www.opensource.org pgp0.pgp Description: PGP signature