Can i split a String by its element ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Can i split a String by its element ?

z_axis
I want to split "2,15,33,0,8,1,16,18" to ([2,15,33],[8,1,16,18]).  the "0" will by discarded.

However, it seems that there isnot any standard function to do it.


Sincerely!
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: Can i split a String by its element ?

Luke Palmer-2
See the http://hackage.haskell.org/package/split package.  You should
be able to do this by splitting the string on comma, and then
splitting the result on 0, plus some plumbing.

Luke

On Mon, Feb 21, 2011 at 11:46 PM, z_axis <[hidden email]> wrote:

>
> I want to split "2,15,33,0,8,1,16,18" to ([2,15,33],[8,1,16,18]).  the "0"
> will by discarded.
>
> However, it seems that there isnot any standard function to do it.
>
>
> Sincerely!
>
> -----
> e^(π.i) + 1 = 0
> --
> View this message in context: http://haskell.1045720.n5.nabble.com/Can-i-split-a-String-by-its-element-tp3395066p3395066.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can i split a String by its element ?

Brent Yorgey-2
Prelude Data.List.Split> (splitOn [0] . map read . splitOn ",") "2,15,33,0,8,1,16,18"
[[2,15,33],[8,1,16,18]]

-Brent

On Tue, Feb 22, 2011 at 12:03:45AM -0700, Luke Palmer wrote:

> See the http://hackage.haskell.org/package/split package.  You should
> be able to do this by splitting the string on comma, and then
> splitting the result on 0, plus some plumbing.
>
> Luke
>
> On Mon, Feb 21, 2011 at 11:46 PM, z_axis <[hidden email]> wrote:
> >
> > I want to split "2,15,33,0,8,1,16,18" to ([2,15,33],[8,1,16,18]).  the "0"
> > will by discarded.
> >
> > However, it seems that there isnot any standard function to do it.
> >
> >
> > Sincerely!
> >
> > -----
> > e^(π.i) + 1 = 0
> > --
> > View this message in context: http://haskell.1045720.n5.nabble.com/Can-i-split-a-String-by-its-element-tp3395066p3395066.html
> > Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
> >
> > _______________________________________________
> > Haskell-Cafe mailing list
> > [hidden email]
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can i split a String by its element ?

Richard A. O'Keefe
In reply to this post by z_axis

On 22/02/2011, at 7:46 PM, z_axis wrote:

>
> I want to split "2,15,33,0,8,1,16,18" to ([2,15,33],[8,1,16,18]).  the "0"
> will by discarded.
>
> However, it seems that there isnot any standard function to do it.

let (front,(_:back)) = List.span (/= 0) xs in (front,back)

What do you plan to do if there is no 0 there?

[Yes, I know this can be done in point free style.  I hereby announce
defeat in code golf.]


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Can i split a String by its element ?

z_axis
In reply to this post by Brent Yorgey-2
:hoogle splitOn
No results found

:m + Data.Text
 splitOn "," "2,15,33,0,8,1,16,18"

<interactive>:1:8:
    Couldn't match expected type `Text' against inferred type `[Char]`
...

:i splitOn
splitOn :: Text -> Text -> [Text] -- Defined in Data.Text

It seems this "splitOn" is not one used by you ?
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: Can i split a String by its element ?

Ivan Lazar Miljenovic
On 23 February 2011 14:06, z_axis <[hidden email]> wrote:
>
> :hoogle splitOn
> No results found
>
> :m + Data.Text
>  splitOn "," "2,15,33,0,8,1,16,18"

It's from Data.List.Split, not Data.Text if you want to user Strings.
Alternatively, you can use the Text version with OverloadedStrings.

--
Ivan Lazar Miljenovic
[hidden email]
IvanMiljenovic.wordpress.com

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe