1

I am trying to write a function removeSpaces that once handed a string it will remove any spaces found within it.
What I got so far is:

import Data.Char
removeSpaces :: [Char] -> [Char]
removeSpaces [] = []
removeSpaces xs = filter isAlpha xs

This however keeps giving me the message "not in scope: data constructor" with the pieces of the string that are before and after any spaces within the String. Any help with this would be great.

1
  • It works for me, what version of what compiler/interpreter are you using and what is the exact output? Also `removeSpaces "Hello World!!11!" == "HelloWorld", which may not be what you want. Commented Sep 23, 2013 at 15:05

2 Answers 2

2

That function looks fine although you don't need the case for the empty list as filter deals with that case.

import Data.Char
removeSpaces :: [Char] -> [Char]
removeSpaces xs = filter isAlpha xs

Can you give an example of how you are calling the function

Sign up to request clarification or add additional context in comments.

5 Comments

I am calling the funnction as removeSpaces "no spaces" which should return nospaces, as I understand.
Are you using GHCi or are you just typing removeSpaces "no spaces" as a top level expression in your file?
Are you sure you have quotation marks around your string where you call it?
I go into the directory where the script is and use ghci then use :l a2 and type the command in.
It finally worked, it was somehow picking up an older version of the script. I just pasted it into a different file to get around that. Thanks everyone.
1

It works fine:

$ echo "import Data.Char
> removeSpaces :: [Char] -> [Char]
> removeSpaces [] = []
> removeSpaces xs = filter isAlpha xs" > so.hs

$ ghci so.hs
GHCi, version 7.6.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( so.hs, interpreted )
Ok, modules loaded: Main.
*Main> removeSpaces "no spaces"
"nospaces"
*Main> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.