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.  N-ary tree search problems (Ryan Temple)
   2. Re:  N-ary tree search problems (i?fai)
   3.  Show Floats (Nathan M. Holden)
   4. Re:  Show Floats (i?fai)
   5. re:  Show Floats (Nathan M. Holden)
   6. Re:  Show Floats (David Virebayre)
   7. Re:  Show Floats (Johannes Laire)
   8. Re:  if ands (Deniz Dogan)
   9. Re:  N-ary tree search problems (Stephen Tetley)


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

Message: 1
Date: Wed, 4 Nov 2009 12:04:08 +0000
From: Ryan Temple <ryantemple...@googlemail.com>
Subject: [Haskell-beginners] N-ary tree search problems
To: beginners@haskell.org
Message-ID:
        <33ff8d520911040404h2db6f4c8k473e1b87e2b35...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

I'm making a general purpose N-ary tree and im coming up with "unexpected
'=' on line 17" as an error.  I have spent a fair while trying to work out
why this isn't accepting the case that an Empty gtree returns false for any
member search.  i realise this is probably a very trivial error but any help
would be appreciated:

module GTrees where

data Gtree = Empty |
             Leaf String |
             Node String [Gtree]
             deriving (Show)

--Tests if a given string is a member of the tree

gtreeMember :: (Ord a) => a -> Gtree a -> Bool
  gtreeMember y Empty = False  -- line 17
  gtreeMember y (Leaf x) = (x==y)
  gtreeMember y (Node x tree)
      |x==y = True
      |otherwise gtreeMember tree

This is the code up to the point of the error with the error line
highlighted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091104/18de551b/attachment-0001.html

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

Message: 2
Date: Thu, 05 Nov 2009 22:10:04 -0500
From: i?fai <iae...@me.com>
Subject: Re: [Haskell-beginners] N-ary tree search problems
To: Ryan Temple <ryantemple...@googlemail.com>
Cc: Beginners@haskell.org
Message-ID: <e9a2eaca-4a80-4d8b-be1a-a22c96db8...@me.com>
Content-Type: text/plain; charset=us-ascii; format=flowed; delsp=yes

Try to make sure things are lined up properly, like the Empty and  
Left, and why are some gtreeMember indented?

On 2009-11-04, at 7:04 AM, Ryan Temple wrote:

> I'm making a general purpose N-ary tree and im coming up with  
> "unexpected
> '=' on line 17" as an error.  I have spent a fair while trying to  
> work out
> why this isn't accepting the case that an Empty gtree returns false  
> for any
> member search.  i realise this is probably a very trivial error but  
> any help
> would be appreciated:
>
> module GTrees where
>
> data Gtree = Empty |
>             Leaf String |
>             Node String [Gtree]
>             deriving (Show)
>
> --Tests if a given string is a member of the tree
>
> gtreeMember :: (Ord a) => a -> Gtree a -> Bool
>  gtreeMember y Empty = False  -- line 17
>  gtreeMember y (Leaf x) = (x==y)
>  gtreeMember y (Node x tree)
>      |x==y = True
>      |otherwise gtreeMember tree
>
> This is the code up to the point of the error with the error line
> highlighted
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 3
Date: Fri, 6 Nov 2009 00:31:05 -0500
From: "Nathan M. Holden" <nathanmhol...@gmail.com>
Subject: [Haskell-beginners] Show Floats
To: beginners@haskell.org
Message-ID: <200911060031.05956.nathanmhol...@gmail.com>
Content-Type: Text/Plain;  charset="us-ascii"

I have been working on a small library that will typeset notes in LaTeX for 
me, since I tend to have haphazard typesetting while I write, but while I read 
I like to have standards.

Anyways, I defined a datatype

data Color = RGB {
  name :: [Char],
  r :: Float,
  g :: Float,
  b :: Float,
  matchText :: [[Char]],
  targetText :: [Char]}
  deriving(Show,Eq,Read)

I wanted to be able to have a piece of code that said

"\\definecolor{"++name++"}{rgb}{"++show r++","++show g++","++show 
b++"}"

but because I have numbers below 0.1, it outputs as 2.0e-2, which is 
useless. I wrote a function that would output useful numbers, but it's REALLY 
bad Haskell:

fToInt :: Float -> Int
fToInt f = if f >= 10 then fToInt (f-10.0)
  else if (f >= 9) then 9
    else if (f >= 8) then 8
      else if (f >= 7) then 7
        else if (f >= 6) then 6
          else if (f >= 5) then 5
            else if (f >= 4) then 4
              else if (f >= 3) then 3
                else if (f >= 2) then 2
                  else if (f >= 1) then 1 else 0

It takes up 11 lines in a module that's only got 74! (128  if you count the 
module to translate the notes into a .tex file)

how would I write this better?


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

Message: 4
Date: Fri, 06 Nov 2009 00:36:06 -0500
From: i?fai <iae...@me.com>
Subject: Re: [Haskell-beginners] Show Floats
To: "Nathan M. Holden" <nathanmhol...@gmail.com>
Cc: Beginners@haskell.org
Message-ID: <74daf871-6666-4a07-a07a-ca2e4e675...@me.com>
Content-Type: text/plain; charset=us-ascii; format=flowed; delsp=yes

Look at the 'floor' function. It would simplify a lot of cases for  
sure. But I suspect there is a better way to do that function as a  
whole.

On 2009-11-06, at 12:31 AM, Nathan M. Holden wrote:

> I have been working on a small library that will typeset notes in  
> LaTeX for
> me, since I tend to have haphazard typesetting while I write, but  
> while I read
> I like to have standards.
>
> Anyways, I defined a datatype
>
> data Color = RGB {
>  name :: [Char],
>  r :: Float,
>  g :: Float,
>  b :: Float,
>  matchText :: [[Char]],
>  targetText :: [Char]}
>  deriving(Show,Eq,Read)
>
> I wanted to be able to have a piece of code that said
>
> "\\definecolor{"++name++"}{rgb}{"++show r++","++show g++","++show
> b++"}"
>
> but because I have numbers below 0.1, it outputs as 2.0e-2, which is
> useless. I wrote a function that would output useful numbers, but  
> it's REALLY
> bad Haskell:
>
> fToInt :: Float -> Int
> fToInt f = if f >= 10 then fToInt (f-10.0)
>  else if (f >= 9) then 9
>    else if (f >= 8) then 8
>      else if (f >= 7) then 7
>       else if (f >= 6) then 6
>         else if (f >= 5) then 5
>           else if (f >= 4) then 4
>             else if (f >= 3) then 3
>               else if (f >= 2) then 2
>                 else if (f >= 1) then 1 else 0
>
> It takes up 11 lines in a module that's only got 74! (128  if you  
> count the
> module to translate the notes into a .tex file)
>
> how would I write this better?
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 5
Date: Fri, 6 Nov 2009 00:59:26 -0500
From: "Nathan M. Holden" <nathanmhol...@gmail.com>
Subject: re: [Haskell-beginners] Show Floats
To: beginners@haskell.org
Message-ID: <200911060059.26931.nathanmhol...@gmail.com>
Content-Type: Text/Plain;  charset="us-ascii"

Um... well, I solved my own problem.

It turns out that my 20-30 minutes of searching for a way to \definecolor in 
terms of 0-255 instead of 0.0-1.0 were just 5 minutes short of finding out a 
way to do it.

This is embarrassing.


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

Message: 6
Date: Fri, 6 Nov 2009 08:32:15 +0100
From: David Virebayre <dav.vire+hask...@gmail.com>
Subject: Re: [Haskell-beginners] Show Floats
To: "Nathan M. Holden" <nathanmhol...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <4c88418c0911052332v580c1a91k479e3d8ccb613...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Fri, Nov 6, 2009 at 6:59 AM, Nathan M. Holden <nathanmhol...@gmail.com>wrote:

> Um... well, I solved my own problem.
>
> It turns out that my 20-30 minutes of searching for a way to \definecolor
> in
> terms of 0-255 instead of 0.0-1.0 were just 5 minutes short of finding out
> a
> way to do it.
>
> This is embarrassing.
>

If you have this problem again, you can use printf :

GHCi, version 6.10.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Text.Printf
Prelude Text.Printf> let f=0.001
Prelude Text.Printf> f
1.0e-3
Prelude Text.Printf> printf "%5.4f\n" f
0.0010
Prelude Text.Printf>



> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091106/b74b4f83/attachment-0001.html

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

Message: 7
Date: Fri, 6 Nov 2009 09:39:40 +0200
From: Johannes Laire <johannes.la...@gmail.com>
Subject: Re: [Haskell-beginners] Show Floats
To: David Virebayre <dav.vire+hask...@gmail.com>
Cc: "Nathan M. Holden" <nathanmhol...@gmail.com>,
        beginners@haskell.org
