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. 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 <[email protected]>
Subject: [Haskell-beginners] N-ary tree search problems
To: [email protected]
Message-ID:
<[email protected]>
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 <[email protected]>
Subject: Re: [Haskell-beginners] N-ary tree search problems
To: Ryan Temple <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Fri, 6 Nov 2009 00:31:05 -0500
From: "Nathan M. Holden" <[email protected]>
Subject: [Haskell-beginners] Show Floats
To: [email protected]
Message-ID: <[email protected]>
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 <[email protected]>
Subject: Re: [Haskell-beginners] Show Floats
To: "Nathan M. Holden" <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 5
Date: Fri, 6 Nov 2009 00:59:26 -0500
From: "Nathan M. Holden" <[email protected]>
Subject: re: [Haskell-beginners] Show Floats
To: [email protected]
Message-ID: <[email protected]>
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 <[email protected]>
Subject: Re: [Haskell-beginners] Show Floats
To: "Nathan M. Holden" <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
On Fri, Nov 6, 2009 at 6:59 AM, Nathan M. Holden <[email protected]>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
> [email protected]
> 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 <[email protected]>
Subject: Re: [Haskell-beginners] Show Floats
To: David Virebayre <[email protected]>
Cc: "Nathan M. Holden" <[email protected]>,
[email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Nov 6, 2009 at 9:32 AM, David Virebayre
<[email protected]> wrote:
> On Fri, Nov 6, 2009 at 6:59 AM, Nathan M. Holden <[email protected]>
> 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 <[email protected]>
Subject: Re: [Haskell-beginners] if ands
To: Keith Sheppard <[email protected]>
Cc: "Nathan M. Holden" <[email protected]>,
[email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
2009/11/6 Keith Sheppard <[email protected]>:
> 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
> <[email protected]> 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 <[email protected]>
Subject: Re: [Haskell-beginners] N-ary tree search problems
To: [email protected]
Message-ID:
<[email protected]>
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
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 17, Issue 6
****************************************