
Hi, In other weak-type language, `round i == i` would work. But in haskell, what should I do? Thanks. -- 竹密岂妨流水过 山高哪阻野云飞

Hi,
Here is an example:
let l = fun num in
if isIntegral l
then l
else 0
How to do the isIntegral thing?
On Tue, Sep 29, 2009 at 1:58 PM, Magicloud Magiclouds
Hi, In other weak-type language, `round i == i` would work. But in haskell, what should I do? Thanks. -- 竹密岂妨流水过 山高哪阻野云飞
-- 竹密岂妨流水过 山高哪阻野云飞

Magicloud,
There are numerous ways to construct such a function. Hoogling
"(Fractional a, Integral b) => a -> b" brings up a host of functions
that would be of use here in combination with fromIntegral.
Thomas
On Mon, Sep 28, 2009 at 11:11 PM, Magicloud Magiclouds
Hi, Here is an example: let l = fun num in if isIntegral l then l else 0 How to do the isIntegral thing?
On Tue, Sep 29, 2009 at 1:58 PM, Magicloud Magiclouds
wrote: Hi, In other weak-type language, `round i == i` would work. But in haskell, what should I do? Thanks. -- 竹密岂妨流水过 山高哪阻野云飞
-- 竹密岂妨流水过 山高哪阻野云飞
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Use properFraction: http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Aprop...
Hi, In other weak-type language, `round i == i` would work. But in haskell, what should I do? Thanks. -- 竹å¯å²å¦¨æµæ°´è¿ å±±é«åªé»éäºé£ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

It never matches to (_, 0.0)....
I mean
case properFraction l of
(_, 0) -> l
_ -> 0 -- always goes here.
On Tue, Sep 29, 2009 at 2:18 PM, Jimmy Hartzell
Use properFraction: http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Aprop...
Hi, In other weak-type language, `round i == i` would work. But in haskell, what should I do? Thanks. -- 竹密岂妨流水过 山高哪阻野云飞 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- 竹密岂妨流水过 山高哪阻野云飞

On Mon, Sep 28, 2009 at 11:35 PM, Magicloud Magiclouds
It never matches to (_, 0.0).... I mean case properFraction l of (_, 0) -> l _ -> 0 -- always goes here.
Odd, it works fine for me. f x = case properFraction x of (_,0) -> True _ -> False *Main> f 5 True *Main> f 5.5 False *Main> f 4.0 True *Main> f 4.00000001 False Thomas

The original code is
givenSum num = map (\a ->
let l = (sqrt $ fromIntegral (a * a + 2 + 2 *
num)) - (fromIntegral a) in
case properFraction l of
(_, 0) ->
True
_ ->
False
) $ take num [1..]
:t l is (Floating a) => a
Well, in ghci
*Main> givenSum 10
[False,False,False,False,False,False,False,False,False,False]
On Tue, Sep 29, 2009 at 2:45 PM, Thomas DuBuisson
On Mon, Sep 28, 2009 at 11:35 PM, Magicloud Magiclouds
wrote: It never matches to (_, 0.0).... I mean case properFraction l of (_, 0) -> l _ -> 0 -- always goes here.
Odd, it works fine for me.
f x = case properFraction x of (_,0) -> True _ -> False
*Main> f 5 True *Main> f 5.5 False *Main> f 4.0 True *Main> f 4.00000001 False
Thomas
-- 竹密岂妨流水过 山高哪阻野云飞

Unless I missed something, the function in question is:
sqrt (a * a + 2 + 2 * num) - fromIntegral a
where num = 10
1 -> sqrt (1 * 1 + 2 + 2 * 10) - 1 -> sqrt (1 + 2 + 20) - 1 -> sqrt
(23) - 1 -> 3.79xxxxx
the fractional will only ever come from the sqrt function. Do any of
the following actually look like square values to you?
26
31
38
47
58
71
86
103
122
IMO, the code works and your expectations are a bit off.
Thomas
On Mon, Sep 28, 2009 at 11:54 PM, Magicloud Magiclouds
The original code is givenSum num = map (\a -> let l = (sqrt $ fromIntegral (a * a + 2 + 2 * num)) - (fromIntegral a) in case properFraction l of (_, 0) -> True _ -> False ) $ take num [1..] :t l is (Floating a) => a Well, in ghci *Main> givenSum 10 [False,False,False,False,False,False,False,False,False,False]
On Tue, Sep 29, 2009 at 2:45 PM, Thomas DuBuisson
wrote: On Mon, Sep 28, 2009 at 11:35 PM, Magicloud Magiclouds
wrote: It never matches to (_, 0.0).... I mean case properFraction l of (_, 0) -> l _ -> 0 -- always goes here.
Odd, it works fine for me.
f x = case properFraction x of (_,0) -> True _ -> False
*Main> f 5 True *Main> f 5.5 False *Main> f 4.0 True *Main> f 4.00000001 False
Thomas
-- 竹密岂妨流水过 山高哪阻野云飞

