<div dir="ltr"><div><div>*wrap, not rap. In my previous mail.<br><br></div>Also, there is already a library that does list operations in a safe way, though they have used monads. Give a look at the source code and see if it helps.<br><br><a href="https://hackage.haskell.org/package/listsafe-0.1.0.0/docs/Data-List-Safe.html">https://hackage.haskell.org/package/listsafe-0.1.0.0/docs/Data-List-Safe.html</a><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 13, 2015 at 3:19 PM, akash g <span dir="ltr"><<a href="mailto:akaberto@gmail.com" target="_blank">akaberto@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Ah, but it doesn't. Since this is a functional language, we have a pointer to it. In fact, all it does is rap it around in the Maybe type.<br><br></div>Also, my version doesn't do what you want. One way to do that would be what you have. Or something like this.<br><br></div>The following turns the partial function in the Prelude to one that is safe<br><br>==========<br>import Data.List<span class=""><br><br>init' :: [a] -> Maybe [a]<br></span>init' [] = Nothing -- Base case<br>init' xs = Just $ tail xs -- Inductive case <br>==========<br><br><br><div><div><div><br><br><br><br></div></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 13, 2015 at 2:47 PM, 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>
What my intention was to make a safe version of the init function
found in Data.list so it procudes a list without the last item. <br>
At first look your function procudes the whole list again.<br>
<br>
Roelof<br>
<br>
<br>
<br>
akash g schreef op 13-5-2015 om 10:57:<br>
</div><div><div>
<blockquote type="cite">
<div dir="ltr">
<div>
<div>
<div>
<div>Why not just?<br>
<br>
==================<br>
</div>
init' [] = Nothing<br>
</div>
init' xs = Just xs<br>
==================<br>
<br>
</div>
<div>Meets your type sig and is also has a time complexity of
O(1) instead of O(n) which will be the time complexity in
the solution involving fromMaybe. Maybe I'm missing
something.<br>
</div>
<div><br>
</div>
Perhaps it'd help us help you if you'd be a bit more clear on
what you want to achieve. </div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Tue, May 12, 2015 at 9:01 PM, Sumit
Sahrawat, Maths & Computing, IIT (BHU) <span dir="ltr"><<a href="mailto:sumit.sahrawat.apm13@iitbhu.ac.in" target="_blank">sumit.sahrawat.apm13@iitbhu.ac.in</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote"><span>On 12 May 2015
at 20:14, 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>Thanks, <br>
<br>
The right solution was this : <br>
<br>
init' (x:xs) = Just (x:fromMaybe xs (init'
xs))<br>
<br>
if I understand it right x:fromMaybe xs takes
care that from xs the just or Nothing is
removed ?<br>
<br>
</div>
</div>
</blockquote>
<div><br>
</div>
</span>Trying it out in ghci,
<div><br>
</div>
<div>fromMaybe [1,2] Nothing == [1,2]</div>
<div>fromMaybe [1,2] Just [3] == [3]</div>
<div><br>
</div>
<div>It seems like that should indeed work.</div>
<div>
<div>
<div><br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div> Roelof<br>
<br>
<br>
<br>
Alexey Shmalko schreef op 12-5-2015 om
16:33:<br>
</div>
<div>
<div>
<blockquote type="cite">
<div dir="ltr">Try fromMaybe [1].
Examples<br>
<br>
<div>Prelude Data.Maybe> fromMaybe
[] (Just [1,2,3])</div>
<div>[1,2,3]</div>
<div>Prelude Data.Maybe> fromMaybe
[1,2,3] Nothing</div>
<div>[1,2,3]</div>
<div><br>
</div>
<div>[1]: <a href="https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Maybe.html#v:fromMaybe" target="_blank">https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Maybe.html#v:fromMaybe</a><br>
</div>
</div>
<br>
<div class="gmail_quote">On Tue, May 12,
2015 at 5:28 PM Roelof Wobben <<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>Oke, <br>
<br>
And how do I do this. Haskell
is a difficult one to learn,<br>
<br>
Roelof<br>
<br>
<br>
<br>
Alexey Shmalko schreef op
12-5-2015 om 16:24:<br>
</div>
</div>
<div bgcolor="#FFFFFF" text="#000000">
<blockquote type="cite">
<div dir="ltr">Before cons'ing
the result of init', you
should check whether it's Just
or Nothing. What you're doing
now is something along the
line with 5 : Just 3 -- this
won't typecheck.<br>
</div>
<br>
<div class="gmail_quote">On Tue,
May 12, 2015 at 5:22 PM Roelof
Wobben <<a href="mailto:r.wobben@home.nl" target="_blank">r.wobben@home.nl</a>>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>Brandon Allbery
schreef op 12-5-2015 om
16:16:<br>
</div>
</div>
<div bgcolor="#FFFFFF" text="#000000">
<blockquote type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">On
Tue, May 12, 2015
at 10:11 AM,
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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
<div>I do not
understand
what you are
saying to me.<br>
<br>
I know that
init produces
a Maybe [a] .
That is why I
did put a Just
before it.<br>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>You are
invoking it
again though,
and using its
result as if it
produces [a]
instead of Maybe
[a].</div>
</div>
<div><br>
</div>
-- <br>
<div>
<div dir="ltr">
<div>brandon s
allbery kf8nh
sine nomine
associates</div>
<div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a>
<a href="mailto:ballbery@sinenomine.net" target="_blank">ballbery@sinenomine.net</a></div>
<div>unix,
openafs,
kerberos,
infrastructure,
xmonad <a href="http://sinenomine.net" target="_blank">http://sinenomine.net</a></div>
</div>
</div>
</div>
</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>
</div>
<div bgcolor="#FFFFFF" text="#000000"> What is
then the right way to do.
I still do not understand
what you are trying to
make clear to me.</div>
<div bgcolor="#FFFFFF" text="#000000"><br>
<br>
Roelof</div>
<div bgcolor="#FFFFFF" text="#000000"><br>
<br>
<br>
<br>
<hr style="border:none;color:rgb(144,144,144);min-height:1px;width:99%;background-color:rgb(176,176,176)">
<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:rgb(61,77,90);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>
_______________________________________________<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>
<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:rgb(144,144,144);min-height:1px;width:99%;background-color:rgb(176,176,176)">
<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:rgb(61,77,90);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>
_______________________________________________<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>
<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:rgb(144,144,144);min-height:1px;width:99%;background-color:rgb(176,176,176)">
<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:rgb(61,77,90);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>
</div>
</div>
<span><font color="#888888"><br>
<br clear="all">
<div><br>
</div>
-- <br>
<div>
<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>
</font></span></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 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>
</div></div></blockquote></div><br></div>