user1934428
user1934428

Reputation: 22225

Can not use spltOn to split String on spaces

I have a string containing words separated by spaces. I would like to get the individual words.

I verified (in ghci) that the following solution works well:

import Data.String
list_of_words = words "a b c"

but I happened to stumble accross the splitOn function, and since I am still in the basement of Haskell programming, and a curious mind, I wondered whether I could also achieve my goal using this function. Hence I tried:

import Data.List
lw = Data.List.splitOn "a b c"

only to get the error message

Not in scope: ‘Data.List.splitOn’

No module named ‘Data.List’ is imported

I don't understand the first message, but the second one is ridiculous, since I imported the module, and the module name is shown in the ghci prompt. Then I realized that the website above referst to a module Data.List.Split, not Data.List. Hence I tried

import Data.List.Split

only to get the error messages

Could not find module ‘Data.List.Split’

Well, curiosity killed the cat. I should have stayed with my original words solution. But since I opened the box now, could some kind soul explain to me, why my attempts have failed, and how I could have it done correctly?

UPDATE Following the comments to my question, I tried

splitOn " " "a  c d" 

but this time get the error message

Variable not in scope: splitOn :: [Char] -> [Char] -> t

Perhaps you meant one of these:

‘Text.splitOn’ (imported from Data.Text), ‘splitAt’ (imported from Data.List), ‘Text.splitAt’ (imported from Data.Text)

It looks like Data.Text (which I happened to import too) also has a splitOn function with different signature. Hence, I started a fresh ghci without any imports, and just did a

import Data.List
splitOn " " "a b c"

This gave also the error message

Variable not in scope: splitOn :: [Char] -> [Char] -> t

Upvotes: 0

Views: 794

Answers (1)

Li-yao Xia
Li-yao Xia

Reputation: 33389

Install the split library to be able to use it. Since you're a stack user here are the commands for that:

stack build split  # install split (some people prefer "stack build" over "stack install", but the difference concerns only executables)
stack ghci         # start ghci using the environment of packages installed by stack

Upvotes: 4

Related Questions