Skip to main content
added 4 characters in body
Source Link
user903589
user903589

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. Just use an iterative recursive helper function like this...

You can use either a local function, or the lifted version, which is what I have here.

whatIndex' :: (Eq a) => Integer -> a -> [a] -> Integer whatIndex' _ _ [] = -1 whatIndex' i p (x:xs) | p == x = i | otherwise = whatIndex' (i+1) p xs whatIndex p xs = whatIndex' 0 p xs main = print $ whatIndex 'a' "bbbbaccc" 

Here's a non tail-recursive version:

whatIndex p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex p xs 

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. So that means that the function is not calling some other function (like +) in the "tail position" (the last place where a function call takes place). You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is +.

http://en.wikipedia.org/wiki/Tail_call

Edit: here's a version that corresponds more closely with your specification, although it's a bit convoluted and inefficient.

whatIndex p (x:xs) | not (any (==p) (x:xs)) = -1 | otherwise = whatIndex' p xs where whatIndex' p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex' p xs 

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. Just use an iterative recursive helper function like this...

You can use either a local function, or the lifted version, which is what I have here.

whatIndex' :: (Eq a) => Integer -> a -> [a] -> Integer whatIndex' _ _ [] = -1 whatIndex' i p (x:xs) | p == x = i | otherwise = whatIndex' (i+1) p xs whatIndex p xs = whatIndex' 0 p xs main = print $ whatIndex 'a' "bbbbaccc" 

Here's a non tail-recursive version:

whatIndex p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex p xs 

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. So that means that the function is not calling some other function (like +) in the "tail position" (the last place where a function call takes place). You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is +.

http://en.wikipedia.org/wiki/Tail_call

Edit: here's a version that corresponds more closely with your specification, although it's a bit convoluted and inefficient.

whatIndex p (x:xs) | not (any (==p) (x:xs)) = -1 | otherwise = whatIndex' p xs where whatIndex' p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex' p xs 

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. Just use an iterative recursive helper function like this...

You can use either a local function, or the lifted version, which is what I have here.

whatIndex' :: (Eq a) => Integer -> a -> [a] -> Integer whatIndex' _ _ [] = -1 whatIndex' i p (x:xs) | p == x = i | otherwise = whatIndex' (i+1) p xs whatIndex p xs = whatIndex' 0 p xs main = print $ whatIndex 'a' "bbbbaccc" 

Here's a non tail-recursive version:

whatIndex p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex p xs 

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. So that means that the function is not calling some other function (like +) in the "tail position" (the last place where a function call takes place). You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is +.

http://en.wikipedia.org/wiki/Tail_call

Edit: here's a version that corresponds more closely with your specification, although it's a bit convoluted and inefficient.

whatIndex p xs | not (any (==p) xs) = -1 | otherwise = whatIndex' p xs where whatIndex' p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex' p xs 
added 341 characters in body
Source Link
user903589
user903589

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. Just use an iterative recursive helper function like this...

You can use either a local function, or the lifted version, which is what I have here.

whatIndex' :: (Eq a) => Integer -> a -> [a] -> Integer whatIndex' _ _ [] = -1 whatIndex' i p (x:xs) | p == x = i | otherwise = whatIndex' (i+1) p xs whatIndex p xs = whatIndex' 0 p xs main = print $ whatIndex 'a' "bbbbaccc" 

Here's a non tail-recursive version:

whatIndex p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex p xs 

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. So that means that the function is not calling some other function (like +) in the "tail position" (the last place where a function call takes place). You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is +.

http://en.wikipedia.org/wiki/Tail_call

Edit: here's a version that corresponds more closely with your specification, although it's a bit convoluted and inefficient.

whatIndex p (x:xs) | not (any (==p) (x:xs)) = -1 | otherwise = whatIndex' p xs where whatIndex' p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex' p xs 

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. Just use an iterative recursive helper function like this...

You can use either a local function, or the lifted version, which is what I have here.

