Created
December 18, 2018 04:01
-
-
Save dmwit/8af20541a8b172984d5aa80057534124 to your computer and use it in GitHub Desktop.
testing tab settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
import System.Environment | |
data SizedTree a = Node Integer a [SizedTree a] deriving (Eq, Ord, Read, Show) | |
type Stream a = [SizedTree a] | |
indexT :: Integer -> SizedTree a -> a | |
indexT 0 (Node _ v _) = v | |
indexT n (Node _ _ vs) = indexS (n-1) vs | |
indexS :: Integer -> Stream a -> a | |
indexS n (t@(Node m _ _):ts) | |
| n < m = indexT n t | |
| otherwise = indexS (n-m) ts | |
fromListS :: [a] -> Stream a | |
fromListS = goS [1] where | |
goS ns@(n:_) xs = goT ns b : goS (2*n+1:ns) e where | |
(b, e) = genericSplitAt n xs | |
goT [1] [x] = Node 1 x [] | |
goT (n:ns@(n':_)) (x:xs) = Node n x [goT ns b, goT ns e] where | |
(b, e) = genericSplitAt n' xs | |
example :: Integer -> Integer | |
example n = sum [indexS i vs | i <- [0..n]] where vs = fromListS [0..] | |
main = getArgs >>= print . example . read . head |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment