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:  Getting into a Haskell project? (Erik de Castro Lopo)
   2. Re:  Getting into a Haskell project? (Erik de Castro Lopo)
   3.  Re: Getting into a Haskell project? (Duncan Mortimer)
   4.  Simple lookahead with Parsec (Derek Thurn)
   5. Re:  Simple lookahead with Parsec (Brandon S. Allbery KF8NH)
   6. Re:  Simple lookahead with Parsec (Derek Thurn)
   7. Re:  Simple lookahead with Parsec (Isaac Dupree)
   8.  Recursion/dynamic programming optimization (ivovnenko)
   9.  How to print the name of a variable (John Velman)
  10. Re:  Recursion/dynamic programming optimization
      (Brandon S. Allbery KF8NH)


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

Message: 1
Date: Fri, 19 Mar 2010 09:58:04 +1100
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Getting into a Haskell project?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Sriram Durbha wrote:

> As you find the need to solve little problems, try to use Haskell for it .
> And after you have a working solution, post it on
> http://reddit.com/r/EnHaskell

Why is there an EnHaskell and a Haskell reddit. They also seem to
have different content!

Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/


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

Message: 2
Date: Fri, 19 Mar 2010 10:05:24 +1100
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Getting into a Haskell project?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Erik de Castro Lopo wrote:

> Why is there an EnHaskell and a Haskell reddit. They also seem to
> have different content!

I would help if I actually read the side bar!

Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/


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

Message: 3
Date: Fri, 19 Mar 2010 10:22:17 +1000
From: Duncan Mortimer <[email protected]>
Subject: [Haskell-beginners] Re: Getting into a Haskell project?
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi all,

Thanks very much for the helpful replies.

regards,
Duncan


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

Message: 4
Date: Fri, 19 Mar 2010 11:14:00 -0400
From: Derek Thurn <[email protected]>
Subject: [Haskell-beginners] Simple lookahead with Parsec
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello. I'm just starting out with Parsec and Haskell, and I want to
parse a very simple grammar with it. I think I may not understand how
to use try correctly.

Here's a simplified version of my grammar:

myType = try (primitiveType) <|> arrayType
primitiveType = do {reserved "int"; return "primitive"}
arrayType = do { primitiveType; symbol "["; symbol "]"; return "array"}

Basically, I want to use lookahead to produce the result "array" on
inputs like "int[]" and the result "primitive" on inputs like "int".
However, no matter what I do with the try function, I am not able to
get what I expect. When I run this code using the runLex function
described in the Parsec documentation, it chokes on "int[]", saying
that the '[' character was unexpected.

I've discovered that switching the order of primitiveType and
arrayType solves this, which makes sense, but I still expected it to
be possible to use try() in this manner to resolve the error with
lookahead. Am I doing something wrong?

(I've got some boilerplate code form the Parsec documentation defined
as well, but I didn't include it for brevity's sake. Reserved and
symbol are defined how you'd expect.)


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

Message: 5
Date: Fri, 19 Mar 2010 11:21:53 -0400
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Simple lookahead with Parsec
To: Derek Thurn <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Mar 19, 2010, at 11:14 , Derek Thurn wrote:
> Here's a simplified version of my grammar:
>
> myType = try (primitiveType) <|> arrayType
> primitiveType = do {reserved "int"; return "primitive"}
> arrayType = do { primitiveType; symbol "["; symbol "]"; return  
> "array"}

What you're doing here is matching and succeeding on primitiveType and  
never even looking for the bracket; your parser then exits and the  
framework fails expecting the end of the token stream but finding the  
unmatched left bracket.

You need to rearrange your cases:

 > myType = try arrayType <|> primitiveType

so that your parser actually checks for the brackets before concluding  
that it has a primitiveType already.

Even better would be to refactor the grammar since both types start  
with a primitiveType, but I'll leave that up to you since it does  
complicate returning the shape of the resulting type.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100319/9db80647/PGP-0001.bin

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

Message: 6
Date: Fri, 19 Mar 2010 14:24:06 -0400
From: Derek Thurn <[email protected]>
Subject: Re: [Haskell-beginners] Simple lookahead with Parsec
To: [email protected],      "Brandon S. Allbery KF8NH"
        <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Mar 19, 2010 at 11:21 AM, Brandon S. Allbery KF8NH
<[email protected]> wrote:
> On Mar 19, 2010, at 11:14 , Derek Thurn wrote:
>>
>> Here's a simplified version of my grammar:
>>
>> myType = try (primitiveType) <|> arrayType
>> primitiveType = do {reserved "int"; return "primitive"}
>> arrayType = do { primitiveType; symbol "["; symbol "]"; return "array"}
>
> What you're doing here is matching and succeeding on primitiveType and never
> even looking for the bracket; your parser then exits and the framework fails
> expecting the end of the token stream but finding the unmatched left
> bracket.
>
> You need to rearrange your cases:
>
>> myType = try arrayType <|> primitiveType
>
> so that your parser actually checks for the brackets before concluding that
> it has a primitiveType already.
>
> Even better would be to refactor the grammar since both types start with a
> primitiveType, but I'll leave that up to you since it does complicate
> returning the shape of the resulting type.
>
> --
> brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
> system administrator [openafs,heimdal,too many hats] [email protected]
> electrical and computer engineering, carnegie mellon university    KF8NH

Ah, I see. The problem is that the primitiveType parser succeeds. Is
there then no generic way to use lookahead in Parsec to make choices
of this nature? I appreciate that it might be possible to refactor my
grammar to never require examining the next token to make a parsing
decision about the current token, but I've already got an LALR(1)
grammar that I'm pretty happy with...

What I'd ideally like is some sort of general solution wherein Parsec
first tries primitiveType, moves to the next token, realizes that it
will be a parse error, and backtracks to try the other alternative.


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

Message: 7
Date: Fri, 19 Mar 2010 15:20:23 -0400
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] Simple lookahead with Parsec
To: Derek Thurn <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 03/19/10 14:24, Derek Thurn wrote:
> Ah, I see. The problem is that the primitiveType parser succeeds. Is
> there then no generic way to use lookahead in Parsec to make choices
> of this nature? I appreciate that it might be possible to refactor my
> grammar to never require examining the next token to make a parsing
> decision about the current token, but I've already got an LALR(1)
> grammar that I'm pretty happy with...

