<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Thanks, <br>
<br>
If I re-implement it like this : <br>
<br>
plusplus :: [a] -> [a] -> [a]<br>
plusplus [] (xs) = xs <br>
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 class="ide-error-span">src/Main.hs@3:26-3:40 </div>
<div class="ide-error-msg"><span>Couldn't match expected type
‘([a0] -> [a0] -> [a0]) -> [a] -> [a]’ with actual
type </span>
<div class="CodeMirror cm-s-default" style="font-size: 14px;">[<span
class="cm-variable">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 class="CodeMirror cm-s-default" style="font-size: 14px;"><span
class="cm-variable">xs</span></div>
<span> is applied to two arguments,
but its type </span>
<div class="CodeMirror cm-s-default" style="font-size: 14px;">[<span
class="cm-variable">a</span>]</div>
<span> has none</span><span title="Click to show/hide extra
information" class="ide-error-collapse-btn"> …<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>
<blockquote
cite="mid:CAP=piQcz0h=NoOnTEf_CQeUsurytojCSj7LE7R4Hd=CbVh94vw@mail.gmail.com"
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 moz-do-not-send="true"
href="http://hackage.haskell.org/package/base-4.8.0.0/docs/src/GHC-Base.html#%2B%2B">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 moz-do-not-send="true"
href="https://www.haskell.org/onlinereport/standard-prelude.html">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 moz-do-not-send="true"
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 moz-do-not-send="true" href="http://www.avast.com"
target="_blank">http://www.avast.com</a><br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a moz-do-not-send="true"
href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a moz-do-not-send="true"
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 class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
Beginners mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Beginners@haskell.org">Beginners@haskell.org</a>
<a class="moz-txt-link-freetext" href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a>
</pre>
</blockquote>
<br>
<br /><br />
<hr style='border:none; color:#909090; background-color:#B0B0B0; height: 1px; width: 99%;' />
<table style='border-collapse:collapse;border:none;'>
<tr>
<td style='border:none;padding:0px 15px 0px 8px'>
<a href="http://www.avast.com/">
<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/">www.avast.com</a>
</p>
</td>
</tr>
</table>
<br />
</body>
</html>