Message-ID:
        <5f271a760911052339r3bd79aban85ded4970cb39...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Nov 6, 2009 at 9:32 AM, David Virebayre
<dav.vire+hask...@gmail.com> wrote:
> On Fri, Nov 6, 2009 at 6:59 AM, Nathan M. Holden <nathanmhol...@gmail.com>
> wrote:
>>
>> Um... well, I solved my own problem.
>>
>> It turns out that my 20-30 minutes of searching for a way to \definecolor
>> in
>> terms of 0-255 instead of 0.0-1.0 were just 5 minutes short of finding out
>> a
>> way to do it.
>>
>> This is embarrassing.
>
> If you have this problem again, you can use printf :
<snip>

There's also Numeric:
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Numeric.html

Prelude> Numeric.showFFloat (Just 4) 0.001 ""
"0.0010"

-- 
Johannes Laire


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

Message: 8
Date: Fri, 6 Nov 2009 10:03:16 +0100
From: Deniz Dogan <deniz.a.m.do...@gmail.com>
Subject: Re: [Haskell-beginners] if ands
To: Keith Sheppard <keiths...@gmail.com>
Cc: "Nathan M. Holden" <nathanmhol...@gmail.com>,
        beginners@haskell.org
Message-ID:
        <7b501d5c0911060103n3f54d8a7m55da916879c18...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

2009/11/6 Keith Sheppard <keiths...@gmail.com>:
> Also, an nice way to check how evaluation works in ghci is to do something 
> like:
>
>> if False && error "error here" then "it's true" else "it's false"
>
> This expression will evaluate as "it's false" without any "error here"
> error message appearing
>
> On Thu, Nov 5, 2009 at 9:05 PM, Nathan M. Holden
> <nathanmhol...@gmail.com> wrote:
>> If you have an if statement like
>>
>> if (a&&b) then fun else fun'
>>
>> and a is false, does GHC actually bother to check b?
>

Note that Haskell is far from the only programming language that is
smart about this. I actually can't think of a single programming
language implementation that I know of which isn't this smart...

For what it's worth, Haskell (and others) is smart about ORs as well.
In (x || y), y will only be evaluated if x is False.

-- 
Deniz Dogan


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

Message: 9
Date: Fri, 6 Nov 2009 09:15:20 +0000
From: Stephen Tetley <stephen.tet...@gmail.com>
Subject: Re: [Haskell-beginners] N-ary tree search problems
To: Beginners@haskell.org
Message-ID:
        <5fdc56d70911060115o4db4f36bh67dea12c4a270...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hello Ryan

As iaefai said, consistent indentation is the key.

But you also have a problem with the type signature of gtreeMember

gtreeMember :: (Ord a) => a -> Gtree a -> Bool

The part ** Gtree a ** insists that you Gtree has a parametric type -
think of list - [a] where a is type parameter, then concretely a list
can hold any type e.g. [Int] a list of Int, [Bool] a list of Bool, and
so on...

However your Gtree type is not parametric - elements can only be
**String**. So you need to change the signature of gtreeMember so that
it explicitly uses Strings and correct Gtree so it doesn't have a type
parameter:

gtreeMember :: String -> Gtree -> Bool

As there is no longer a type variable you no longer need the type
constraint - Ord a.

That gets things to work, but as you want a general purpose tree this
specialization to Strings for the element isn't really what you need.
Following on you would want to change the Gtree data type to be
polymorphic on element, whence it will have a parametric type
signature:


data Gtree a = Empty
             | Leaf a
             | Node a [Gtree a]
  deriving (Show)

(Trivia - I've changed the style to be have the line for each
alternative constructor start with | which is more conventional, your
tastes may vary. Ideally the pipes should line up with the equals, but
as I'm typing with a variable width font I can't be sure).

Note the ** Gtree a ** in the initial part of the data declaration,
also note the ** Gtree a** in the recursive part of the Node
constructor.


Best wishes

Stephen




> On 2009-11-04, at 7:04 AM, Ryan Temple wrote:

>>
>> data Gtree = Empty |
>>            Leaf String |
>>            Node String [Gtree]
>>            deriving (Show)
>>
>> --Tests if a given string is a member of the tree
>>
>> gtreeMember :: (Ord a) => a -> Gtree a -> Bool
>>  gtreeMember y Empty = False  -- line 17
>>  gtreeMember y (Leaf x) = (x==y)
>>  gtreeMember y (Node x tree)
>>     |x==y = True
>>     |otherwise gtreeMember tree


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

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


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

Reply via email to