Of course them are not. But that is why I need the detector....
2009/9/29 Thomas DuBuisson
Unless I missed something, the function in question is:
sqrt (a * a + 2 + 2 * num) - fromIntegral a where num = 10
1 -> sqrt (1 * 1 + 2 + 2 * 10) - 1 -> sqrt (1 + 2 + 20) - 1 -> sqrt (23) - 1 -> 3.79xxxxx
the fractional will only ever come from the sqrt function. Do any of the following actually look like square values to you?
26 31 38 47 58 71 86 103 122
IMO, the code works and your expectations are a bit off.
Thomas
On Mon, Sep 28, 2009 at 11:54 PM, Magicloud Magiclouds
wrote: The original code is givenSum num = map (\a -> let l = (sqrt $ fromIntegral (a * a + 2 + 2 * num)) - (fromIntegral a) in case properFraction l of (_, 0) -> True _ -> False ) $ take num [1..] :t l is (Floating a) => a Well, in ghci *Main> givenSum 10 [False,False,False,False,False,False,False,False,False,False]
On Tue, Sep 29, 2009 at 2:45 PM, Thomas DuBuisson
wrote: On Mon, Sep 28, 2009 at 11:35 PM, Magicloud Magiclouds
wrote: It never matches to (_, 0.0).... I mean case properFraction l of (_, 0) -> l _ -> 0 -- always goes here.
Odd, it works fine for me.
f x = case properFraction x of (_,0) -> True _ -> False
*Main> f 5 True *Main> f 5.5 False *Main> f 4.0 True *Main> f 4.00000001 False
Thomas
-- 竹密岂妨流水过 山高哪阻野云飞
-- 竹密岂妨流水过 山高哪阻野云飞

Am Dienstag 29 September 2009 09:02:19 schrieb Thomas DuBuisson:
Unless I missed something, the function in question is:
sqrt (a * a + 2 + 2 * num) - fromIntegral a where num = 10
1 -> sqrt (1 * 1 + 2 + 2 * 10) - 1 -> sqrt (1 + 2 + 20) - 1 -> sqrt (23) - 1 -> 3.79xxxxx
the fractional will only ever come from the sqrt function. Do any of the following actually look like square values to you?
26 31 38 47 58 71 86 103 122
IMO, the code works and your expectations are a bit off.
Quite. *MMlouds> givenSum 11 [True,False,False,False,True,False,False,False,False,False,False] The code tests whether a*a+2*(num+1) is a square, equivalently, whether there's a b such that 2*(num+1) == b^2 - a^2 Now, the difference of two squares is either odd or a multiple of 4. 2*(num+1) is never odd, so it must be a multiple of 4, i.e. num must be odd for any number of the form a^2 + 2*(num+1) to be a square. isIntegral x = snd (properFraction x) == 0 works for rational x and for small enough doubles/floats, but beware: *MMlouds> [n | n <- [1 .. 100], not $ isIntegral (sqrt $ 4^n+222)] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
Thomas
On Mon, Sep 28, 2009 at 11:54 PM, Magicloud Magiclouds
wrote: *Main> givenSum 10 [False,False,False,False,False,False,False,False,False,False]

Did you test the properFraction-based code in isolation? If code is broken, it's important to figure out which part of it is broken. Also, this function is not divided into constituent parts, but is a long unruly mess. Dividing it into parts would make it much much more readable, and you would then be able to test the parts individually. Jimmy
The original code is givenSum num = map (\a -> let l = (sqrt $ fromIntegral (a * a + 2 + 2 * num)) - (fromIntegral a) in case properFraction l of (_, 0) -> True _ -> False ) $ take num [1..] :t l is (Floating a) => a Well, in ghci *Main> givenSum 10 [False,False,False,False,False,False,False,False,False,False]
On Tue, Sep 29, 2009 at 2:45 PM, Thomas DuBuisson
wrote: On Mon, Sep 28, 2009 at 11:35 PM, Magicloud Magiclouds
wrote: It never matches to (_, 0.0).... I mean case properFraction l of  (_, 0) -> l  _ -> 0 -- always goes here.
Odd, it works fine for me.
f x =     case properFraction x of         (_,0) -> True         _   -> False
*Main> f 5 True *Main> f 5.5 False *Main> f 4.0 True *Main> f 4.00000001 False
Thomas
-- 竹å¯å²å¦¨æµæ°´è¿ å±±é«åªé»éäºé£

