Simple Question Follow Up

Jerry, JiJie jerry@gime.com
Thu, 16 May 2002 23:30:10 +0800


* Ch. A. Herrmann <herrmann@fmi.uni-passau.de> [020516 21:38]:
> 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