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:  consing an element to a list inside a file ([email protected])
   2. Re:  consing an element to a list inside a file (Daniel Fischer)
   3. Re:  consing an element to a list inside a file ([email protected])
   4. Re:  consing an element to a list inside a file (Daniel Fischer)
   5. Re:  consing an element to a list inside a file ([email protected])
   6. Re:  consing an element to a list inside a file (Daniel Fischer)
   7.  Data.Tree computation (Gabi)
   8.  Re: Data.Tree computation (Heinrich Apfelmus)
   9. Re:  Data.Tree computation (Stephen Tetley)


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

Message: 1
Date: Thu, 11 Feb 2010 21:09:37 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] consing an element to a list inside a
        file
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

mysecondlist = 0:mylist

that's was I was searching for, but how can I add the element to the same list? 
I have to get any number of inputs from the user and at the end, if he doesn't 
want to make more inputs, I have to print a list with all the inputs he did.

-------- Original-Nachricht --------
> Datum: Wed, 10 Feb 2010 22:21:13 +0100
> Von: Daniel Fischer <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] consing an element to a list inside a file

> Am Mittwoch 10 Februar 2010 21:52:48 schrieb [email protected]:
> > Hi,
> > I want to add an element to a list inside a haskell file:
> > mylist = [1,2,3,4]
> > 0:mylist
> > in Prelude it works fine, but when I do it in a file I get the error:
> > parse error (possibly incorrect indentation)
> > Failed, modules loaded: none.
> > in a line after this two that doesn't exist
> 
> 0:mylist
> 
> is a plain value. At the ghci or hugs prompt, if you enter an expression 
> (4, 3*7+5, [0 .. 10],...), that means "evaluate the expression and print 
> the result" (unless it's a value of type IO a, then it means "execute this
> action").
> In a source file, you write definitions,
> 
> name = expression to be bound to name
> 
> func arg1 arg2 = body
> 
> etc.
> 
> You can have pattern bindings in a source file,
> 
> (a,b) = ([0 .. 100],True)
> 
> -- defines a and b
> 
> 0:mylist = map (`mod` 7) [14 .. 100]
> 
> -- defines mylist as [1,2,3,4,5,6,0,1,...,1,2]
> 
> 0:myotherlist = [4,5,6]
> 
> {- will fail with
> ghci> myotherlist
> *** Exception: PatTest.hs:4:0-22: Irrefutable pattern failed for pattern 0
> : myotherlist
> 
> when demanded
> -}
> , so that's what the parser expected, a '=' and an expression which
> defines 
> mylist.
> Since it didn't find a '=' on the same line, it tried the next (where it 
> found end of file, another definition or whatever) where it reported the 
> parse error.
> 
> Probably you wanted
> 
> mysecondlist = 0:mylist
> 
> ?

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser


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

Message: 2
Date: Thu, 11 Feb 2010 21:41:15 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] consing an element to a list inside a
        file
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Donnerstag 11 Februar 2010 21:09:37 schrieb [email protected]:
> mysecondlist = 0:mylist
>
> that's was I was searching for, but how can I add the element to the
> same list?

You can't. When you add an element to a list, it's a different list.

> I have to get any number of inputs from the user and at the
> end, if he doesn't want to make more inputs, I have to print a list with
> all the inputs he did.

Make the list a parameter of the input loop.

getInputs previous = do
    user <- getUserInput
    if endNow user
      then printList previous
      else getInputs (user:previous)



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

Message: 3
Date: Thu, 11 Feb 2010 22:34:45 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] consing an element to a list inside a
        file
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

the problem is the function should be
readMyDatatype :: IO MyDatatype
readMyDatatype = do ...
Is it possible to do it somehow different without a parameter?

-------- Original-Nachricht --------
> Datum: Thu, 11 Feb 2010 21:41:15 +0100
> Von: Daniel Fischer <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] consing an element to a list inside a file

> Am Donnerstag 11 Februar 2010 21:09:37 schrieb [email protected]:
> > mysecondlist = 0:mylist
> >
> > that's was I was searching for, but how can I add the element to the
> > same list?
> 
> You can't. When you add an element to a list, it's a different list.
> 
> > I have to get any number of inputs from the user and at the
> > end, if he doesn't want to make more inputs, I have to print a list with
> > all the inputs he did.
> 
> Make the list a parameter of the input loop.
> 
> getInputs previous = do
>     user <- getUserInput
>     if endNow user
>       then printList previous
>       else getInputs (user:previous)

-- 
NEU: Mit GMX DSL über 1000,- ¿ sparen!
http://portal.gmx.net/de/go/dsl02


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

Message: 4
Date: Thu, 11 Feb 2010 22:54:59 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] consing an element to a list inside a
        file
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Donnerstag 11 Februar 2010 22:34:45 schrieb [email protected]:
> the problem is the function should be
> readMyDatatype :: IO MyDatatype
> readMyDatatype = do ...
> Is it possible to do it somehow different without a parameter?

Separate the worker from the API function.

API:

readMyDatatype :: IO MyDatatype
readMyDatatype = call worker with appropriate arguments

worker:

readMD :: t1 -> t2 -> ... -> tn -> IO MyDatatype

>
> -------- Original-Nachricht --------
>
> > Datum: Thu, 11 Feb 2010 21:41:15 +0100
> > Von: Daniel Fischer <[email protected]>
> > An: [email protected]
> > CC: [email protected]
> > Betreff: Re: [Haskell-beginners] consing an element to a list inside a
> > file
> >
> > Am Donnerstag 11 Februar 2010 21:09:37 schrieb [email protected]:
> > > mysecondlist = 0:mylist
> > >
> > > that's was I was searching for, but how can I add the element to the
> > > same list?
> >
> > You can't. When you add an element to a list, it's a different list.
> >
> > > I have to get any number of inputs from the user and at the
> > > end, if he doesn't want to make more inputs, I have to print a list
> > > with all the inputs he did.
> >
> > Make the list a parameter of the input loop.
> >
> > getInputs previous = do
> >     user <- getUserInput
> >     if endNow user
> >       then printList previous
> >       else getInputs (user:previous)



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

Message: 5
Date: Thu, 11 Feb 2010 23:04:01 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] consing an element to a list inside a
        file
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

but the exercise was nearly complete on the sheet and I just have to complete 
two parts and one of them is to ask the user if he want's to do more input. If 
so, I have to let him input a new value (triple) for MyDatatype, otherwise I 
have to print the list with all triples

-------- Original-Nachricht --------
> Datum: Thu, 11 Feb 2010 22:54:59 +0100
> Von: Daniel Fischer <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] consing an element to a list inside a file

> Am Donnerstag 11 Februar 2010 22:34:45 schrieb [email protected]:
> > the problem is the function should be
> > readMyDatatype :: IO MyDatatype
> > readMyDatatype = do ...
> > Is it possible to do it somehow different without a parameter?
> 
> Separate the worker from the API function.
> 
> API:
> 
> readMyDatatype :: IO MyDatatype
> readMyDatatype = call worker with appropriate arguments
> 
> worker:
> 
> readMD :: t1 -> t2 -> ... -> tn -> IO MyDatatype
> 
> >
> > -------- Original-Nachricht --------
> >
> > > Datum: Thu, 11 Feb 2010 21:41:15 +0100
> > > Von: Daniel Fischer <[email protected]>
> > > An: [email protected]
> > > CC: [email protected]
> > > Betreff: Re: [Haskell-beginners] consing an element to a list inside a
> > > file
> > >
> > > Am Donnerstag 11 Februar 2010 21:09:37 schrieb [email protected]:
> > > > mysecondlist = 0:mylist
> > > >
> > > > that's was I was searching for, but how can I add the element to the
> > > > same list?
> > >
> > > You can't. When you add an element to a list, it's a different list.
> > >
> > > > I have to get any number of inputs from the user and at the
> > > > end, if he doesn't want to make more inputs, I have to print a list
> > > > with all the inputs he did.
> > >
> > > Make the list a parameter of the input loop.
> > >
> > > getInputs previous = do
> > >     user <- getUserInput
> > >     if endNow user
> > >       then printList previous
> > >       else getInputs (user:previous)

-- 
NEU: Mit GMX DSL über 1000,- ¿ sparen!
http://portal.gmx.net/de/go/dsl02


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

Message: 6
Date: Thu, 11 Feb 2010 23:30:05 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] consing an element to a list inside a
        file
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Donnerstag 11 Februar 2010 23:04:01 schrieb [email protected]:
> but the exercise was nearly complete on the sheet and I just have to
> complete two parts and one of them is to ask the user if he want's to do
> more input. If so, I have to let him input a new value (triple) for
> MyDatatype, otherwise I have to print the list with all triples
>

But to print it, you need to have a reference to the list of inputs. The 
easiest way is to have it as a parameter in the worker loop.

What *exactly* is your task? What is given and what are the specifications 
for a solution?

> >
> > Am Donnerstag 11 Februar 2010 22:34:45 schrieb [email protected]:
> > > the problem is the function should be
> > > readMyDatatype :: IO MyDatatype
> > > readMyDatatype = do ...
> > > Is it possible to do it somehow different without a parameter?
> >
> > Separate the worker from the API function.
> >
> > API:
> >
> > readMyDatatype :: IO MyDatatype
> > readMyDatatype = call worker with appropriate arguments
> >
> > worker:
> >
> > readMD :: t1 -> t2 -> ... -> tn -> IO MyDatatype



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

Message: 7
Date: Fri, 12 Feb 2010 11:31:49 +0200
From: Gabi <[email protected]>
Subject: [Haskell-beginners] Data.Tree computation
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi guys,
I've tried to use Data.Tree as a computation tree (each node is
numerical function, each leaf is a terminal)
It kinda works, but the code seems very verbose.  How can it made more
concise ? I am sure I missed a lot of shortcuts and idioms.

-- file t.hs
import qualified Data.Tree as T

data Term = TInt Int| TDouble Double
            deriving (Show, Eq)

data Func = Plus | Minus | Mult | Div
            deriving (Show, Eq)

data ANode = GFunc Func | GTerm Term
              deriving (Show, Eq)

fNode :: Func -> T.Forest ANode-> T.Tree ANode
fNode f = T.Node (GFunc f)

tNode:: Term -> T.Tree ANode
tNode t = T.Node (GTerm t) []

calc :: T.Tree ANode -> Double
calc (T.Node (GTerm (TInt n))[]) = fromIntegral n :: Double
calc (T.Node (GFunc Plus) xs ) = foldl1 (+) (map calc xs)
calc (T.Node (GFunc Minus) xs ) = foldl1 (-) (map calc xs)
calc (T.Node (GFunc Mult) xs ) = foldl1 (*) (map calc xs)
calc (T.Node (GFunc Div) xs ) = foldl1 (/) (map calc xs)

-- (/ (+ 5 5 (- 10 100)) 10) -  calc Should return -8.0
aTree =  fNode Div [fNode Plus [tNode $ TInt 5,tNode $ TInt 5, fNode
Minus [tNode $ TInt 10,tNode $ TInt 100]], tNode (TInt 10)]


Regards,
Gabi

http://bugspy.net


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

Message: 8
Date: Fri, 12 Feb 2010 11:48:00 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Data.Tree computation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Gabi wrote:
> I've tried to use Data.Tree as a computation tree (each node is
> numerical function, each leaf is a terminal)
> It kinda works, but the code seems very verbose.  How can it made more
> concise ? I am sure I missed a lot of shortcuts and idioms.
> 
> -- file t.hs
> import qualified Data.Tree as T
> 
> data Term = TInt Int| TDouble Double
>             deriving (Show, Eq)
> 
> data Func = Plus | Minus | Mult | Div
>             deriving (Show, Eq)
> 
> data ANode = GFunc Func | GTerm Term
>               deriving (Show, Eq)
> 
> fNode :: Func -> T.Forest ANode-> T.Tree ANode
> fNode f = T.Node (GFunc f)
> 
> tNode:: Term -> T.Tree ANode
> tNode t = T.Node (GTerm t) []
> 
> calc :: T.Tree ANode -> Double
> calc (T.Node (GTerm (TInt n))[]) = fromIntegral n :: Double
> calc (T.Node (GFunc Plus) xs ) = foldl1 (+) (map calc xs)
> calc (T.Node (GFunc Minus) xs ) = foldl1 (-) (map calc xs)
> calc (T.Node (GFunc Mult) xs ) = foldl1 (*) (map calc xs)
> calc (T.Node (GFunc Div) xs ) = foldl1 (/) (map calc xs)

