Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/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:  Beginners Digest, Vol 74, Issue 5 (Neeraj Rao)
   2. Re:  Beginners Digest, Vol 74, Issue 5 (Neeraj Rao)
   3. Re:  Beginners Digest, Vol 74, Issue 5 (Bob Ippolito)
   4.  Too much mapM, fmap and concat (martin)


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

Message: 1
Date: Wed, 13 Aug 2014 11:21:10 -0400
From: Neeraj Rao <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Beginners Digest, Vol 74, Issue 5
Message-ID:
        <camtpa6bge1bwkbbgaeebxr_6aoa5zwxomtuykta1e71dzeo...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Why not add the Ord constraint to the function signatures in the class
definition?

class Set s where
    empty :: s a
    insert :: Ord a => a -> s a -> s a
    member :: Ord a => a -> s a -> Bool

Seems to me like insert and member do need those constraints, irrespective
of what type 's' is.

Message: 1
> Date: Tue, 12 Aug 2014 21:14:09 -0600
> From: Dimitri DeFigueiredo <[email protected]>
> To: The Haskell-Beginners Mailing List - Discussion of primarily
>         beginner-level topics related to Haskell <[email protected]>
> Subject: [Haskell-beginners] expressing constraints 101
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> Hi All,
> I am working through an exercise in Chris Okasaki's book (#2.2). In the
> book, he is trying to implement a minimal interface for a Set. I wrote
> that simple interface in Haskell as:
> class Set s where
>      empty  :: s a                -- returns an empty set of type Set of a
>      insert :: a -> s a -> s a   -- returns set with new element inserted
>      member :: a -> s a -> Bool  -- True if element is a member of the Set
> To implement that interface with the appropriately O(log n) insert and
> member functions he suggests the use of a Binary Search Tree, which I
> translated to Haskell as:
> data Tree a = Empty | MkTree (Tree a) a (Tree a)
> But for the tree to work, we also need the "a"s to be totally ordered.
> I.e. (Ord a) is a constraint. So, it makes sense to write:
> treeEmpty :: Tree a
> treeEmpty = Empty
> treeInsert :: Ord a => a -> Tree a -> Tree a
> treeInsert = undefined
> treeMember :: Ord a => a -> Tree a -> Bool
> treeMember = undefined
> Now, I would like to bind this implementation using Trees of an ordered
> type "a" to the set type class. So, I would like to write something like:


instance Set Tree where
     empty  = treeEmpty
     insert = treeInsert
     member = treeMember

But that doesn't work. Using GHC 7.6.3, I get a:

     No instance for (Ord a) arising from a use of `treeInsert'
     Possible fix:
       add (Ord a) to the context of
         the type signature for insert :: a -> Tree a -> Tree a
     In the expression: treeInsert a
     In an equation for `insert': insert a = treeInsert a
     In the instance declaration for `Set Tree'

Which makes sense, but I'm not sure how to express this constraint.
So, what is the proper way to do this?
Where have I gone wrong?


Thanks!

Dimitri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140813/6d4fe279/attachment-0001.html>

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

Message: 2
Date: Wed, 13 Aug 2014 11:26:40 -0400
From: Neeraj Rao <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Beginners Digest, Vol 74, Issue 5
Message-ID:
        <CAMtPA6Y9RpyQdxLy4pPo_03CRR7gCxm1WBqyZWwN=umphqa...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Sorry, I just realized this is exactly what Akash suggested.


On Wed, Aug 13, 2014 at 11:21 AM, Neeraj Rao <[email protected]> wrote:

> Why not add the Ord constraint to the function signatures in the class
> definition?
>
> class Set s where
>     empty :: s a
>     insert :: Ord a => a -> s a -> s a
>     member :: Ord a => a -> s a -> Bool
>
> Seems to me like insert and member do need those constraints, irrespective
> of what type 's' is.
>
> Message: 1
>> Date: Tue, 12 Aug 2014 21:14:09 -0600
>> From: Dimitri DeFigueiredo <[email protected]>
>> To: The Haskell-Beginners Mailing List - Discussion of primarily
>>         beginner-level topics related to Haskell <[email protected]>
>> Subject: [Haskell-beginners] expressing constraints 101
>> Message-ID: <[email protected]>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>> Hi All,
>> I am working through an exercise in Chris Okasaki's book (#2.2). In the
>> book, he is trying to implement a minimal interface for a Set. I wrote
>> that simple interface in Haskell as:
>> class Set s where
>>      empty  :: s a                -- returns an empty set of type Set of a
>>      insert :: a -> s a -> s a   -- returns set with new element inserted
>>      member :: a -> s a -> Bool  -- True if element is a member of the Set
>> To implement that interface with the appropriately O(log n) insert and
>> member functions he suggests the use of a Binary Search Tree, which I
>> translated to Haskell as:
>> data Tree a = Empty | MkTree (Tree a) a (Tree a)
>> But for the tree to work, we also need the "a"s to be totally ordered.
>> I.e. (Ord a) is a constraint. So, it makes sense to write:
>> treeEmpty :: Tree a
>> treeEmpty = Empty
>> treeInsert :: Ord a => a -> Tree a -> Tree a
>> treeInsert = undefined
>> treeMember :: Ord a => a -> Tree a -> Bool
>> treeMember = undefined
>> Now, I would like to bind this implementation using Trees of an ordered
>> type "a" to the set type class. So, I would like to write something like:
>
>
> instance Set Tree where
>      empty  = treeEmpty
>      insert = treeInsert
>      member = treeMember
>
> But that doesn't work. Using GHC 7.6.3, I get a:
>
>      No instance for (Ord a) arising from a use of `treeInsert'
>      Possible fix:
>        add (Ord a) to the context of
>          the type signature for insert :: a -> Tree a -> Tree a
>      In the expression: treeInsert a
>      In an equation for `insert': insert a = treeInsert a
>      In the instance declaration for `Set Tree'
>
> Which makes sense, but I'm not sure how to express this constraint.
> So, what is the proper way to do this?
> Where have I gone wrong?
>
>
> Thanks!
>
> Dimitri
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140813/0bdcff8b/attachment-0001.html>

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