The properFaction part is correct. So I posted the whole code, since
"isInteger" should accept any reasonable incoming types. Well, in this
one situation, it does not. And I cannot figure out why....
On Tue, Sep 29, 2009 at 3:07 PM, Jimmy Hartzell
Did you test the properFraction-based code in isolation? If code is broken, it's important to figure out which part of it is broken. Also, this function is not divided into constituent parts, but is a long unruly mess. Dividing it into parts would make it much much more readable, and you would then be able to test the parts individually.
Jimmy
The original code is givenSum num = map (\a -> let l = (sqrt $ fromIntegral (a * a + 2 + 2 * num)) - (fromIntegral a) in case properFraction l of (_, 0) -> True _ -> False ) $ take num [1..] :t l is (Floating a) => a Well, in ghci *Main> givenSum 10 [False,False,False,False,False,False,False,False,False,False]
On Tue, Sep 29, 2009 at 2:45 PM, Thomas DuBuisson
wrote: On Mon, Sep 28, 2009 at 11:35 PM, Magicloud Magiclouds
wrote: It never matches to (_, 0.0).... I mean case properFraction l of (_, 0) -> l _ -> 0 -- always goes here.
Odd, it works fine for me.
f x = case properFraction x of (_,0) -> True _ -> False
*Main> f 5 True *Main> f 5.5 False *Main> f 4.0 True *Main> f 4.00000001 False
Thomas
-- 竹密岂妨流水过 山高哪阻野云飞
-- 竹密岂妨流水过 山高哪阻野云飞

On Tue, Sep 29, 2009 at 9:13 AM, Magicloud Magiclouds
The properFaction part is correct. So I posted the whole code, since "isInteger" should accept any reasonable incoming types. Well, in this one situation, it does not. And I cannot figure out why....
Floating point gives a lot of small errors, so maybe you shouldn't test your fractional part to be = 0, but you should test it to be close enough.

*Should* isInteger be returning True for any numbers generated by this code? If so, can you simplify this test down to that example, so that it's obvious what the test should do, and that it's not doing it (if it in fact is not doing as it should)? In any case, it would help to divide this block of code into more manageable pieces. isInteger should have the type Fractional a => a -> Boolean.
The properFaction part is correct. So I posted the whole code, since "isInteger" should accept any reasonable incoming types. Well, in this one situation, it does not. And I cannot figure out why....
On Tue, Sep 29, 2009 at 3:07 PM, Jimmy Hartzell
wrote: Did you test the properFraction-based code in isolation? If code is broken, it's important to figure out which part of it is broken. Also, this function is not divided into constituent parts, but is a long unruly mess. Dividing it into parts would make it much much more readable, and you would then be able to test the parts individually.
Jimmy
The original code is givenSum num = map (\a ->            let l = (sqrt $ fromIntegral (a * a + 2 + 2 * num)) - (fromIntegral a) in            case properFraction l of             (_, 0) ->              True             _ ->              False           ) $ take num [1..] :t l is (Floating a) => a Well, in ghci *Main> givenSum 10 [False,False,False,False,False,False,False,False,False,False]
On Tue, Sep 29, 2009 at 2:45 PM, Thomas DuBuisson
wrote: On Mon, Sep 28, 2009 at 11:35 PM, Magicloud Magiclouds
wrote: It never matches to (_, 0.0).... I mean case properFraction l of  (_, 0) -> l  _ -> 0 -- always goes here.
Odd, it works fine for me.
f x =     case properFraction x of         (_,0) -> True         _   -> False
*Main> f 5 True *Main> f 5.5 False *Main> f 4.0 True *Main> f 4.00000001 False
Thomas
-- 竹å¯å²å¦¨æµæ°´è¿ å±±é«åªé»éäºé£
-- 竹å¯å²å¦¨æµæ°´è¿ å±±é«åªé»éäºé£

