Rnet
Rnet

Reputation: 5040

Haskell : words, unwords delimiter

Is there any way I can provide a delimiter to words and unwords in haskell, to make it similar to split and join in python?

Upvotes: 9

Views: 3901

Answers (3)

xged
xged

Reputation: 1243

Text has splitOn & intercalate which is equivalent to Python's split & join.

Upvotes: 1

fuz
fuz

Reputation: 93107

Please also have a look at the genial package split. It provides a module Data.List.Split for all sort of splitting.

Upvotes: 10

geekosaur
geekosaur

Reputation: 61449

No, but they're really just (optimized versions of) applications of Data.List.break and Data.List.intersperse, respectively.

pythonicSplit      :: String -> Char -> [String]
pythonicSplit "" _ =  []
pythonicSplit s  c =  let (r,rs) = break (== c) s
                       in r : pythonicSplit rs c

pythonicJoin       :: [String] -> Char -> String
pythonicJoin  ss c =  intersperse c ss -- or: flip intersperse

Upvotes: 4

Related Questions