
Hi, enlightenment much appreciated for my newbie question -- to achieve: append' [ [1, 2], [3, 4], [5] ] 6 -> [ [1, 2], [3, 4], [5, 6] ] append' [ ['1', '2'], ['3'] ] '4' -> [ ['1', '2'], ['3', '4'] ] append' [ [True], [True] ] False -> [ [True], [True, False] ] so I (naively) write: 18) append' :: [[a]] -> a -> [[a]] 19) append' [] y = [[y]] 20) append' x:xs y = [(init x:xs)] ++ [(tail xs)++[y]] and ghc(5.02.3) says: $ ghc p3a.hs p3a.hs:20: Parse error in pattern thanx in advance! -- Regards, Jerry

Hi, JiJie> 20) append' x:xs y = [(init x:xs)] ++ [(tail xs)++[y]] function application (blank) binds stronger than :, thus you should write append' (x:xs) y = ... Cheers -- Christoph

* Ch. A. Herrmann
Hi,
JiJie> 20) append' x:xs y = [(init x:xs)] ++ [(tail xs)++[y]]
function application (blank) binds stronger than :, thus you should write
append' (x:xs) y = ...
-- so I added the parenthesis: 18) append' :: [[a]] -> a -> [[a]] 19) append' [] y = [[y]] 20) append' (x:xs) y = [(init (x:xs))] ++ [(tail xs)++[y]] -- and now ghc says: p3a.hs:20: Cannot unify the type-signature variable `a' with the type `[a]' Expected type: a Inferred type: [a] In the first argument of `(:)', namely `x' In the first argument of `init', namely `(x : xs)' -- okey, init doesn't operate on [[a]], so I'll just write mine: concat' :: [[a]] -> [[a]] -> [[a]] concat' [] [] = [[]] concat' [] [y] = [y] concat' [x] [] = [x] concat' [x] [y] = [x, y] init' :: [[a]] -> [[a]] init' [[]] = [[]] init' (x:xs) = case xs of [[]] -> [[]] (y:ys) -> concat' (concat' [x] [y]) (init' xs) -- together with my tail': tail' :: [[a]] -> [[a]] tail' [[]] = [[]] tail' (x:xs) = case xs of [[]] -> [x] (y:ys) -> tail' xs -- and my append' now becomes: 18) append' :: [[a]] -> a -> [[a]] 19) append' [] y = [[y]] 20) append' (x:xs) y = [(init' (x:xs))] ++ [(tail' xs)++[y]] -- only to result in another mess: p3a.hs:20: Cannot unify the type-signature variable `a' with the type `[a1]' Expected type: [a] Inferred type: [[a1]] In the application `init' (x : xs)' In the list element: (init' (x : xs)) -- in fact, with append' commented out, neither my init' nor -- tail' works (but compiles) with the following run time error: Fail: p3a.hs:2: Non-exhaustive patterns in function concat' -- I apologise for the lengthy mail, but would really appreciate any -- help to achieve such a _simple_ function: append' [ [1, 2], [3, 4], [5] ] 6 -> [ [1, 2], [3, 4], [5, 6] ] append' [ ['1', '2'], ['3'] ] '4' -> [ ['1', '2'], ['3', '4'] ] append' [ [True], [True] ] False -> [ [True], [True, False] ] -- Regards, Jerry

To make it short: Main> let append' xs y = init xs ++ [last xs ++ [y]] This works for the three given examples but maybe incorrect for the task you have in mind, e.g., if xs is empty. Main> append' [ [1, 2], [3, 4], [5] ] 6 [[1,2],[3,4],[5,6]] Main> append' [ ['1', '2'], ['3'] ] '4' ["12","34"] Main> append' [ [True], [True] ] False [[True],[True,False]] Cheers -- Christoph
participants (2)
-
Ch. A. Herrmann
-
Jerry, JiJie