sachalondon
sachalondon

Reputation: 165

Haskell Tail Split

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

Answers (3)

Aaron
Aaron

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

fjh
fjh

Reputation: 13091

You have to apply unwords after tail, not the other way around.

The intended sequence of steps is (probably) as follows:

  1. Break string into list of words
  2. Drop the first word from the list
  3. Join the remaining words to a string

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

bheklilr
bheklilr

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

Related Questions