LoginSignup
1
1

More than 5 years have passed since last update.

[0,0.1..1.0] != (map (/ 10) [0..10]) ?

Posted at

IRC の #Haskell に貼った小ネタ

QuickCheck 用に aribitrary を実装する際などに,本当に [0,0.1..1.0] となる数のリストが欲しいのであれば,そのまま 小数のリテラルを使うのではなく,整数のリストを map (/ 10) で割ると期待通りの値が取れるよ,というお話


if you wants number list of [0,0.1..1.0] , not use it but map (/ 10) [0..10] .

numberliteral-is-liar.hs
-- Is List Literal with Number liar ?
-- 
-- ;-)
--
-- This Sample Code Depend on test-framework, and QuickCheck 2
-- try this
-- cabal install test-framework
-- cabal install test-framework-th
-- cabal install test-framework-quickcheck2
-- cabal install QuickCheck
-- runhaskell numberliteral-is-liar.hs

{-# LANGUAGE TemplateHaskell #-}

module Main ( main ) where

import Test.Framework.TH
import Test.Framework
import Test.Framework.Providers.QuickCheck2
import Test.QuickCheck

main = $(defaultMainGenerator)


contains xs x = any (== x) xs

fixedList = map (/ 10) [1..10]

-- Do you expect to pass this test property ?
prop_floating_number_list =
  forAll ( elements [0,0.1..1.0] ) $ contains fixedList

-- ;-)
prop_true_arbitrary_value =
  forAll ( fmap (/ 10) $ elements [1..10] ) $ contains fixedList

-- Why? try ghci ;-)
-- > map (/ 10) [1..10]
-- > [0,0.1..1.0]
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1