Last active
December 12, 2019 15:18
-
-
Save yasar11732/94f3527f312ca07ccdd91b4965a3afa6 to your computer and use it in GitHub Desktop.
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
| fib :: Integer -> Integer | |
| fib 0 = 0 | |
| fib 1 = 1 | |
| fib n = fib (n-1) + fib (n - 2) | |
| fibs1 :: [Integer] | |
| fibs1 = map fib [0..] | |
| fibs2 :: [Integer] | |
| fibs2 = [0,1,1] ++ zipWith (+) (drop 1 fibs2) (drop 2 fibs2) | |
| data Stream a = Cons a (Stream a) | |
| streamToList :: Stream a -> [a] | |
| streamToList (Cons x y) = x:streamToList y | |
| join :: String -> [String] -> String | |
| join sep [] = "" | |
| join sep [x] = x | |
| join sep (x:xs) = x ++ sep ++ join sep xs | |
| joincomma = join "," | |
| instance Show a => Show (Stream a) where | |
| show a = "[" ++ joincomma (take 20 $ map show (streamToList a)) ++ "...]" | |
| dropStream :: Int -> Stream a -> Stream a | |
| dropStream 0 n = n | |
| dropStream x (Cons a b) = dropStream (x-1) b | |
| takeStream :: Int -> Stream a -> Stream a | |
| takeStream n (Cons x xs) = Cons x (takeStream (n-1) xs) | |
| zipWithStream :: (a -> b -> c) -> Stream a -> Stream b -> Stream c | |
| zipWithStream f (Cons a resta) (Cons b restb) = Cons (f a b) (zipWithStream f resta restb) | |
| fibs3 :: Stream Integer | |
| fibs3 = Cons 0 (Cons 1 (Cons 1 (zipWithStream (+) (dropStream 1 fibs3) (dropStream 2 fibs3)))) | |
| streamRepeat :: a -> Stream a | |
| streamRepeat a = Cons a (streamRepeat a) | |
| streamMap :: (a -> b) -> Stream a -> Stream b | |
| streamMap f (Cons xval xlist) = Cons (f xval) (streamMap f xlist) | |
| streamFromSeed :: (a -> a) -> a -> Stream a | |
| streamFromSeed f start = Cons start (streamFromSeed f (f start)) | |
| interleaveStreams :: Stream a -> Stream a -> Stream a | |
| interleaveStreams (Cons afirst arest) (Cons bfirst brest) = | |
| Cons afirst (Cons bfirst (interleaveStreams arest brest)) | |
| nats :: Stream Integer | |
| nats = streamFromSeed succ 0 | |
| maxPowerOfTwoDivisor :: Integer -> Integer | |
| maxPowerOfTwoDivisor n | |
| | n `mod` 2 == 0 = 1 + maxPowerOfTwoDivisor (n `div` 2) | |
| | otherwise = 0 | |
| ruler :: Stream Integer | |
| ruler = interleaveStreams (streamRepeat 0) (streamMap succ ruler) | |
| main = putStrLn "placeholder" |
Author
yasar11732
commented
Dec 12, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment