Stern-Brocot sequence: Difference between revisions

Content added Content deleted
mNo edit summary
Line 546: Line 546:
use scripting additions
use scripting additions



------------------ STERN-BROCOT SEQUENCE -----------------


-- sternBrocot :: Generator [Int]
-- sternBrocot :: Generator [Int]
Line 559: Line 561:




-- TEST ------------------------------------------------------------------
--------------------------- TEST -------------------------
on run
on run
set sbs to take(1200, sternBrocot())
set sbs to take(1200, sternBrocot())
Line 610: Line 612:




-- GENERIC ABSTRACTIONS -------------------------------------------------------
------------------------- GENERIC ------------------------



-- Absolute value.
-- Absolute value.
Line 622: Line 623:
end if
end if
end abs
end abs



-- Applied to a predicate and a list, `all` determines if all elements
-- Applied to a predicate and a list, `all` determines if all elements
Line 635: Line 637:
end tell
end tell
end all
end all



-- comparing :: (a -> b) -> (a -> a -> Ordering)
-- comparing :: (a -> b) -> (a -> a -> Ordering)
Line 679: Line 682:
end if
end if
end drop
end drop



-- dropWhile :: (a -> Bool) -> [a] -> [a]
-- dropWhile :: (a -> Bool) -> [a] -> [a]
Line 692: Line 696:
drop(i - 1, xs)
drop(i - 1, xs)
end dropWhile
end dropWhile



-- enumFrom :: a -> [a]
-- enumFrom :: a -> [a]
Line 712: Line 717:
end script
end script
end enumFrom
end enumFrom



-- filter :: (a -> Bool) -> [a] -> [a]
-- filter :: (a -> Bool) -> [a] -> [a]
Line 725: Line 731:
end tell
end tell
end filter
end filter



-- fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
-- fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
Line 741: Line 748:
end script
end script
end fmapGen
end fmapGen



-- fst :: (a, b) -> a
-- fst :: (a, b) -> a
Line 750: Line 758:
end if
end if
end fst
end fst



-- gcd :: Int -> Int -> Int
-- gcd :: Int -> Int -> Int
Line 764: Line 773:
return x
return x
end gcd
end gcd



-- head :: [a] -> a
-- head :: [a] -> a
Line 773: Line 783:
end if
end if
end head
end head



-- iterate :: (a -> a) -> a -> Gen [a]
-- iterate :: (a -> a) -> a -> Gen [a]
Line 823: Line 834:
end if
end if
end min
end min



-- Lift 2nd class handler function into 1st class script wrapper
-- Lift 2nd class handler function into 1st class script wrapper
Line 865: Line 877:
go's |λ|(xs)
go's |λ|(xs)
end nubBy
end nubBy



-- partition :: predicate -> List -> (Matches, nonMatches)
-- partition :: predicate -> List -> (Matches, nonMatches)
Line 883: Line 896:
Tuple(ys, zs)
Tuple(ys, zs)
end partition
end partition



-- showJSON :: a -> String
-- showJSON :: a -> String
Line 911: Line 925:
end if
end if
end showJSON
end showJSON



-- snd :: (a, b) -> b
-- snd :: (a, b) -> b
Line 943: Line 958:
end if
end if
end sortBy
end sortBy



-- tail :: [a] -> [a]
-- tail :: [a] -> [a]
Line 965: Line 981:
end if
end if
end tail
end tail



-- take :: Int -> [a] -> [a]
-- take :: Int -> [a] -> [a]
Line 997: Line 1,014:
end if
end if
end take
end take



-- takeWhile :: (a -> Bool) -> [a] -> [a]
-- takeWhile :: (a -> Bool) -> [a] -> [a]
Line 1,013: Line 1,031:
end if
end if
end takeWhile
end takeWhile



-- takeWhileGen :: (a -> Bool) -> Gen [a] -> [a]
-- takeWhileGen :: (a -> Bool) -> Gen [a] -> [a]
Line 1,026: Line 1,045:
return ys
return ys
end takeWhileGen
end takeWhileGen



-- Tuple (,) :: a -> b -> (a, b)
-- Tuple (,) :: a -> b -> (a, b)
Line 1,031: Line 1,051:
{type:"Tuple", |1|:a, |2|:b, length:2}
{type:"Tuple", |1|:a, |2|:b, length:2}
end Tuple
end Tuple



-- unlines :: [String] -> String
-- unlines :: [String] -> String
Line 1,040: Line 1,061:
str
str
end unlines
end unlines



-- zip :: [a] -> [b] -> [(a, b)]
-- zip :: [a] -> [b] -> [(a, b)]
Line 1,045: Line 1,067:
zipWith(Tuple, xs, ys)
zipWith(Tuple, xs, ys)
end zip
end zip



-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]