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.  OrdList implementation (Mark Carter)
   2. Re:  question about list processing (Martin Vlk)
   3. Re:  question about list processing (Martin Vlk)
   4. Re:  question about list processing (Dennis Raddle)
   5. Re:  question about list processing (akash g)
   6. Re:  question about list processing (akash g)
   7. Re:  question about list processing (Dennis Raddle)


----------------------------------------------------------------------

Message: 1
Date: Thu, 12 Nov 2015 12:41:18 +0000
From: Mark Carter <[email protected]>
To: [email protected], Mark Carter <[email protected]>
Subject: [Haskell-beginners] OrdList implementation
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

I am trying to implement "OrdList", which is a list that is guaranteed 
to be ordered. Here's my implementation so far:

module OrdList (empty, fromList, insert, toList, OrdList(OrdList)) where
import Data.List as L (span)

data OrdList a = OrdList [a] deriving (Show)


empty :: OrdList a
empty = OrdList []


insert :: (Ord a) => a -> OrdList a -> OrdList a
insert x ol =
   let -- OrdList xs = ol
       (before, after) = L.span (\el -> el <= x) $ toList xs
   in
     OrdList $  before ++ [x] ++ after

fromList :: Ord a => [a] -> OrdList a
fromList xs =
   if null xs then empty else insert (head xs) (fromList $ tail xs)

toList :: OrdList t -> [t]
toList xs =
   let OrdList xs' = xs in xs'



It "works", but no doubt there's plenty to dislike about it. Some 
specific questions I have in mind:
1. Should OrdList be best implemented as a class, data, or newtype? If 
so, what changes need to be made?
2. How comes I can't refer to OrdList as a data type? This is a problem 
because I can't say that I want the type of something to be OrdList Int, 
for example.




------------------------------

Message: 2
Date: Thu, 12 Nov 2015 13:22:08 +0000
From: Martin Vlk <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] question about list processing
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252

How about:

computeHead f = take 1 . fmap f

Martin

Dennis Raddle:
> What would be an elegant way of writing this
> 
> computeHead :: (a -> [b]) -> [a] -> [b]
> 
> Where when [a] is null, it returns a null list, but when [a] contains one
> or more elements, it applies the given function to the head of a and
> returns that? Is there some existing typeclass operator that facilitates
> this?
> 
> You can write
> 
> computeHead _ [] = []
> computeHead f (x:_) = f x
> 
> But that first line seems suspicious to me... it makes me think about how
> in the list Monad, an empty list falls through. But I can't quite make it
> work.
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> 


------------------------------

Message: 3
Date: Thu, 12 Nov 2015 13:26:03 +0000
From: Martin Vlk <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] question about list processing
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252

Oh, and a little background.

fmap comes from the Functor typeclass, and lists have an instance of
Functor so the empty list case is handled automatically.

Then I used "take 1" to get the head in a safe way, e.g. defaulting to
empty list if the output of fmap is empty.

M.

Martin Vlk:
> How about:
> 
> computeHead f = take 1 . fmap f
> 
> Martin
> 
> Dennis Raddle:
>> What would be an elegant way of writing this
>>
>> computeHead :: (a -> [b]) -> [a] -> [b]
>>
>> Where when [a] is null, it returns a null list, but when [a] contains one
>> or more elements, it applies the given function to the head of a and
>> returns that? Is there some existing typeclass operator that facilitates
>> this?
>>
>> You can write
>>
>> computeHead _ [] = []
>> computeHead f (x:_) = f x
>>
>> But that first line seems suspicious to me... it makes me think about how
>> in the list Monad, an empty list falls through. But I can't quite make it
>> work.
>>
>>
>>
>> _______________________________________________
>> 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
> 
> 


------------------------------

Message: 4
Date: Thu, 12 Nov 2015 05:33:22 -0800
From: Dennis Raddle <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] question about list processing
Message-ID:
        <CAKxLvorRRvRxQRXFXRdaapX=-yfujx-jqaz7chfrued+bbc...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Just after I posted that question, I started driving home, and on the drive
I thought of your answer. I think I'm starting to ask the right questions
when I'm programming in Haskell. Like redundancy and bloat is a sure sign
that a more witty expression is available, and that I should consult the
typeclasses.

Second, I am not used to the implications of laziness, so it took me a
while to hit on your solution because I keep thinking you have to map
something over the whole list, and that if you only want to map it over the
head, you are stuck.

