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:  Empty list (Mike Meyer)
   2. Re:  Empty list (Max Voit)
   3.  parsec and a never terminating parser (Adam Flott)
   4.  Open-ended list comprehension (martin)
   5. Re:  Open-ended list comprehension (divyanshu ranjan)
   6. Re:  Open-ended list comprehension (martin)
   7. Re:  Open-ended list comprehension (Marcin Mrotek)
   8. Re:  parsec and a never terminating parser (David McBride)


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

Message: 1
Date: Wed, 25 Mar 2015 09:11:07 -0500
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Empty list
Message-ID:
        <CAD=7u2au1hbbzzdyqb6x92n2jendqd-dj9e17dnzo7fetrv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Wed, Mar 25, 2015 at 9:03 AM, Brandon Allbery <[email protected]>
wrote:

> On Wed, Mar 25, 2015 at 10:00 AM, Norbert Melzer <[email protected]>
> wrote:
>
>> You are correct, a list is not a set. A list is a list of things, that
>> can be there multiple times. A set is a set of things, where nothing can be
>> twice. So take a look at Data.Set
>>
>
> Note that this won't actually solve the original problem; Haskell is an
> implementation of a strongly typed lambda calculus, not of number theory,
> and Haskell collections cannot (easily) contain elements of different types
> --- so the empty set is not an element of a set, and the empty list is not
> an element of a list.
>

Did you forget a couple of "necessarily"'s?

Prelude Data.Set> [] `elem` [[]]
True
Prelude Data.Set> empty `member` (insert empty empty)
True
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150325/7ff0cf94/attachment-0001.html>

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

Message: 2
Date: Wed, 25 Mar 2015 15:12:49 +0100
From: Max Voit <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Empty list
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Hi Shishir,

(in the following I'm ignoring the problem that lists have order and
elements may be in there multiple times) 

On Wed, 25 Mar 2015 13:55:11 +0000
Shishir Srivastava <[email protected]> wrote:

> Shouldn't the empty set by definition be the element of all sets
> including a non-empty set ?

This is not even true in the mathematical sense. True are:
        a) all power sets include the empty set as an *element*
        b) the empty set is a *subset* of all sets

For case (a) you'd need a list of lists since a powerset is a set of
sets.
For (b) `elem` is not the correct function.

Max


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

Message: 3
Date: Wed, 25 Mar 2015 11:37:52 -0400
From: Adam Flott <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] parsec and a never terminating parser
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

I'm having trouble understanding why my simple parser never terminates when
specific input is used.

For example, let's say the first column is a field which can be in one of 4
states, empty, omitted, other, and any arbitrary value. That is,

    data FieldState a = EmptyState | OmittedState | OtherState | FullState a
      deriving (Eq, Ord)

