Reputation: 165
this is what I'm trying to do:
If I give
$hello world
I would get world
What I am doing is this :
tail (unwords (words "$hello world"))
but I'm getting "hello world"
instead of world
What do I have to do to get it right?
Upvotes: 2
Views: 506
Reputation: 1009
As fjh correctly stated, the array of strings ["$hello", "world"]
is rejoined into a string "$hello world"
and tail
then chops off the first Char $
. I would recommend using the function last
instead of tail $ unwords
which will get the last element out of the array of words world
.
Prelude> last $ words $ "$hello world"
"world"
Upvotes: 0
Reputation: 13091
You have to apply unwords
after tail
, not the other way around.
The intended sequence of steps is (probably) as follows:
The way you do it, you split and immediately re-join the words and then you just drop the first character of the resulting string (since a String is just a list of Chars).
Upvotes: 8
Reputation: 54068
What you're wanting to do is
unwords $ tail $ words "$hello world"
Working through it in GHCi we get
> words "$hello world"
["$hello", "world"]
> tail $ words "$hello world"
["world"]
> unwords $ tail $ words "$hello world"
"world"
Upvotes: 2