簡體   English   中英

Haskell試圖在[String]中改變字符串

[英]Haskell trying to mutate string in [String]

movex [] a s = []    
movex (x:xs) a s
| elem a x = moveNow x a s
| otherwise = x : (movex xs a s)
where
  moveNow x a s
    | s == 'l' = moveNow2 x a
    where
        moveNow2 [] _ = []
        moveNow2 (x:y:xs) a
          | x == ' ' && y == a = a : x : moveNow2 (y:xs) a
          | otherwise = x : moveNow2 (y:xs) a

< - 這就是我現在所得到的

我正在嘗試創建一個遍歷[string]的函數,找到正確的字符串,然后將其變異。

給定輸入

func ["abc", "dfg"] f l -- move f in this list 1 space left --

預期產出

["abc", "fdg"]

現在我被困在movex函數,給我錯誤

Couldn't match expected type `Char' with actual type `[Char]'
In the first argument of `(:)', namely `x'
In the expression: x : (movex xs a s)

直接解決錯誤就是更換線路

| elem a x = moveNow x a s

| elem a x = moveNow x a s : movex xs a s

或者,可能

| elem a x = moveNow x a s : xs

取決於您在第一場比賽后想要做的事情:繼續尋找某個角色,或保持其他角色不變。

你的moveNow函數有返回類型String[Char] ,而movex[String][[Char]] ,這就是編譯器抱怨的原因。

為避免此類問題(或更容易修復),請考慮編寫顯式類型簽名,如下所示:

movex :: [String]->String->String->[String]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM