Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  parse String -> Expression (John Moore)
   2. Re:  parse String -> Expression (Felipe Lessa)
   3. Re:  parse String -> Expression (Felipe Lessa)
   4. Re:  parse String -> Expression (Daniel Fischer)
   5.  Re: Installing packages in Ubuntu (Nathan M. Holden)
   6.  Re: Installing packages in Ubuntu (Maur??cio CA)
   7.  Simple haskell problem ! Help please (Denis Firsov)
   8. Re:  Simple haskell problem ! Help please (Joe Fredette)
   9.  Re: Simple haskell problem ! Help please (Tim Attwood)


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

Message: 1
Date: Sun, 8 Nov 2009 17:53:48 +0000
From: John Moore <john.moor...@gmail.com>
Subject: [Haskell-beginners] parse String -> Expression
To: beginners@haskell.org
Message-ID:
        <4f7ad1ad0911080953t17ffd10co237995d6a6ce5...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi,
    I'm trying to find a way to parseRPN (Reverse Polish Numbers) to
expressions rather than to just numbers. e.g. I want the answer to be in the
form

(Multiply (Val 2) (Val 3)) rather than just the answer.



Are these anyway near the steps
parseRPN :: String->Expression

This is a lot more complicated then I thought.!!!

First do we have to read in a string is this (IsString)

 fromString :: String -> a

Then this goes on a stack

pushStack :: a -> Stack -> Stack (Takes a value and puts in on a stack)

Later we pop it off

popStack :: Stack -> (a,Stack) -- takes the value of the stack and leaves
the stack

Do we also have to define taking off the stack such as head(popstack) or
fst(popstack) if we do we would probably have  one for putting it onto a
stack.

Do we then turn the value into an Expression.?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091108/4fec4429/attachment-0001.html

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

Message: 2
Date: Sun, 8 Nov 2009 16:49:37 -0200
From: Felipe Lessa <felipe.le...@gmail.com>
Subject: Re: [Haskell-beginners] parse String -> Expression
To: beginners@haskell.org
Message-ID: <20091108184937.ga22...@kira.casa>
Content-Type: text/plain; charset=us-ascii

On Sun, Nov 08, 2009 at 05:53:48PM +0000, John Moore wrote:
> Hi,
>     I'm trying to find a way to parseRPN (Reverse Polish Numbers) to
> expressions rather than to just numbers. e.g. I want the answer to be in the
> form
>
> (Multiply (Val 2) (Val 3)) rather than just the answer.

You don't need to code all the parser by hand.  You can use, for
example, Parsec parsers.

Spoilers ahead!!!






If your data type is

data Expr a = Multiply (Expr a) (Expr a)
            | Val a

then you may write something like

expression :: Parser a -> Parser (Expr a)
expression valParser = spaces >> (mult <|> val)
  where
    expr = expression valParser
    mult = char "*" >> (Multiply <$> expr <*> expr)
    val  = Val <$> valParser

using Parsec for a concrete example.

HTH,

--
Felipe.


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

Message: 3
Date: Sun, 8 Nov 2009 16:53:00 -0200
From: Felipe Lessa <felipe.le...@gmail.com>
Subject: Re: [Haskell-beginners] parse String -> Expression
To: beginners@haskell.org
Message-ID: <20091108185300.gb22...@kira.casa>
Content-Type: text/plain; charset=us-ascii

On Sun, Nov 08, 2009 at 04:49:37PM -0200, Felipe Lessa wrote:
> You don't need to code all the parser by hand.  You can use, for
> example, Parsec parsers.

IOW, you may use the language's implicit stack instead of
building your own.

--
Felipe.


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

Message: 4
Date: Sun, 8 Nov 2009 20:00:10 +0100
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] parse String -> Expression
To: beginners@haskell.org
Message-ID: <200911082000.11449.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-15"

Am Sonntag 08 November 2009 18:53:48 schrieb John Moore:
> Hi,
>     I'm trying to find a way to parseRPN (Reverse Polish Numbers) to
> expressions rather than to just numbers. e.g. I want the answer to be in
> the form
>
> (Multiply (Val 2) (Val 3)) rather than just the answer.
>

I'd suggest using something like

type Stack = [Expression]

parseRPN :: Parser Expression
parseRPN = rpn []

parseVal :: Parser Expression
parseVal = do
    num <- parseNumber
    return (Val num)

rpn :: Stack -> Parser Expression
rpn stack = (do
    char '+'
    case stack of
        (x:y:ts) -> rpn (Add x y:ts)
        _ -> parsecFail "BinOp requires two values on Stack")
    <|> (do
    char '*'
    case stack of
        (x:y:ts) -> rpn (Multiply x y:ts)
        _ -> parsecFail "BinOp requires two values on Stack")
    <|> ...
    <|> (do
    v <- parseVal
    rpn (v:stack))
    <|> (do
    eof
    case stack of
        [x] -> return x
        _ -> parsecFail "No parse")

-- You could also use the userstate of Parsec for the stack

>
>
> Are these anyway near the steps
> parseRPN :: String->Expression
>
> This is a lot more complicated then I thought.!!!
>
> First do we have to read in a string is this (IsString)
>
>  fromString :: String -> a
>
> Then this goes on a stack
>
> pushStack :: a -> Stack -> Stack (Takes a value and puts in on a stack)
>
> Later we pop it off
>
> popStack :: Stack -> (a,Stack) -- takes the value of the stack and leaves
> the stack
>
> Do we also have to define taking off the stack such as head(popstack) or
> fst(popstack) if we do we would probably have  one for putting it onto a
> stack.
>
> Do we then turn the value into an Expression.?



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

Message: 5
Date: Sun, 8 Nov 2009 14:14:06 -0500
From: "Nathan M. Holden" <nathanmhol...@gmail.com>
Subject: [Haskell-beginners] Re: Installing packages in Ubuntu
To: streb...@hotmail.com
Cc: beginners@haskell.org
Message-ID: <200911081414.06456.nathanmhol...@gmail.com>
Content-Type: Text/Plain;  charset="us-ascii"

I can't say that you NEED to be using Cabal. I have it, and I'm also an Ubuntu 
newbie. You can download the bootstrap installer, you'll need to install (via 
runghc Setup configure, runghc Setup build, runghc Install) the most recent 
HTML package, and parsec 2.x to get it to run. Then just add an alias (or, I 
did) so you don't have to write a super-long command to run Cabal.

Running Kubuntu 9.10

On Sunday 08 November 2009 08:28:24 am beginners-requ...@haskell.org wrote:
> Message: 6
> Date: Sat, 7 Nov 2009 22:05:34 +0000 (UTC)
> From: Glurk <streb...@hotmail.com>
> Subject: [Haskell-beginners] Installing packages in Ubuntu
> To: beginners@haskell.org
> Message-ID: <loom.20091107t230222-...@post.gmane.org>
> Content-Type: text/plain; charset=us-ascii
> 
> Hi,
> 
> I'm a beginner to both Cabal and Linux.
> I'm a bit confused as to whether I need to be using Cabal when installng
> packages.
> 
> I'm running Ubuntu 9.10, and from the Synaptic Package Manager, I can
>  select various Haskell packages to install - will this install the package
>  correctly, or is there something I need to do in Cabal as well ?
> 
> Thanks ! :)


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

Message: 6
Date: Sun, 08 Nov 2009 18:19:39 -0200
From: Maur??cio CA <mauricio.antu...@gmail.com>
Subject: [Haskell-beginners] Re: Installing packages in Ubuntu
To: beginners@haskell.org
Message-ID: <hd794s$3i...@ger.gmane.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

> I'm a beginner to both Cabal and Linux.
> I'm a bit confused as to whether I need to be using Cabal when installng 
> packages.

There's no real confront between then, as cabal will default to
install its packages in your home directory. Follow this rule in
Ubuntu: install what's available from Ubuntu repositories (using
Synaptic) and, whenever you want, need or feel like doing it,
install from cabal. If something goes wrong for any reason, just

   rm ~/.cabal

and you get back to what you have in a fresh install.

(If you are a begginer in Haskell: also do use cabal system to
package your own programs. This will give you a good understandment
on how the package system work, and let you more confortable to
experiment with advanced language features.)

Best,
MaurĂ­cio



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

Message: 7
Date: Sun, 8 Nov 2009 00:01:46 +0200
From: Denis Firsov <denis.fir...@gmail.com>
Subject: [Haskell-beginners] Simple haskell problem ! Help please
To: beginners@haskell.org
Message-ID:
        <5874e6730911071401t280bc718m4e94514f58e2a...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi ! I am beginner in Haskell and have problems with this problem:
compress :: Eq a => [a] -> [(a, Int)]
If you have string "AAABCCC" it transforms it to : {A, 3} {B,1} {C,3}

Could you help me with it ?
Thank you in advance !


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

Message: 8
Date: Sun, 8 Nov 2009 23:56:37 -0500
From: Joe Fredette <jfred...@gmail.com>
Subject: Re: [Haskell-beginners] Simple haskell problem ! Help please
To: Denis Firsov <denis.fir...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <c9c4b3f2-3cf7-4927-bcf2-e3f0a017e...@gmail.com>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes

Here are some hints to consider:

1. There are two ways to think about this -- pattern matching on a list,
    or higher-order functions
2. The former will require thinking about several cases,
        a. What is a "compressed" empty list?
        b. How many elements do we need to look at at once?
        c. What happens if the first and the next element are the same?
        d. What if they're different?
3. Higher order functions over the list might be easier, consider the
    `takeWhile` and `dropWhile` functions, as well as the `group`  
function,
    there are several ways to use these functions to solve the problem.


HTH

/Joe

On Nov 7, 2009, at 5:01 PM, Denis Firsov wrote:

> Hi ! I am beginner in Haskell and have problems with this problem:
> compress :: Eq a => [a] -> [(a, Int)]
> If you have string "AAABCCC" it transforms it to : {A, 3} {B,1} {C,3}
>
> Could you help me with it ?
> Thank you in advance !
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 9
Date: Mon, 9 Nov 2009 00:07:38 -0800
From: "Tim Attwood" <timoth...@comcast.net>
Subject: [Haskell-beginners] Re: Simple haskell problem ! Help please
To: beginners@haskell.org
Message-ID: <hd8ike$u9...@ger.gmane.org>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
        reply-type=original

> Hi ! I am beginner in Haskell and have problems with this problem:
> compress :: Eq a => [a] -> [(a, Int)]
> If you have string "AAABCCC" it transforms it to : {A, 3} {B,1} {C,3}
> 
> Could you help me with it ?
> Thank you in advance !

This is straightforward with a list comprehension and the group function.

import Data.List

compress s = [(head g, length g) | g <- group s]



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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 17, Issue 11
*****************************************

Reply via email to