How about

    calc :: T.Tree ANode -> Double
    calc (T.Node (GTerm sym) [])      = term sym
        where
        term (TInt n)    = fromIntegral n
        term (TDouble d) = d
    calc (T.Node (GFunc sym)      xs) = foldl1 (op sym) (map calc xs)
        where
        op Plus  = (+)
        op Minus = (-)
        op Mult  = (*)
        op Div   = (/)


By the way,  Data.Tree  is not used very often, people usually roll
their own syntax trees because it's so easy.

   data Expr  = V Value | App Fun [Expr]
   data Value = VInt Int | VDouble Double
   data Fun   = Plus | Minus | Mult | Div

   eval :: Expr -> Double
   eval (V (VInt    n)) = fromIntegral n
   eval (V (VDouble d)) = d
   eval (App sym xs   ) = foldl1 (op sym) (map eval xs)
      where
      op Plus = ...


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



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

Message: 9
Date: Fri, 12 Feb 2010 10:48:57 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Data.Tree computation
To: Gabi <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello

One idiom is to avoid Data.Tree unless you really have a "rose tree" -
Haskell's algebraic data types model trees.

Note this one is a binary tree rather than a "rose tree" - _plus_
doesn't work quite the same as it did in your original.

(+ 1 2 3 4) == (+ 1 (+ 2 (+ 3 4)))


data Val = VInt Int
         | VDbl Double
  deriving (Eq,Ord,Show)

data Op = Plus | Minus | Mult | Div
  deriving (Eq,Ord,Show)

-- Note this is a binary tree
-- Data.Tree is a "rose tree"
data Tree = Tree { operator     :: Op
                 , left_branch  :: Tree
                 , right_branch :: Tree }
          | Leaf { value :: Val }
  deriving (Eq,Show)

-- Or whithout field labels:
-- data Tree = Tree Op Tree Tree
--           | Leaf  Val


calc :: Tree -> Double
calc (Leaf v)         = val v
calc (Tree Plus  l r) = calc l + calc r
calc (Tree Minus l r) = calc l - calc r
calc (Tree Mult  l r) = calc l * calc r
calc (Tree Div   l r) = calc l / calc r

val :: Val -> Double
val (VInt i) = fromIntegral i
val (VDbl d) = d

-- "wrapped" constructors

dblLeaf :: Double -> Tree
dblLeaf d = Leaf $ VDbl d

intLeaf :: Int -> Tree
intLeaf i = Leaf $ VInt i

-- simulates: (+ a b c ... n)
--
plus :: [Tree] -> Tree
plus []     = error "Bad plus"
plus [a]    = a
plus (a:as) = Tree Plus a (plus as)

-- (/ (+ 5 5 (- 10 100)) 10) -  calc Should return -8.0
aTree :: Tree
aTree =  Tree Div (plus [ intLeaf 5
                        , intLeaf 5
                        , Tree Minus (intLeaf 10) (intLeaf 100) ])
                  (intLeaf 10)


demo1 = calc aTree

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

Once you've made the tree type concrete there are other variations you
can consider.

E.g, a polymorphic tree - leaf type is a parameter:

data Tree a = Tree Op (Tree a) (Tree a)
              | Leaf a
  deriving (Eq,Show)

Or you could really wanted a multiple argument plus:


data Op2 = Minus' | Mult' | Div'
  deriving (Eq,Ord,Show)

data Tree2 = Tree Op2 Tree Tree
           | MultiPlus [Tree]
           | Leaf  Val

... although once things start going irregular, they often cause problems later.


Best wishes

Stephen


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

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


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

Reply via email to