0

What I got is

digitIndex :: String -> Int
digitIndex [] = 1
digitIndex (x:xs) =
 if
 isDigit x == True
 then
 -- Count list
 else
 -- Create list with x(x is not a digit)

What my idea is is to make a list with all the x that he is passing so when he passes a digit he only needs to count the list and that will be the position of the digit(when you count a +1).

Only thing is I don't know how to get the job more done. Can you guys help me out with tips?

asked Mar 18, 2014 at 20:11
1
  • 2
    Please don't vandalize your own posts. If it is necessary to remove them, flag for moderator attention and ask for removal. Commented Mar 19, 2014 at 15:09

2 Answers 2

3

You can use findIndex:

import Data.List
digitIndex :: String -> Int
digitIndex = maybe 0 id . findIndex isDigit
answered Mar 18, 2014 at 20:31
Sign up to request clarification or add additional context in comments.

Comments

0

Just the normal recursion:

digitIndex :: String -> Int
digitIndex [] = 0
digitIndex (x:xs) = if isDigit x
 then 1
 else 1 + digitIndex xs

If the first character itself is a digit, then the function returns 1 else it just adds 1 and passes the remaining string (xs) to the function and the recursion goes on.

Also note that the above function doesn't work properly when the String doesn't have a number at all.

And also checking isDigit == True isn't necessary.

answered Mar 18, 2014 at 20:14

3 Comments

That wont make the good output because if I fill in: digitIndex "hel3lo" it will output 1 and not the 4 (the 3 is in the 4th place of the string)
@user3434886 I understood your question wrongly, have updated the solution.
Maybe I needed to be more clear in my explanation but you solved my problem. Thank you!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.