Skip to content

Instantly share code, notes, and snippets.

@yasar11732
Last active December 12, 2019 15:18
Show Gist options
  • Select an option

  • Save yasar11732/94f3527f312ca07ccdd91b4965a3afa6 to your computer and use it in GitHub Desktop.

Select an option

Save yasar11732/94f3527f312ca07ccdd91b4965a3afa6 to your computer and use it in GitHub Desktop.
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"
@yasar11732
Copy link
Copy Markdown
Author

hw6.hs:19:50: error:
    * Couldn't match type `Char' with `[Char]'
      Expected type: [String]
        Actual type: String
    * In the second argument of `(:)', namely `show y'
      In the second argument of `take', namely `(show x : show y)'
      In the second argument of `($)', namely `take 20 (show x : show y)'
   |
19 |     show (Cons x y) = concat $ take 20 (show x : show y)

   |                                                  ^^^^^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment