<div dir="ltr">The evaluation would go the way Frerich shows it. In particular you should know that these two notations are equivalent.<div><br></div><div><font face="monospace, monospace">[1,2,3] == 1 : 2 : 3 : []</font></div><div><br></div><div>The cons (:) operator (with type a -> [a] -> [a]) adds an element to the start of a list. Also, as it is right associative</div><div><br></div><div><font face="monospace, monospace"> 1 : 2 : 3 : []</font></div><div><font face="monospace, monospace">== 1 : (2 : (3 : [])) { right associativity }</font></div><div><font face="monospace, monospace">== 1 : (2 : [3]) { add element to empty list }</font></div><div><font face="monospace, monospace">== 1 : [2, 3]</font><span style="font-family:monospace,monospace"> </span><span style="font-family:monospace,monospace"> { add element }</span></div><div><font face="monospace, monospace">== [1, 2, 3]</font><span style="font-family:monospace,monospace"> </span><span style="font-family:monospace,monospace"> { add element }</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 13 May 2015 at 12:50, Roelof Wobben <span dir="ltr"><<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>Hello, <br>
<br>
I see it. <br>
<br>
plusplus [1,2] [3,4] <br>
plusplus [1] : plusplus [2] [3,4] <br>
plusplus [1] : [2] : plusplus [] [3,4] <br>
plusplus [1] : [2] : [3,4] <br>
[1:2:3:4] <br>
<br>
I think I need some more practice on this. <br>
Does anyone know a good exercise ?<br>
<br>
Roelof<br>
<br>
<br>
Daniel P. Wright schreef op 13-5-2015 om 9:08:<br>
</div><div><div class="h5">
<blockquote type="cite">
<div dir="ltr">Think about what the possible values of "xs" might
be, and trace through the next call to "plusplus xs ys".
<div><br>
</div>
<div>It would help if I didn't name the variables stupidly...
here is a slightly better version:</div>
<div><br>
</div>
<div>plusplus [] ys = ys</div>
<div>plusplus (x:xs) ys = x : plusplus xs ys</div>
<div><br>
</div>
<div>If it helps, try tracing through the steps required to
evaluate "plusplus [1, 2] [3, 4]" manually (using a pen and
paper, not on the computer)</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">2015-05-13 15:55 GMT+09:00 Roelof
Wobben <span dir="ltr"><<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>Thanks, <br>
<br>
So the answer is x and the rest of xs and ys. <br>
How do x then get added to ys. <br>
<br>
Roelof<br>
<br>
Daniel P. Wright schreef op 13-5-2015 om 8:52:<br>
</div>
<div>
<div>
<blockquote type="cite">
<div dir="ltr">Ah, that's just a small syntactic
issue -- in Haskell, operators are infix (go
between the arguments) by default, but named
functions are not. So you would have to write it:
<div><br>
</div>
<div>plusplus [] xs = xs</div>
<div>plusplus (x:xs) ys = x : plusplus xs ys</div>
<div><br>
</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">2015-05-13 15:39
GMT+09:00 Roelof Wobben <span dir="ltr"><<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>Thanks, <br>
<br>
If I re-implement it like this : <br>
<span> <br>
plusplus :: [a] -> [a] -> [a]<br>
plusplus [] (xs) = xs <br>
</span> plusplus (x:xs) yx = x : xs
plusplus yx<br>
<br>
main = print $ ["a","b"] plusplus
["c","d"]<br>
<br>
<br>
I see this error appear : <br>
<br>
<div>src/Main.hs@3:26-3:40 </div>
<div><span>Couldn't match expected type
‘([a0] -> [a0] -> [a0]) ->
[a] -> [a]’ with actual type </span>
<div style="font-size:14px">[<span>a</span>]</div>
<span> Relevant bindings include yx ::
[a] (bound at
/home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:17)
xs :: [a] (bound at
/home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:13)
x :: a (bound at
/home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:3:11)
plusplus :: [a] -> [a] -> [a]
(bound at
/home/app/isolation-runner-work/projects/112825/session.207/src/src/Main.hs:2:1)
The
function </span>
<div style="font-size:14px"><span>xs</span></div>
<span> is applied to two arguments, but
its type </span>
<div style="font-size:14px">[<span>a</span>]</div>
<span> has none</span><span title="Click
to show/hide extra information"> …<br>
<br>
So for me not a aha moment. I was
hoping I would get it <br>
<br>
Roelof</span><br>
</div>
<br>
Daniel P. Wright schreef op 13-5-2015 om
8:27:<br>
</div>
<div>
<div>
<blockquote type="cite">
<div dir="ltr">Hi Roelof,
<div><br>
</div>
<div>If you don't consider it
cheating (and I suggest you
shouldn't, having had a stab at
the answers), you will find great
enlightenment looking at how these
functions are *actually*
implemented in the wild. Did you
know that there is a "source" link
for each function on Hackage? By
clicking on that, for your first
example, you can compare the
actual implementation of (++) with
your "plusplus" function:</div>
<div><br>
</div>
<div><a href="http://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-Base.html#%2B%2B" target="_blank">http://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-Base.html#%2B%2B</a><br>
</div>
<div><br>
</div>
<div>(By the way, it's that function
in particular that gave me one of
my first "Aha!" moments in
Haskell... it's quite beautiful in
its simplicity).</div>
<div><br>
</div>
<div>One thing with viewing the
source on Hackage is that
sometimes it can be a little more
confusing than it needs to be for
the sake of efficiency. A really
good source for good, readable
examples of Prelude functions in
Haskell is the Haskell Report:</div>
<div><br>
</div>
<div><a href="https://www.haskell.org/onlinereport/standard-prelude.html" target="_blank">https://www.haskell.org/onlinereport/standard-prelude.html</a></div>
<div><br>
</div>
<div>(In this case, though, the
implementation is the same).</div>
<div><br>
</div>
<div>Having a shot at defining these
library functions yourself, as you
have done, and then comparing your
version with the "official"
version in the prelude is a great
way to learn good style!</div>
<div><br>
</div>
<div>-Dani. </div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">2015-05-13
15:10 GMT+09:00 Roelof Wobben <span dir="ltr"><<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
For practising pattern matching
and recursion I did recreate
some commands of Data,list.<br>
<br>
My re-implementation of ++ :<br>
<br>
plusplus :: [a] -> [a] ->
[a]<br>
plusplus [] [] = [] ;<br>
plusplus [] (xs) = xs<br>
plusplus (xs) [] = xs<br>
plusplus (xs) yx = plusplus'
(reverse xs) yx<br>
<br>
plusplus' :: [a] -> [a] ->
[a]<br>
plusplus' [] (xs) = xs<br>
plusplus' (x:xs) yx = plusplus'
xs (x:yx)<br>
<br>
main = print $ plusplus
["a","b"] ["c","d"]<br>
<br>
my re-implementation of init :<br>
<br>
import Data.Maybe<br>
<br>
-- | The main entry point.<br>
init' :: [a] -> Maybe [a]<br>
init' [] = Nothing<br>
init' [x] = Just []<br>
init' (x:xs) = Just (x:fromMaybe
xs (init' xs))<br>
<br>
main = print . init' $ [1,3]<br>
<br>
<br>
my re-implementation of last :<br>
<br>
-- | The main entry point.<br>
last' :: [a] -> Maybe a<br>
last' [] = Nothing<br>
last' [x] = Just x<br>
last' (_:xs) = last' xs<br>
<br>
main = print . last' $ []<br>
<br>
Now I wonder if these solutions
are the haskell way ? if not so,
how can I improve them ,<br>
<br>
Roelof<br>
<br>
<br>
---<br>
Dit e-mailbericht is
gecontroleerd op virussen met
Avast antivirussoftware.<br>
<a href="http://www.avast.com" target="_blank">http://www.avast.com</a><br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote>
</div>
<br>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
Beginners mailing list
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
</blockquote>
<br>
<br>
<br>
</div>
</div>
<hr style="border:none;color:#909090;background-color:#b0b0b0;min-height:1px;width:99%">
<table style="border-collapse:collapse;border:none">
<tbody>
<tr>
<td style="border:none;padding:0px 15px 0px 8px"> <a href="http://www.avast.com/" target="_blank"> <img src="http://static.avast.com/emails/avast-mail-stamp.png" alt="Avast logo" border="0"> </a>
</td>
<td>
<p style="color:#3d4d5a;font-family:"Calibri","Verdana","Arial","Helvetica";font-size:12pt"><span>
Dit e-mailbericht is
gecontroleerd op virussen met
Avast antivirussoftware. <br>
</span><a href="http://www.avast.com/" target="_blank">www.avast.com</a>
</p>
</td>
</tr>
</tbody>
</table>
<br>
</div>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br>
</blockquote>
</div>
<br>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
Beginners mailing list
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
</blockquote>
<br>
<br>
<br>
<hr style="border:none;color:#909090;background-color:#b0b0b0;min-height:1px;width:99%">
<table style="border-collapse:collapse;border:none">
<tbody>
<tr>
<td style="border:none;padding:0px 15px 0px 8px">
<a href="http://www.avast.com/" target="_blank">
<img src="http://static.avast.com/emails/avast-mail-stamp.png" alt="Avast logo" border="0"> </a> </td>
<td>
<p style="color:#3d4d5a;font-family:"Calibri","Verdana","Arial","Helvetica";font-size:12pt">
Dit e-mailbericht is gecontroleerd op
virussen met Avast antivirussoftware. <br>
<a href="http://www.avast.com/" target="_blank">www.avast.com</a> </p>
</td>
</tr>
</tbody>
</table>
<br>
</div>
</div>
</div>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br>
</blockquote>
</div>
<br>
</div>
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
Beginners mailing list
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
</blockquote>
<br>
<br><br>
<hr style="border:none;color:#909090;background-color:#b0b0b0;min-height:1px;width:99%">
<table style="border-collapse:collapse;border:none">
<tbody><tr>
<td style="border:none;padding:0px 15px 0px 8px">
<a href="http://www.avast.com/" target="_blank">
<img border="0" src="http://static.avast.com/emails/avast-mail-stamp.png" alt="Avast logo">
</a>
</td>
<td>
<p style="color:#3d4d5a;font-family:"Calibri","Verdana","Arial","Helvetica";font-size:12pt">
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
<br><a href="http://www.avast.com/" target="_blank">www.avast.com</a>
</p>
</td>
</tr>
</tbody></table>
<br>
</div></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div>
</div>