[Haskell-beginners] how can I make this work

Jonathan Skårstedt jonathan.skarstedt at gmail.com
Tue May 12 18:58:41 UTC 2015


Hello!

I will call plusplus' by pp' in the text below

1. Your pp' gives a warning, namely that the patterns
pp' [] xs = xs
pp' [] []  = []
are overlapping. This is because of pattern matching working top-down ie if
I give the input *pp' [] [] *it will match both your function entry points
at *pp' [] xs* and *pp' [] []*. We don't really need *pp' [] []* as *pp' []
ys = ys *will give the correct answer even if ys is an empty list.

2. In your last function body you have this expression
pp' (x:xs) ys = pp' xs (x:ys)

this will not do what you expect, consider the flow below.

I will call pp' on two strings "ABC" and "DEF"

Lets extend this function
pp' (A:BC) DEF = pp' BC (A:DEF)
pp' (B:C) ADEF = pp' C (B:ADEF)
pp' C:[] BADEF = pp' [] (C:BADEF)
pp' [] CBADEF  = CBADEF -- <- base case

As you see, it will pick the first of xs to put first on ys but is this
what we want? For one recursion, yes but after that it gets a bit weird.
The second recursion will flip the order of the resulting list by adding
the first object at the start first, instead of last.

We actually don't want our construction (the colon :) to work from inside
the recursive call (pp' xs (x:ys)), we want to move it outside, like this,
and just like we did in init: pp' (x:xs) ys = *x: *pp' xs ys


2015-05-12 20:33 GMT+02:00 Roelof Wobben <r.wobben at home.nl>:

>  pfff, I got grazy here
>
> On this function it works fine :
>
> -- | The main entry point.
> init' :: [a] -> Maybe [a]
> init' [] = Nothing
> init' [x] = Just []
> init' (x:xs) = Just (x:fromMaybe xs (init' xs))
>
> main = print . init' $ [1,3]
>
>
> and on this one it does not work :
>
> plusplus' :: [a] -> [a] -> [a]
> plusplus' [] (xs) = xs
> plusplus' [] [] = []
> plusplus' (xs) [] =  xs
> plusplus' (x:xs) yx = plusplus' xs (x:yx)
>
>
> main = print . plusplus' $  [1,2] [3, 4]
>
> Roelof
>
>
>
>
> Alex Hammel schreef op 12-5-2015 om 20:19:
>
>  The relevant part of that error message is this:
>
> > The function ‘[1, 2]’ is applied to one argument, but its type
> > [t0]
>  > has none
>
>  Which means that you're trying to call a list literal as though it were a
> function (with one argument). Which probably means that you've got a ($)
> out of place.
>
>
> On Tue, 12 May 2015 at 11:16 Roelof Wobben <r.wobben at home.nl> wrote:
>
>>  Thanks,
>>
>> Now I see the last error on the main line :
>>
>> src/Main.hs at 7:28-7:39
>> Couldn't match expected type ‘[t1] -> [a0]’ with actual type
>> [t0]
>>  The function ‘[1, 2]’ is applied to one argument, but its type
>> [t0]
>>  has none … In the second argument of ‘($)’, namely ‘[1, 2] [3, 4]’ In
>> the expression: print . plusplus' $ [1, 2] [3, 4]
>>
>> Roelof
>>
>>
>>
>>
>> Alex Hammel schreef op 12-5-2015 om 20:11:
>>
>>  You're missing the parens around (x:yx) in the last clause.
>>
>> On Tue, 12 May 2015 at 11:02 Roelof Wobben <r.wobben at home.nl> wrote:
>>
>>>  Hello,
>>>
>>> I try to re implement ++
>>>
>>> So far I have this ;
>>>
>>> plusplus' :: [a] -> [a] -> [a]
>>> plusplus' [] (xs) = xs
>>> plusplus' [] [] = []
>>> plusplus' (xs) [] =  xs
>>> plusplus' (x:xs) yx = plusplus' xs x:yx
>>>
>>> main = print . plusplus' $ [1,2] [3,4]
>>>
>>>
>>> But I still get this error message :
>>>
>>> src/Main.hs at 5:23-5:37
>>> Couldn't match expected type
>>> a
>>>  with actual type
>>> [a]
>>>  a
>>>  is a rigid type variable bound by the type signature for plusplus' ::
>>> [a] -> [a] -> [a] at
>>> /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:1:14
>>> Relevant bindings include yx :: [a] (bound at
>>> /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:5:18)
>>> xs :: [a] (bound at
>>> /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:5:14)
>>> x :: a (bound at
>>> /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:5:12)
>>> plusplus' :: [a] -> [a] -> [a] (bound at
>>> /home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:2:1)
>>>>>>
>>>
>>>
>>>
>>>
>>> ------------------------------
>>>    [image: Avast logo] <http://www.avast.com/>
>>>
>>> Dit e-mailbericht is gecontroleerd op virussen met Avast
>>> antivirussoftware.
>>> www.avast.com
>>>
>>>  _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>>
>>
>> _______________________________________________
>> Beginners mailing listBeginners at haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>>
>>
>> ------------------------------
>>    [image: Avast logo] <http://www.avast.com/>
>>
>> Dit e-mailbericht is gecontroleerd op virussen met Avast
>> antivirussoftware.
>> www.avast.com
>>
>>  _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing listBeginners at haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>
>
> ------------------------------
>   [image: Avast logo] <http://www.avast.com/>
>
> Dit e-mailbericht is gecontroleerd op virussen met Avast
> antivirussoftware.
> www.avast.com
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>


-- 
Jonathan Skårstedt
Bergsgårdsgärdet 39
Lgh 1108
424 32 Göteborg
Mobil: 073 - 76 20 20 7
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150512/4319fcf2/attachment-0001.html>


More information about the Beginners mailing list