mirror of
https://github.com/ilyakooo0/Idris-dev.git
synced 2024-11-15 01:25:05 +03:00
Numeric ranges [a..b], [a,b .. c] and list monad
This commit is contained in:
parent
2a501693ca
commit
02701e7bef
@ -108,6 +108,11 @@ instance MonadPlus Maybe where {
|
||||
mplus Nothing Nothing = Nothing;
|
||||
}
|
||||
|
||||
instance Monad List where {
|
||||
return x = [x];
|
||||
m >>= f = concatMap f m;
|
||||
}
|
||||
|
||||
---- Functors
|
||||
|
||||
class Functor (f : Set -> Set) where {
|
||||
@ -173,6 +178,18 @@ floor x = prim__floatFloor x;
|
||||
ceiling : Float -> Float;
|
||||
ceiling x = prim__floatCeil x;
|
||||
|
||||
|
||||
-- Ranges
|
||||
|
||||
count : Num a => a -> a -> a -> List a;
|
||||
count a inc b = if (a <= b) then (a :: count (a + inc) inc b)
|
||||
else [];
|
||||
|
||||
syntax "[" [start] ".." [end] "]"
|
||||
= count start 1 end;
|
||||
syntax "[" [start] "," [next] ".." [end] "]"
|
||||
= count start (next - start) end;
|
||||
|
||||
---- some basic io
|
||||
|
||||
putStr : String -> IO ();
|
||||
|
@ -38,6 +38,10 @@ map : (a -> b) -> List a -> List b;
|
||||
map f [] = [];
|
||||
map f (x :: xs) = f x :: map f xs;
|
||||
|
||||
concatMap : (a -> List b) -> List a -> List b;
|
||||
concatMap f [] = [];
|
||||
concatMap f (x :: xs) = app (f x) (concatMap f xs);
|
||||
|
||||
mapMaybe : (a -> Maybe b) -> List a -> List b;
|
||||
mapMaybe f [] = [];
|
||||
mapMaybe f (x :: xs) = case f x of {
|
||||
|
@ -8,3 +8,4 @@ Tests:
|
||||
006: Provisional definitions; class resolution in patterns
|
||||
007: Applicative and idiom brackets
|
||||
008: case and pattern matching let
|
||||
009: ranges and list monad
|
||||
|
1
test/test009/expected
Normal file
1
test/test009/expected
Normal file
@ -0,0 +1 @@
|
||||
[(3, (4, 5)), (6, (8, 10)), (5, (12, 13)), (9, (12, 15)), (8, (15, 17)), (12, (16, 20)), (15, (20, 25)), (7, (24, 25)), (10, (24, 26)), (20, (21, 29)), (18, (24, 30)), (16, (30, 34)), (21, (28, 35)), (12, (35, 37)), (15, (36, 39)), (24, (32, 40)), (9, (40, 41)), (27, (36, 45)), (30, (40, 50)), (14, (48, 50))]
|
4
test/test009/run
Executable file
4
test/test009/run
Executable file
@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
idris test009.idr -o test009
|
||||
./test009
|
||||
rm -f test009 test009.ibc
|
12
test/test009/test009.idr
Normal file
12
test/test009/test009.idr
Normal file
@ -0,0 +1,12 @@
|
||||
module main;
|
||||
|
||||
pythag : Int -> List (Int, Int, Int);
|
||||
pythag n = do { z <- [1..n];
|
||||
y <- [1..z];
|
||||
x <- [1..y];
|
||||
if (x * x + y * y == z * z) then (return (x, y, z)) else [];
|
||||
};
|
||||
|
||||
main : IO ();
|
||||
main = print (pythag 50);
|
||||
|
Loading…
Reference in New Issue
Block a user