Resolved. As Thomas said, mixing up sure is a bad thing. But then I
have to name so many meanless (at least I think) computing process....
On Tue, Sep 29, 2009 at 3:32 PM, Jimmy Hartzell
*Should* isInteger be returning True for any numbers generated by this code? If so, can you simplify this test down to that example, so that it's obvious what the test should do, and that it's not doing it (if it in fact is not doing as it should)? In any case, it would help to divide this block of code into more manageable pieces.
isInteger should have the type Fractional a => a -> Boolean.
The properFaction part is correct. So I posted the whole code, since "isInteger" should accept any reasonable incoming types. Well, in this one situation, it does not. And I cannot figure out why....
On Tue, Sep 29, 2009 at 3:07 PM, Jimmy Hartzell
wrote: Did you test the properFraction-based code in isolation? If code is broken, it's important to figure out which part of it is broken. Also, this function is not divided into constituent parts, but is a long unruly mess. Dividing it into parts would make it much much more readable, and you would then be able to test the parts individually.
Jimmy
The original code is givenSum num = map (\a -> let l = (sqrt $ fromIntegral (a * a + 2 + 2 * num)) - (fromIntegral a) in case properFraction l of (_, 0) -> True _ -> False ) $ take num [1..] :t l is (Floating a) => a Well, in ghci *Main> givenSum 10 [False,False,False,False,False,False,False,False,False,False]
On Tue, Sep 29, 2009 at 2:45 PM, Thomas DuBuisson
wrote: On Mon, Sep 28, 2009 at 11:35 PM, Magicloud Magiclouds
wrote: It never matches to (_, 0.0).... I mean case properFraction l of (_, 0) -> l _ -> 0 -- always goes here.
Odd, it works fine for me.
f x = case properFraction x of (_,0) -> True _ -> False
*Main> f 5 True *Main> f 5.5 False *Main> f 4.0 True *Main> f 4.00000001 False
Thomas
-- 竹密岂妨流水过 山高哪阻野云飞
-- 竹密岂妨流水过 山高哪阻野云飞
-- 竹密岂妨流水过 山高哪阻野云飞

On Tue, Sep 29, 2009 at 2:30 AM, Magicloud Magiclouds
Resolved. As Thomas said, mixing up sure is a bad thing. But then I have to name so many meanless (at least I think) computing process....
That is the primary challenge of writing readable code: identifying the meaningful parts. You can separate out a function into its parts in many, many ways, but only a few of them will have parts that are comprehensible in isolation. Your original question asked "how to decide if a number is an integer". Surely you would have loved it if isInteger were a library function. It has a nice, simple meaning, that is completely independent of what the surrounding code is doing. It is pretty easy to name. These things often line up. They indicate that the function is most easily understood outside of its context, as an atomic building block. It is an ideal candidate for a small function. An example of a bad constituent function from your example would be this one: huh a = fromIntegral (a * a + 2 + 2 * a) What does it mean, why are you doing it? It would be absurd to ask for this as a library function in any library. Who knows what it could possibly be named, except something inane like doArithmetic. This function is most easily understood in its context; the function that is trying to find integral values of this expression. Spending a lot of time and thought about how to break up your functions in the most understandable way will make you a better engineer. In fact, it's a quality I find missing in many professional programmers... unfortunately. Luke

$ ghci Prelude> let isInteger' l = case properFraction l of { (_,0) -> 1; _ -> 0 } Prelude> isInteger' 2.0 1 Prelude> isInteger' 1.9 0 Do you really get 1? For what input types/values? Although I would write: isInteger = (== 0) . snd . properFraction
It never matches to (_, 0.0).... I mean case properFraction l of (_, 0) -> l _ -> 0 -- always goes here.
On Tue, Sep 29, 2009 at 2:18 PM, Jimmy Hartzell
wrote: Use properFraction: http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Aprop...
Hi,  In other weak-type language, `round i == i` would work. But in haskell, what should I do? Thanks. -- 竹å¯å²å¦¨æµæ°´è¿ å±±é«åªé»éäºé£ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- 竹å¯å²å¦¨æµæ°´è¿ å±±é«åªé»éäºé£

On Tue, 29 Sep 2009, Magicloud Magiclouds wrote:
Hi, In other weak-type language, `round i == i` would work. But in haskell, what should I do? Thanks.
Am I right, that you want to check whether a number is a square number? http://www.haskell.org/haskellwiki/Generic_number_type#isSquare
participants (7)
-
Daniel Fischer
-
david48
-
Henning Thielemann
-
Jimmy Hartzell
-
Luke Palmer
-
Magicloud Magiclouds
-
Thomas DuBuisson