When attempting to use,

    $ echo "- " | ./parser
    "- \n"
    empty ('-')
    $ echo "^ " | ./parser
    "^ \n"
    omitted ('^')
    $ echo "~ " | ./parser
    "~ \n"
    other ('~')
    [ all of this is as expected ]
    $ echo "1 " | ./parser
    "1 \n"
    [ computer twiddles it's thumbs here until I manually terminate it ... ]
    ^C^C
    $

Does anyone know what's happening and now to alleviate it?


-- begin full code --

-- base
import Control.Applicative
import Data.Word

-- Hackage
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.IO as TLIO
import Text.Parsec (parse)
import Text.Parsec.Text.Lazy (Parser)
import Text.Parser.Combinators
import Text.Parser.Char

data FieldState a = EmptyState | OmittedState | OtherState | FullState a
  deriving (Eq, Ord)

instance Functor FieldState where
    fmap f (FullState a)    = FullState (f a)
    fmap _ EmptyState       = EmptyState
    fmap _ OmittedState     = OmittedState
    fmap _ OtherState       = OtherState
    
instance Applicative FieldState where
    pure = FullState
    (FullState f) <*> (FullState x) = FullState (f x)
    _             <*> _             = EmptyState
    
instance Monad FieldState where
    (FullState x) >>= k = k x
    EmptyState    >>= _ = EmptyState
    OmittedState  >>= _ = OmittedState
    OtherState    >>= _ = OtherState

    (FullState _) >> k = k
    EmptyState    >> _ = EmptyState
    OmittedState  >> _ = OmittedState
    OtherState    >> _ = OtherState

    return  = FullState
    fail _  = EmptyState

instance Show (FieldState x) where
    show (EmptyState)   = "empty ('-')"
    show (OmittedState) = "omitted ('^')"
    show (OtherState)   = "other ('~')"
    show x' = show x'
    
data Counter = Counter Word64 deriving (Eq, Ord, Show)

parseNum :: (Num a) => Parser a
parseNum = do
    n <- rd <$> many digit
    return $ fromIntegral n
    where rd = read :: String -> Integer

parseCounter :: Parser Counter
parseCounter = Counter <$> parseNum

parseFieldStateOff :: Parser Char
parseFieldStateOff = char '-'

parseFieldStateOmitted :: Parser Char
parseFieldStateOmitted = char '^'

parseFieldStateOther :: Parser Char
parseFieldStateOther = char '~'

parseFieldState :: Parser a -> Parser (FieldState a)
parseFieldState p = (parseFieldStateOff >> return EmptyState)
                  <|> (parseFieldStateOmitted >> return OmittedState)
                  <|> (parseFieldStateOther >> return OtherState)
                  <|> (p >>= return . FullState)

main :: IO ()
main = do
    ls <- TLIO.getContents
    print ls
    mapM_ processLine (TL.lines ls)

processLine :: TL.Text -> IO ()
processLine line = case (parse (parseFieldState parseCounter) "" line) of
  Left err -> print err
  Right xs -> print xs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150325/d703b853/attachment-0001.sig>

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

Message: 4
Date: Wed, 25 Mar 2015 18:51:48 +0100
From: martin <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Open-ended list comprehension
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Hello all,

when I write my own recursions, I often end up with something like

a : b : c : (f x)

where f x returns a list. I assume this avoids the costly (++) function, right?

But when I use list comprehensions I always get a complete list and it is 
costly to append something to it. How can I
use the power of list comprehensions, but return an [a]->[a] instead of an [a]?

Is this a good idea at all, or will I pile up thunks?





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

Message: 5
Date: Wed, 25 Mar 2015 23:30:54 +0530
From: divyanshu ranjan <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Open-ended list comprehension
Message-ID:
        <CAL9hw24PMGztqVAQxmUR7qfRDROHnfR80cYyhch1PZfjjbo-=a...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi martin,

Check dlist (https://hackage.haskell.org/package/dlist-0.7.1.1).

On Wed, Mar 25, 2015 at 11:21 PM, martin <[email protected]> wrote:

> Hello all,
>
> when I write my own recursions, I often end up with something like
>
> a : b : c : (f x)
>
> where f x returns a list. I assume this avoids the costly (++) function,
> right?
>
> But when I use list comprehensions I always get a complete list and it is
> costly to append something to it. How can I
> use the power of list comprehensions, but return an [a]->[a] instead of an
> [a]?
>
> Is this a good idea at all, or will I pile up thunks?
>
>
>
> _______________________________________________
> 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/20150325/fec4da7d/attachment-0001.html>

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

Message: 6
Date: Wed, 25 Mar 2015 19:20:43 +0100
From: martin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Open-ended list comprehension
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252

Am 03/25/2015 um 07:00 PM schrieb divyanshu ranjan:
> Hi martin,
> 
> Check dlist (https://hackage.haskell.org/package/dlist-0.7.1.1). 

I cannot see how I can create a dlist via a list comprehension, other than 
running fromList. But I assume this already
has o(n) complexity. It may pay after several appends, though.

What I am looking for is, if I have:

veryLongList = [x | x <- ...]

how can I use list a comprehension and end up with a dlist?




> 
> On Wed, Mar 25, 2015 at 11:21 PM, martin <[email protected] 
> <mailto:[email protected]>> wrote:
> 
>     Hello all,
> 
>     when I write my own recursions, I often end up with something like
> 
>     a : b : c : (f x)
> 
>     where f x returns a list. I assume this avoids the costly (++) function, 
> right?
> 
>     But when I use list comprehensions I always get a complete list and it is 
> costly to append something to it. How can I
>     use the power of list comprehensions, but return an [a]->[a] instead of 
> an [a]?
> 
>     Is this a good idea at all, or will I pile up thunks?
> 
> 
> 
>     _______________________________________________
>     Beginners mailing list
>     [email protected] <mailto:[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: 7
Date: Wed, 25 Mar 2015 19:42:06 +0100
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Open-ended list comprehension
Message-ID:
        <CAJcfPz=nbzg_xmop2zxfc5y+pesx+su291ieb+hwm-vopdx...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hello,

DList is a monad, so you can use them like lists with the
MonadComprehensions extension. Alternatively, make use of the
MonadPlus functions, which give you the same power without the nice
syntax.

Best regards,
Marcin MRotek


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

Message: 8
Date: Wed, 25 Mar 2015 17:10:00 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] parsec and a never terminating parser
Message-ID:
        <can+tr43gj_xafbq70n6u4qyjpvpkanymxs8vd5-dbfe6xju...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

The problem is your show instance.

show x' = show x', means that when x' is a FullState, it shows it, which
causes an infinite loop.

You need to take out the default case and add something for FullState.

On Wed, Mar 25, 2015 at 11:37 AM, Adam Flott <[email protected]> wrote:

> I'm having trouble understanding why my simple parser never terminates when
> specific input is used.
>
> For example, let's say the first column is a field which can be in one of 4
> states, empty, omitted, other, and any arbitrary value. That is,
>
>     data FieldState a = EmptyState | OmittedState | OtherState | FullState
> a
>       deriving (Eq, Ord)
>
> When attempting to use,
>
>     $ echo "- " | ./parser
>     "- \n"
>     empty ('-')
>     $ echo "^ " | ./parser
>     "^ \n"
>     omitted ('^')
>     $ echo "~ " | ./parser
>     "~ \n"
>     other ('~')
>     [ all of this is as expected ]
>     $ echo "1 " | ./parser
>     "1 \n"
>     [ computer twiddles it's thumbs here until I manually terminate it ...
> ]
>     ^C^C
>     $
>
> Does anyone know what's happening and now to alleviate it?
>
>
> -- begin full code --
>
> -- base
> import Control.Applicative
> import Data.Word
>
> -- Hackage
> import qualified Data.Text.Lazy as TL
> import qualified Data.Text.Lazy.IO as TLIO
> import Text.Parsec (parse)
> import Text.Parsec.Text.Lazy (Parser)
> import Text.Parser.Combinators
> import Text.Parser.Char
>
> data FieldState a = EmptyState | OmittedState | OtherState | FullState a
>   deriving (Eq, Ord)
>
> instance Functor FieldState where
>     fmap f (FullState a)    = FullState (f a)
>     fmap _ EmptyState       = EmptyState
>     fmap _ OmittedState     = OmittedState
>     fmap _ OtherState       = OtherState
>
> instance Applicative FieldState where
>     pure = FullState
>     (FullState f) <*> (FullState x) = FullState (f x)
>     _             <*> _             = EmptyState
>
> instance Monad FieldState where
>     (FullState x) >>= k = k x
>     EmptyState    >>= _ = EmptyState
>     OmittedState  >>= _ = OmittedState
>     OtherState    >>= _ = OtherState
>
>     (FullState _) >> k = k
>     EmptyState    >> _ = EmptyState
>     OmittedState  >> _ = OmittedState
>     OtherState    >> _ = OtherState
>
>     return  = FullState
>     fail _  = EmptyState
>
> instance Show (FieldState x) where
>     show (EmptyState)   = "empty ('-')"
>     show (OmittedState) = "omitted ('^')"
>     show (OtherState)   = "other ('~')"
>     show x' = show x'
>
> data Counter = Counter Word64 deriving (Eq, Ord, Show)
>
> parseNum :: (Num a) => Parser a
> parseNum = do
>     n <- rd <$> many digit
>     return $ fromIntegral n
>     where rd = read :: String -> Integer
>
> parseCounter :: Parser Counter
> parseCounter = Counter <$> parseNum
>
> parseFieldStateOff :: Parser Char
> parseFieldStateOff = char '-'
>
> parseFieldStateOmitted :: Parser Char
> parseFieldStateOmitted = char '^'
>
> parseFieldStateOther :: Parser Char
> parseFieldStateOther = char '~'
>
> parseFieldState :: Parser a -> Parser (FieldState a)
> parseFieldState p = (parseFieldStateOff >> return EmptyState)
>                   <|> (parseFieldStateOmitted >> return OmittedState)
>                   <|> (parseFieldStateOther >> return OtherState)
>                   <|> (p >>= return . FullState)
>
> main :: IO ()
> main = do
>     ls <- TLIO.getContents
>     print ls
>     mapM_ processLine (TL.lines ls)
>
> processLine :: TL.Text -> IO ()
> processLine line = case (parse (parseFieldState parseCounter) "" line) of
>   Left err -> print err
>   Right xs -> print xs
>
> _______________________________________________
> 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/20150325/fdb78372/attachment.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 81, Issue 60
*****************************************

Reply via email to