You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap' for
lists?

D
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151112/265b37f7/attachment-0001.html>

------------------------------

Message: 5
Date: Thu, 12 Nov 2015 19:11:11 +0530
From: akash g <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] question about list processing
Message-ID:
        <CALiga_c+K9=by-v248m+7_oy22r0mdpctxbi+8lk_ns94-l...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

map is specialized for lists while fmap is for any functors.  Its presence
is historical.  Prefer fmap over map.

On Thu, Nov 12, 2015 at 7:03 PM, Dennis Raddle <[email protected]>
wrote:

> Just after I posted that question, I started driving home, and on the
> drive I thought of your answer. I think I'm starting to ask the right
> questions when I'm programming in Haskell. Like redundancy and bloat is a
> sure sign that a more witty expression is available, and that I should
> consult the typeclasses.
>
> Second, I am not used to the implications of laziness, so it took me a
> while to hit on your solution because I keep thinking you have to map
> something over the whole list, and that if you only want to map it over the
> head, you are stuck.
>
> You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap' for
> lists?
>
> D
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151112/820210d9/attachment-0001.html>

------------------------------

Message: 6
Date: Thu, 12 Nov 2015 19:12:27 +0530
From: akash g <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] question about list processing
Message-ID:
        <caliga_e27drb3jtegfnajdt8gzeplc1vckcsw0otdugqcxv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

http://stackoverflow.com/questions/7463500/why-do-we-have-map-fmap-and-liftm
have very good answers on this.

On Thu, Nov 12, 2015 at 7:11 PM, akash g <[email protected]> wrote:

> map is specialized for lists while fmap is for any functors.  Its presence
> is historical.  Prefer fmap over map.
>
> On Thu, Nov 12, 2015 at 7:03 PM, Dennis Raddle <[email protected]>
> wrote:
>
>> Just after I posted that question, I started driving home, and on the
>> drive I thought of your answer. I think I'm starting to ask the right
>> questions when I'm programming in Haskell. Like redundancy and bloat is a
>> sure sign that a more witty expression is available, and that I should
>> consult the typeclasses.
>>
>> Second, I am not used to the implications of laziness, so it took me a
>> while to hit on your solution because I keep thinking you have to map
>> something over the whole list, and that if you only want to map it over the
>> head, you are stuck.
>>
>> You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap'
>> for lists?
>>
>> D
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151112/f3c3d753/attachment-0001.html>

------------------------------

Message: 7
Date: Thu, 12 Nov 2015 05:48:46 -0800
From: Dennis Raddle <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] question about list processing
Message-ID:
        <cakxlvoo4vydzjvsfkdpj+wyhhx06lfs_bel4m9o8wlvqadn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Speaking of lists and history, I noticed that a lot of library functions
which were formerly defined over lists (when I first looked at Haskell six
years ago) are now defined on Traversable, which makes it a lot harder for
beginners to read the documentation. I have been playing with Haskell for
five years, but not much, so I'm still a beginner. I just mentally
substitute lists when I see Traversable.

I only really use lists and Maybe, as far as instances of the typeclasses
go. That's only two types, but a lot to learn!

D




On Thu, Nov 12, 2015 at 5:42 AM, akash g <[email protected]> wrote:

>
> http://stackoverflow.com/questions/7463500/why-do-we-have-map-fmap-and-liftm
> have very good answers on this.
>
> On Thu, Nov 12, 2015 at 7:11 PM, akash g <[email protected]> wrote:
>
>> map is specialized for lists while fmap is for any functors.  Its
>> presence is historical.  Prefer fmap over map.
>>
>> On Thu, Nov 12, 2015 at 7:03 PM, Dennis Raddle <[email protected]>
>> wrote:
>>
>>> Just after I posted that question, I started driving home, and on the
>>> drive I thought of your answer. I think I'm starting to ask the right
>>> questions when I'm programming in Haskell. Like redundancy and bloat is a
>>> sure sign that a more witty expression is available, and that I should
>>> consult the typeclasses.
>>>
>>> Second, I am not used to the implications of laziness, so it took me a
>>> while to hit on your solution because I keep thinking you have to map
>>> something over the whole list, and that if you only want to map it over the
>>> head, you are stuck.
>>>
>>> You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap'
>>> for lists?
>>>
>>> D
>>>
>>>
>>> _______________________________________________
>>> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20151112/3030a5da/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 89, Issue 18
*****************************************

Reply via email to