Well, you could learn to make something in Parsec that matches your 
grammar, or you could use a LALR(1) parsing library.  Like "Happy". 
(Well, Happy is actually a sort of preprocessor for Haskell. But still, 
doesn't using the most-fitting tool for the job seem to make sense?)

-Isaac


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

Message: 8
Date: Fri, 19 Mar 2010 16:41:48 -0400
From: ivovnenko <[email protected]>
Subject: [Haskell-beginners] Recursion/dynamic programming
        optimization
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

Hi.
I tried to solve one of the google code jam's problems using haskel
http://code.google.com/codejam/contest/dashboard?c=90101#s=p2:

w :: Eq a => [a] -> [a] -> Int
w text str = w' (reverse text) (reverse str)
w' :: Eq a => [a] -> [a] -> Int
-- Init
w' _ [] = 0
w' [] _ = 0
-- For one symbol string
w' text [a] =
        if head text == a then
                w' (tail text) [a] +1
        else
                w' (tail text) [a]
-- General case
w' text str =
        if head str == head text then
                w' (tail text) (tail str) + w' (tail text) str
        else
                w' (tail text) str

and it worked great on small inputs, not on large:

w 
"viwiwhiuwwwgxzseebneesgzxiekzeeyivrbbhzrlllccxczyurxfhhccghkzvxusfbcucyvncpzpvfvccxcvbqnqogobzoiorxrbikyzzbobpmmmmpmvrzyivpfmmuzxixhqpeebyekkxffeyexeiukuebgbrukzefeu
rkxy z h x vkq fsyp iy  tfyzbptygtsrkhbytvruztzbttnoxuquokipyyqoik ib
xu    
gxchcnccuccyfcccccsczccskifcprgovryxuxoozqoozsvudyfkdpkkxyhdvfsrnvpnpzvrhqhbduidkdpdysinyxnuxnxipddevqhgeufxeepyyezbekyviieeeryiexynevbzfeezryizvepynefffy
y v zuzrbpzn  sz y
vzirkxjniuqvjjbjgubnjkujhjpiyanaabrqagzqqvspqpkvqyuhrgupbaiaasxybiygmmfimyvykkmrzum"
"welcome to code jam"

Can you please advice how to optimize this solution? Thanks.

Regards, Ivan Vovnenko


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

Message: 9
Date: Fri, 19 Mar 2010 15:23:28 -0700
From: John Velman <[email protected]>
Subject: [Haskell-beginners] How to print the name of a variable
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

I have a number of variables (whose values are computed in my program) of
the same type, and in ghci I want to be able to call a function to print
something computed from the value of one or more of these to a file.  I
want the file name to reflect the name of the variable.  This is probably
not clear, but conceptually the function to do the output would look
something like this:

  someFunction :: MyTypeVariable -> String
  writeFileFor :: MyTypeVariable -> IO ()
  writeFileFor v =
      do 
        nameOfV <- someFunction v
        outh <- openFile nameOfV ++ ".txt" WriteMode

        outputstring = calculationsFrom v

        hPutStr outh outputstring

        hClose outh

Is there any such "someFunction", or is there a way to build one?

Thanks,

John Velman




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

Message: 10
Date: Sat, 20 Mar 2010 01:32:59 -0400
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] Recursion/dynamic programming
        optimization
To: ivovnenko <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Mar 19, 2010, at 16:41 , ivovnenko wrote:
> w :: Eq a => [a] -> [a] -> Int
> w text str = w' (reverse text) (reverse str)
> w' :: Eq a => [a] -> [a] -> Int
> -- Init
> w' _ [] = 0
> w' [] _ = 0
> -- For one symbol string
> w' text [a] =
>         if head text == a then
>                 w' (tail text) [a] +1
>         else
>                 w' (tail text) [a]

Couple of suggestions here:

 > w' (x:xs) s...@[a] = w' xs s + (if x == a then 1 else 0)

> -- General case
> w' text str =
>         if head str == head text then
>                 w' (tail text) (tail str) + w' (tail text) str
>         else
>                 w' (tail text) str

 > w' (x:xs) s@(a:as) =
 >   let next = w' xs s
 >    in if x == a then
 >         w' xs as + next
 >       else
 >         next

This will save some function calls by using pattern matching.   
Combining the common subexpressions probably won't count for much  
except in terms of elegance.

The rest probably comes down to strictness but I'll let more  
knowledgeable people comment on that :)

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100320/147b0cd3/PGP.bin

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

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


End of Beginners Digest, Vol 21, Issue 22
*****************************************

Reply via email to