Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: another type problem
(Sumit Sahrawat, Maths & Computing, IIT (BHU))
----------------------------------------------------------------------
Message: 1
Date: Wed, 13 May 2015 17:02:54 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
<[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] another type problem
Message-ID:
<CAJbEW8Ngpb+MxVCp52TU8GqxHO5QkJFDEj=Ky5grFziTX1=m...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Init returns all but the last element of a list. The idea behind the
recursive solution is to keep accepting elements till there is only one
left, and replace it with the empty list.
For example,
init [1, 2, 3]
== init (1 : 2 : 3 : []) { desugar list syntax }
== 1 : init (2 : 3 : []) { apply init }
== 1 : 2 : init (3 : []) { apply init }
== 1 : 2 : [] { init [x] = [] }
On 13 May 2015 at 15:32, akash g <[email protected]> wrote:
> Perhaps a good place to start with functional programming is SICP :
> http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-4.html#%_toc_start
>
> While it doesn't teach you via the Haskell language, it teaches you how to
> construct functional programs and how the compiler/interpreter views it.
> Also, it teaches higher order programming quite well, At least, it is much
> better than every intro haskell book I've read.
>
> On Wed, May 13, 2015 at 3:23 PM, akash g <[email protected]> wrote:
>
>> *wrap, not rap. In my previous mail.
>>
>> 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.
>>
>>
>> https://hackage.haskell.org/package/listsafe-0.1.0.0/docs/Data-List-Safe.html
>>
>>
>> On Wed, May 13, 2015 at 3:19 PM, akash g <[email protected]> wrote:
>>
>>> 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.
>>>
>>> Also, my version doesn't do what you want. One way to do that would be
>>> what you have. Or something like this.
>>>
>>> The following turns the partial function in the Prelude to one that is
>>> safe
>>>
>>> ==========
>>> import Data.List
>>>
>>> init' :: [a] -> Maybe [a]
>>> init' [] = Nothing -- Base case
>>> init' xs = Just $ tail xs -- Inductive case
>>> ==========
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Wed, May 13, 2015 at 2:47 PM, Roelof Wobben <[email protected]> wrote:
>>>
>>>> Hello,
>>>>
>>>> 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.
>>>> At first look your function procudes the whole list again.
>>>>
>>>> Roelof
>>>>
>>>>
>>>>
>>>> akash g schreef op 13-5-2015 om 10:57:
>>>>
>>>> Why not just?
>>>>
>>>> ==================
>>>> init' [] = Nothing
>>>> init' xs = Just xs
>>>> ==================
>>>>
>>>> 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.
>>>>
>>>> Perhaps it'd help us help you if you'd be a bit more clear on what you
>>>> want to achieve.
>>>>
>>>> On Tue, May 12, 2015 at 9:01 PM, Sumit Sahrawat, Maths & Computing, IIT
>>>> (BHU) <[email protected]> wrote:
>>>>
>>>>> On 12 May 2015 at 20:14, Roelof Wobben <[email protected]> wrote:
>>>>>
>>>>>> Thanks,
>>>>>>
>>>>>> The right solution was this :
>>>>>>
>>>>>> init' (x:xs) = Just (x:fromMaybe xs (init' xs))
>>>>>>
>>>>>> if I understand it right x:fromMaybe xs takes care that from xs the
>>>>>> just or Nothing is removed ?
>>>>>>
>>>>>>
>>>>> Trying it out in ghci,
>>>>>
>>>>> fromMaybe [1,2] Nothing == [1,2]
>>>>> fromMaybe [1,2] Just [3] == [3]
>>>>>
>>>>> It seems like that should indeed work.
>>>>>
>>>>> Roelof
>>>>>>
>>>>>>
>>>>>>
>>>>>> Alexey Shmalko schreef op 12-5-2015 om 16:33:
>>>>>>
>>>>>> Try fromMaybe [1]. Examples
>>>>>>
>>>>>> Prelude Data.Maybe> fromMaybe [] (Just [1,2,3])
>>>>>> [1,2,3]
>>>>>> Prelude Data.Maybe> fromMaybe [1,2,3] Nothing
>>>>>> [1,2,3]
>>>>>>
>>>>>> [1]:
>>>>>> https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Maybe.html#v:fromMaybe
>>>>>>
>>>>>> On Tue, May 12, 2015 at 5:28 PM Roelof Wobben <[email protected]>
>>>>>> wrote:
>>>>>>
>>>>>>> Oke,
>>>>>>>
>>>>>>> And how do I do this. Haskell is a difficult one to learn,
>>>>>>>
>>>>>>> Roelof
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Alexey Shmalko schreef op 12-5-2015 om 16:24:
>>>>>>>
>>>>>>> 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.
>>>>>>>
>>>>>>> On Tue, May 12, 2015 at 5:22 PM Roelof Wobben <[email protected]>
>>>>>>> wrote:
>>>>>>>
>>>>>>>> Brandon Allbery schreef op 12-5-2015 om 16:16:
>>>>>>>>
>>>>>>>> On Tue, May 12, 2015 at 10:11 AM, Roelof Wobben <[email protected]>
>>>>>>>> wrote:
>>>>>>>>
>>>>>>>>> I do not understand what you are saying to me.
>>>>>>>>>
>>>>>>>>> I know that init produces a Maybe [a] . That is why I did put a
>>>>>>>>> Just before it.
>>>>>>>>>
>>>>>>>>
>>>>>>>> You are invoking it again though, and using its result as if it
>>>>>>>> produces [a] instead of Maybe [a].
>>>>>>>>
>>>>>>>> --
>>>>>>>> brandon s allbery kf8nh sine nomine
>>>>>>>> associates
>>>>>>>> [email protected]
>>>>>>>> [email protected]
>>>>>>>> unix, openafs, kerberos, infrastructure, xmonad
>>>>>>>> http://sinenomine.net
>>>>>>>>
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> Beginners mailing
>>>>>>>> [email protected]http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>>>>>
>>>>>>>>
>>>>>>>> What is then the right way to do. I still do not understand what
>>>>>>>> you are trying to make clear to me.
>>>>>>>>
>>>>>>>>
>>>>>>>> Roelof
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> ------------------------------
>>>>>>>> [image: Avast logo] <http://www.avast.com/>
>>>>>>>>
>>>>>>>> Dit e-mailbericht is gecontroleerd op virussen met Avast
>>>>>>>> antivirussoftware.
>>>>>>>> www.avast.com
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> Beginners mailing list
>>>>>>>> [email protected]
>>>>>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> Beginners mailing
>>>>>>> [email protected]http://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
>>>>>>> [email protected]
>>>>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>>>>
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> Beginners mailing
>>>>>> [email protected]http://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
>>>>>> [email protected]
>>>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Regards
>>>>>
>>>>> Sumit Sahrawat
>>>>>
>>>>> _______________________________________________
>>>>> Beginners mailing list
>>>>> [email protected]
>>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>>
>>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Beginners mailing
>>>> [email protected]http://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
>>>> [email protected]
>>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>>
>>>>
>>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150513/94a58c4a/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 83, Issue 33
*****************************************