You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Haskell变量赋值问题:多空格合并函数编译报错咨询

Fixing Your Multiple Spaces to Single Space Function in Haskell

Let's walk through what's causing that parse error and build a working solution for your problem.

First, the Immediate Error

The parse error on = happens because Haskell doesn't allow assignment inside expressions. In your code, you wrote:

if isInfixOf " " index then index = " " else index

The index = " " part is invalid here—Haskell uses = to define bindings (like function definitions), not to mutate variables (since it's a pure functional language). That's why the compiler throws a fit.

Next, Fixing the Logic

Even if we fixed the syntax, your current logic doesn't actually handle multiple spaces—it just checks if a space exists at all. To collapse consecutive spaces into one, we have two great options:


Option 1: Use Standard Library Functions (Simplest!)

Haskell's standard library has words and unwords which do exactly what we need:

  • words splits a string into a list of words, ignoring any number of whitespace characters between them.
  • unwords joins that list back into a single string, with exactly one space between each word.

Here's the one-liner:

getSpaces :: String -> String
getSpaces = unwords . words

Try this in GHCI—getSpaces "hello world foo" will return "hello world foo" perfectly.


Option 2: Manual Recursive Implementation (Great for Learning)

If you want to build the logic yourself to understand how it works, here's a recursive version:

getSpaces :: String -> String
-- Base case: empty string stays empty
getSpaces [] = []
-- If we hit a space, keep one space, then skip all following consecutive spaces
getSpaces (' ':xs) = ' ' : getSpaces (dropWhile (== ' ') xs)
-- For non-space characters, just keep the char and recurse on the rest
getSpaces (x:xs) = x : getSpaces xs

Let's break this down:

  • When we see a space, we add one space to the result, then use dropWhile to skip every subsequent space before continuing the recursion.
  • For non-space characters, we just append the character to the result and keep processing the rest of the string.

Quick Notes on Your Original Code

  • The parameter name index is a bit confusing here—since it's a full string, something like str or s would be more readable.
  • Checking length index == 1 is unnecessary; both solutions above handle single-character strings correctly automatically.

内容的提问来源于stack exchange,提问作者Dan Hessler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:34:42