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: How to convert a float or double number into a string?
(Stephen Tetley)
2. Re: recursive and non recursive functions (Brent Yorgey)
3. Re: How to convert a float or double number into a string?
(Oscar Benjamin)
4. Re: using the joker "_" on a constructor (TP)
----------------------------------------------------------------------
Message: 1
Date: Wed, 18 Sep 2013 17:47:00 +0100
From: Stephen Tetley <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to convert a float or double
number into a string?
Message-ID:
<CAB2TPRDNRywcdJUCRE9xGRrtDUhdS-WJnxBMmfb3gfe5=2e...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
It you don't mind truncation from the outset there is Data.Fixed.
On 18 September 2013 17:16, David McBride <[email protected]> wrote:
> Show is not truncating anything. Show never had those digits to read from
> in the first place. The problem is that once you convert to floats or
> doubles, the number will be truncated. They do not have infinite
> precision. You cannot pass a literal float and have it go to infinite
> precision.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20130918/76b1c250/attachment-0001.html>
------------------------------
Message: 2
Date: Wed, 18 Sep 2013 15:31:59 -0400
From: Brent Yorgey <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] recursive and non recursive functions
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
On Wed, Sep 18, 2013 at 12:41:54AM -0600, Dan Lior wrote:
> The following snippet is from a recent stack overflow post (by someone else).
>
>
>
> 7
> down vote
> accept
> I'd like to mention an important distinction:
>
> cyclic' = [0,1] ++ cyclic'
>
> cyclic'' = [0,1] ++ x
> where x = cyclic''
> These two functions are recursive. But
>
> cyclic = let x = 0 : y
> y = 1 : x
> in x
> is not! It uses x internally, which is recursive, but the whole cyclic isn't.
> This is also why they're different when compiled into the core language.
>
> This has some important practical implications, namely that recursive
> functions can't be inlined, but non-recursive can.
>
> Presumably,
>
> cyclic = let
> x = [0,1] ++ y
> y = x
> in x
>
> is another "nonrecursive function".
>
> I don't understand the distinction. I imagine that it is related to the
> let?in clause, but I don't think that I understand that either. Can someone
> try to clarify this ?
>
> dan
The distinction is simply this: given a definition
foo = ...
does foo show up in the ..., i.e. on the right-hand-side of its own
definition, or not? If foo shows up in its own definition, it is
recursive. If not, it is not recursive. E.g. it is easy to see that in
cyclic = let x = 0 : y
y = 1 : x
in x
'cyclic' does not show up on the right-hand side of its own
definition.
-Brent
------------------------------
Message: 3
Date: Wed, 18 Sep 2013 23:46:27 +0100
From: Oscar Benjamin <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] How to convert a float or double
number into a string?
Message-ID:
<CAHVvXxT6RT-QDHxoQf66YnYcYJCaxs92VKA-Ro3=x3rzo9t...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On 18 September 2013 16:07, yi lu <[email protected]> wrote:
> You are right! I hope to input a number, for example 123, and output its
> text "one hundred and twenty-three".
> So, for 1.23456789012345678901, I want the result is "one point two three
> four five six ...(something omitted)".
You should probably explain this at the beginning!
> I can define a funciton, say "toText", to preform this action.
> In ghci, I can use like this.
> Prelude>toText 123.45
> "one hundred and twenty-three point four five"
>
> However, in this function, I have to read this number as String(originally a
> number, now "123"), and make it to words(String) like "one two three".
> But for a float number, it will not work very well.
> Prelude>toText 1.23456789012345678901
> "(a truncated answer)"
>
> This confuses me!
I suspect you don't yet understand what a float really is:
http://en.wikipedia.org/wiki/Single-precision_floating-point_format
If you do understand what a single (or double) precision float is then
consider this: most values cannot be exactly represented by the float
type. When you ask Haskell to create a float with some value given as
a decimal string it will usually not return a float with the exact
value you asked for since this is often impossible. Instead it will
only promise to give you a "close" value.
For your problem I would just use string manipulation. Testing for
n<1000000 is just as easy as testing the length of a decimal string.
Conversion to float or Rational is usually only worthwhile if you're
intending to do some arithmetic which you're not. So your input is a
string your output should be a string and you're not doing arithmetic:
you may as well view this as a text processing problem and just write
functions that work with strings.
Oscar
------------------------------
Message: 4
Date: Thu, 19 Sep 2013 12:44:58 +0200
From: TP <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] using the joker "_" on a constructor
Message-ID: <[email protected]>
Content-Type: text/plain; charset="UTF-8"
Christian Maeder wrote:
> In order to avoid your duplicate code consider to define "Foo" as follows:
>
> data Foo = F FB Int
> data FB = Foo | Bar
>
> f (F _ n) = even n
Thanks a lot. Your solution, also given by Kim-Ee Yeoh, is the one I prefer:
? the common features are factorized in the type;
? the values are deconstructed by pattern matching maybe in a clearer way;
? `_` is explicitly used, making clear in a blink of an eye that we have
some common behavior over several types.
TP
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 63, Issue 27
*****************************************