mirror of
https://github.com/idris-lang/Idris2.git
synced 2024-11-24 06:52:19 +03:00
feat: even and odd for Nat and Integral (#3021)
This commit is contained in:
parent
1fa638494d
commit
c7abb148e8
@ -175,6 +175,7 @@
|
||||
|
||||
* Adds `leftmost` and `rightmost` to `Control.Order`, a generalisation of `min` and `max`.
|
||||
|
||||
* Adds `even` and `odd` to `Data.Integral`.
|
||||
* `Eq` and `Ord` implementations for `Fin n` now run in constant time.
|
||||
|
||||
* Adds `getTermCols` and `getTermLines` to the base library. They return the
|
||||
|
9
libs/base/Data/Integral.idr
Normal file
9
libs/base/Data/Integral.idr
Normal file
@ -0,0 +1,9 @@
|
||||
module Data.Integral
|
||||
|
||||
export
|
||||
even : Integral n => Eq n => n -> Bool
|
||||
even n = n `mod` 2 == 0
|
||||
|
||||
export
|
||||
odd : Integral n => Eq n => n -> Bool
|
||||
odd = not . even
|
52
tests/base/data_integral/Integral.idr
Normal file
52
tests/base/data_integral/Integral.idr
Normal file
@ -0,0 +1,52 @@
|
||||
import Data.Integral
|
||||
|
||||
Cases : Type
|
||||
Cases = List Bool
|
||||
|
||||
natEvenCases : Cases
|
||||
natEvenCases = [
|
||||
even 0,
|
||||
even 2,
|
||||
even 4,
|
||||
even Z,
|
||||
even (S (S Z))
|
||||
]
|
||||
|
||||
natOddCases : Cases
|
||||
natOddCases = [
|
||||
odd 1,
|
||||
odd 3,
|
||||
odd 5,
|
||||
odd (S Z),
|
||||
odd (S (S (S Z)))
|
||||
]
|
||||
|
||||
integralEvenCases : Cases
|
||||
integralEvenCases = [
|
||||
even (-4),
|
||||
even (-2),
|
||||
even 0,
|
||||
even 2,
|
||||
even 4
|
||||
]
|
||||
|
||||
integralOddCases : Cases
|
||||
integralOddCases = [
|
||||
odd (-5),
|
||||
odd (-3),
|
||||
odd (-1),
|
||||
odd 1,
|
||||
odd 3,
|
||||
odd 5
|
||||
]
|
||||
|
||||
main : IO ()
|
||||
main = do
|
||||
printLn "Nat Even"
|
||||
printLn natEvenCases
|
||||
printLn "Nat Odd"
|
||||
printLn natOddCases
|
||||
printLn "Integral Even"
|
||||
printLn integralEvenCases
|
||||
printLn "Integral Odd"
|
||||
printLn integralOddCases
|
10
tests/base/data_integral/expected
Normal file
10
tests/base/data_integral/expected
Normal file
@ -0,0 +1,10 @@
|
||||
1/1: Building Integral (Integral.idr)
|
||||
Main> "Nat Even"
|
||||
[True, True, True, True, True]
|
||||
"Nat Odd"
|
||||
[True, True, True, True, True]
|
||||
"Integral Even"
|
||||
[True, True, True, True, True]
|
||||
"Integral Odd"
|
||||
[True, True, True, True, True, True]
|
||||
Main> Bye for now!
|
2
tests/base/data_integral/input
Normal file
2
tests/base/data_integral/input
Normal file
@ -0,0 +1,2 @@
|
||||
:exec main
|
||||
:q
|
3
tests/base/data_integral/run
Normal file
3
tests/base/data_integral/run
Normal file
@ -0,0 +1,3 @@
|
||||
rm -rf build
|
||||
|
||||
$1 --no-color --console-width 0 --no-banner Integral.idr < input
|
Loading…
Reference in New Issue
Block a user