2013-09-23 2 views
1

문자열을 한 번 전달하면 함수 내에서 발견 된 공백을 제거하는 함수 removeSpaces을 작성하려고합니다. 내가 지금까지 가지고 무엇
은 다음과 같습니다 이전과 String 내에서 공백 이후에있는 문자열의 조각 :함수 문제가 범위에 없습니다 : 데이터 생성자

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

이 그러나 "데이터 생성자를하지 범위에"나에게 메시지를주는 유지합니다. 이것에 대한 도움이 될 것입니다.

+0

그것은 당신이 어떤 컴파일러/인터프리터의 버전을 사용하고 무엇을하는, 나를 위해 작동 정확한 출력입니까? 또한'removeSpaces "Hello World !! 11!" == "HelloWorld", 원하는 것이 아닐 수도 있습니다. –

답변

2

필터가 해당 사례를 처리하므로 빈 목록의 대소 문자는 필요하지 않지만이 함수는 문제가없는 것처럼 보입니다.

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

당신은 그것은 잘 작동이 기능

+0

나는 퍼스널을 removeSpaces로 "no spaces"라고 부르고 있는데, 이것은 nospaces를 반환해야한다. – user2807679

+0

GHCi를 사용하고 있습니까, 아니면 파일의 최상위 표현식으로'removeSpaces '를 공백없이'그냥 입력하고 있습니까? –

+0

전화를 걸 때 문자열 주변에 큰 따옴표가 붙어 있습니까? – Alex

1

를 호출하는 방법의 예를 줄 수 :

$ 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> 
관련 문제