Message: 3
Date: Wed, 13 Aug 2014 08:26:41 -0700
From: Bob Ippolito <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Beginners Digest, Vol 74, Issue 5
Message-ID:
        <cacwmpm_npl-m-k18tc8433uoa+knxm7o4ko_hbw+txfsk33...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

The only necessary and common constraint is Eq. Other constraints such as
Ord or Hashable could be used to implement Set much more efficiently, but
they are not strictly necessary.


On Wed, Aug 13, 2014 at 8:21 AM, Neeraj Rao <[email protected]> wrote:

> Why not add the Ord constraint to the function signatures in the class
> definition?
>
> class Set s where
>     empty :: s a
>     insert :: Ord a => a -> s a -> s a
>     member :: Ord a => a -> s a -> Bool
>
> Seems to me like insert and member do need those constraints, irrespective
> of what type 's' is.
>
> Message: 1
>> Date: Tue, 12 Aug 2014 21:14:09 -0600
>> From: Dimitri DeFigueiredo <[email protected]>
>> To: The Haskell-Beginners Mailing List - Discussion of primarily
>>         beginner-level topics related to Haskell <[email protected]>
>> Subject: [Haskell-beginners] expressing constraints 101
>> Message-ID: <[email protected]>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>> Hi All,
>> I am working through an exercise in Chris Okasaki's book (#2.2). In the
>> book, he is trying to implement a minimal interface for a Set. I wrote
>> that simple interface in Haskell as:
>> class Set s where
>>      empty  :: s a                -- returns an empty set of type Set of a
>>      insert :: a -> s a -> s a   -- returns set with new element inserted
>>      member :: a -> s a -> Bool  -- True if element is a member of the Set
>> To implement that interface with the appropriately O(log n) insert and
>> member functions he suggests the use of a Binary Search Tree, which I
>> translated to Haskell as:
>> data Tree a = Empty | MkTree (Tree a) a (Tree a)
>> But for the tree to work, we also need the "a"s to be totally ordered.
>> I.e. (Ord a) is a constraint. So, it makes sense to write:
>> treeEmpty :: Tree a
>> treeEmpty = Empty
>> treeInsert :: Ord a => a -> Tree a -> Tree a
>> treeInsert = undefined
>> treeMember :: Ord a => a -> Tree a -> Bool
>> treeMember = undefined
>> Now, I would like to bind this implementation using Trees of an ordered
>> type "a" to the set type class. So, I would like to write something like:
>
>
> instance Set Tree where
>      empty  = treeEmpty
>      insert = treeInsert
>      member = treeMember
>
> But that doesn't work. Using GHC 7.6.3, I get a:
>
>      No instance for (Ord a) arising from a use of `treeInsert'
>      Possible fix:
>        add (Ord a) to the context of
>          the type signature for insert :: a -> Tree a -> Tree a
>      In the expression: treeInsert a
>      In an equation for `insert': insert a = treeInsert a
>      In the instance declaration for `Set Tree'
>
> Which makes sense, but I'm not sure how to express this constraint.
> So, what is the proper way to do this?
> Where have I gone wrong?
>
>
> Thanks!
>
> Dimitri
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140813/e803ba2d/attachment-0001.html>

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

Message: 4
Date: Wed, 13 Aug 2014 18:21:46 +0200
From: martin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Too much mapM, fmap and concat
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-15

Hello all,

I never did much IO in haskell, but now I have to process some files. I catch 
myself adding a mapM here and an fmap
there. While it is clear to me what these functions do, I wonder if there is a 
way to avoid the noise. Also I could not
find a simple way to pair the lines of a file with its filename. I ended up 
writing a function "nameAndContent". Finally
I am amazed by the many "concat"s I need.

Maybe these things just lie in the nature of the problem ("process a number of 
files"). Otherwise any style suggestions
would be much appreciated.

import System.FilePath.Glob
import Data.List

filePattern="*.hs"
fileDirectory = "."

processFile :: (FilePath, [String]) -> [String]
processFile (path, flines) = ["done"]

main = do
    matchingFiles <- fmap (concat . fst) $ globDir [compile filePattern] 
fileDirectory
    flines <- mapM nameAndContent matchingFiles
    result <- mapM (return . processFile) flines
    mapM putStrLn $ concat  result
            where
                nameAndContent :: FilePath -> IO (FilePath, [String])
                nameAndContent fn = do
                    content <- fmap lines $ readFile fn
                    return (fn, content)



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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 74, Issue 7
****************************************

Reply via email to