Using an accumulator, "iterating"...

mike castleman m@mlcastle.net
Fri, 20 Jun 2003 21:47:54 -0400


-----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-----