You misunderstand the priority. It:
f idx str = last $ (take . succ) idx str
It is analyzed as follows:
f idx str = last $ ( (take . succ) idx str )
Not (what do you think):
f idx str = ( last $ (take . succ) ) idx str
$ has an extremely low priority for any operator, and a function call has an extremely high level. . has the second highest, therefore (take . succ) binds it with arguments ( idx str ) before binding to last $ .
Also, the function (as it compiles) does not do what looks like what you want. It increments idx , then takes this character from the string. If this is what you want, why use succ when (+1) works? You have already limited the type to integers.
As written, your function is identical to the operator !! is just an array index function. Is this what you want? Or do you want a succ element at a given index? This can be done as follows:
f :: Enum a => Int -> [a] -> a f idx str = succ $ str !! idx -- or f idx str = succ $ (!!) str idx -- or, only one argument f idx = succ . (!! idx)
I am still working on a version with no written arguments. Perhaps it is more important to write working code ?;)
Chris lutz
source share