[Haskell-cafe] How to decide if a number is an integer?

Jimmy Hartzell jim at shareyourgifts.net
Tue Sep 29 03:07:05 EDT 2009


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
> <thomas.dubuisson at gmail.com> wrote:
>> On Mon, Sep 28, 2009 at 11:35 PM, Magicloud Magiclouds
>> <magicloud.magiclouds at gmail.com> 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
>>
>
>
>
> --
> 竹密岂妨流水过
> 山高哪阻野云飞
>




More information about the Haskell-Cafe mailing list