Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 369
sachalondon Avatar asked Aug 01 '26 03:08

sachalondon


2 Answers

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).

like image 119
fjh Avatar answered Aug 04 '26 07:08

fjh


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"
like image 36
bheklilr Avatar answered Aug 04 '26 05:08

bheklilr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!