whatIndex' :: (Eq a) => Integer -> a -> [a] -> Integer whatIndex' _ _ [] = -1 whatIndex' i p (x:xs) | p == x = i | otherwise = whatIndex' (i+1) p xs whatIndex p xs = whatIndex' 0 p xs main = print $ whatIndex 'a' "bbbbaccc" 

Here's a non tail-recursive version:

whatIndex p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex p xs 

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. So that means that the function is not calling some other function (like +) in the "tail position" (the last place where a function call takes place). You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is +.

http://en.wikipedia.org/wiki/Tail_call

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. Just use an iterative recursive helper function like this...

You can use either a local function, or the lifted version, which is what I have here.

whatIndex' :: (Eq a) => Integer -> a -> [a] -> Integer whatIndex' _ _ [] = -1 whatIndex' i p (x:xs) | p == x = i | otherwise = whatIndex' (i+1) p xs whatIndex p xs = whatIndex' 0 p xs main = print $ whatIndex 'a' "bbbbaccc" 

Here's a non tail-recursive version:

whatIndex p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex p xs 

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. So that means that the function is not calling some other function (like +) in the "tail position" (the last place where a function call takes place). You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is +.

http://en.wikipedia.org/wiki/Tail_call

Edit: here's a version that corresponds more closely with your specification, although it's a bit convoluted and inefficient.

whatIndex p (x:xs) | not (any (==p) (x:xs)) = -1 | otherwise = whatIndex' p xs where whatIndex' p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex' p xs 
deleted 27 characters in body
Source Link
user903589
user903589

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. Just use an iterative recursive helper function like this...

You can use either a local function, or the lifted version, which is what I have here.

whatIndex' :: (Eq a) => Integer -> a -> [a] -> Integer whatIndex' _ _ [] = -1 whatIndex' i p (x:xs) | p == x = i | otherwise = whatIndex' (i+1) p xs whatIndex p xs = whatIndex' 0 p xs main = print $ whatIndex 'a' "bbbbaccc" 

Here's a non tail-recursive version:

whatIndex _ [] = -1 whatIndex p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex p xs 

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. So that means that the function is not calling some other function (like +) in the "tail position" (the last place where a function call takes place). You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is +.

http://en.wikipedia.org/wiki/Tail_call

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. Just use an iterative recursive helper function like this...

You can use either a local function, or the lifted version, which is what I have here.

whatIndex' :: (Eq a) => Integer -> a -> [a] -> Integer whatIndex' _ _ [] = -1 whatIndex' i p (x:xs) | p == x = i | otherwise = whatIndex' (i+1) p xs whatIndex p xs = whatIndex' 0 p xs main = print $ whatIndex 'a' "bbbbaccc" 

Here's a non tail-recursive version:

whatIndex _ [] = -1 whatIndex p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex p xs 

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. So that means that the function is not calling some other function (like +) in the "tail position" (the last place where a function call takes place). You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is +.

http://en.wikipedia.org/wiki/Tail_call

1+whatIndex p as will go through all of the remaining list and count them, it won't give you the index. Just use an iterative recursive helper function like this...

You can use either a local function, or the lifted version, which is what I have here.

whatIndex' :: (Eq a) => Integer -> a -> [a] -> Integer whatIndex' _ _ [] = -1 whatIndex' i p (x:xs) | p == x = i | otherwise = whatIndex' (i+1) p xs whatIndex p xs = whatIndex' 0 p xs main = print $ whatIndex 'a' "bbbbaccc" 

Here's a non tail-recursive version:

whatIndex p (x:xs) | p == x = 0 | otherwise = 1 + whatIndex p xs 

Tail recursion refers to a class of recursive functions where the "last" or "final" function call in a recursive function is to the function itself. So that means that the function is not calling some other function (like +) in the "tail position" (the last place where a function call takes place). You can clearly see that the final function that is called in the first version of whatIndex is whatIndex whereas the final function that is called in the second version (with a call to whatIndex as a parameter) is +.

http://en.wikipedia.org/wiki/Tail_call

added 132 characters in body
Source Link
user903589
user903589
Loading
added 132 characters in body
Source Link
user903589
user903589
Loading
Source Link
user903589
user903589
Loading