2016-10-25 9 views
1

私はリスト内の1つの要素の値にしたいいくつかのリストを追加し、だから私は[」Haskellはで一緒に

changePoint :: (Int,Int) -> [[[Char]]] -> [[Char]] 

changePoint (x,y) maze = let 

      linelement = maze !! (y-1) 

-- value ["X"," "," ",...," ","X"] 

      rowelement = chunksOf 1 $ head linelement 
-- type:[[a]]; value ["X"," "," "," "," "," "," "," "," "," "," "," "," ","X"] 

      l = length rowelement 
      list = take (x-1) rowelement 

--   in take (x-1) rowelement -- ["X"] 

      in take (x-1) rowelement ++ (["."] : (drop (x) rowelement)) 

が、私はリストを追加「を取る(X-1)rowelement」にしたい行なったし、 」。 "]" と "ドロップ(x)のrowelement "] "" ["、リストのタイプは[[]]

Couldn't match expected type ‘Char’ with actual type ‘[Char]’ 
    In the expression: "." 
    In the first argument of ‘(:)’, namely ‘["."]’ 
    In the second argument of ‘(++)’, namely 
    ‘(["."] : (drop (x) rowelement))’ Failed, modules loaded: none. 

x = 2 . 

になります私は問題がある知っている" が、私は本当にドン」それを修正する方法。

真のリターンはあなたがsomefunctionの種類を取得するために:t somefunctionを使用することができ、GHCiので["X","."," "," ",..,"X" ]

答えて

1

でなければなりません。

ここで問題は(:)で、GHCIが私たちに語っていることを見せてくれます。

Prelude λ> :t (:) 
(:) :: a -> [a] -> [a] 

ので(:)は、aのリストをaを取り、新しいリストを返します。手元にあるユースケースに特化して、(:)[Char] -> [[Char]] -> [[Char]]a = [Char]のため)タイプがあります。しかし["."]のタイプは[[Char]]なので、(:)の予想と一致しません。

take (x-1) rowelement ++ ("." : (drop (x) rowelement))[]が約"."であることに注意してください)を使用すると、関数は正常にコンパイルされます。

1

いずれかを使用:

(take (x-1) rowelement) ++ ["."] ++ (drop x rowelement) 

または[]

(take (x-1) rowelement) ++ ("." : (drop x rowelement)) 

は必要ない

+1

'['] '最初の例で必要実際います。 – tomahh