Skip to content

Instantly share code, notes, and snippets.

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

  • Save yasar11732/5c32906c38a5dcae174ee81557b1a408 to your computer and use it in GitHub Desktop.

Select an option

Save yasar11732/5c32906c38a5dcae174ee81557b1a408 to your computer and use it in GitHub Desktop.
import Data.List
fun1 :: [Integer] -> Integer
fun1 = product . map (subtract 2) . filter even
fun2' :: Integer -> Integer
fun2' 1 = 0
fun2' n | even n = n + fun2' (n `div` 2)
| otherwise = fun2' (3 * n + 1)
collatzNext n | even n = n `div` 2
| otherwise = 3 * n + 1
fun2 :: Integer -> Integer
fun2 = sum . filter even . takeWhile (>1) . iterate collatzNext
xor' :: Bool -> Bool -> Bool
xor' prev next | next = not prev
| otherwise = prev
xor :: [Bool] -> Bool
xor n = foldl xor' False n
map' :: (a -> b) -> [a] -> [b]
map' f = foldr (\x y -> (f x):y) []
toBeCrossed n = sort $ filter (<= n) [i + j + 2*i*j | j <- [1..n], i <- [1..j]]
remove [] _ = []
remove n [] = n
remove (x:xs) (y:ys) | x < y = [x] ++ remove xs (y:ys)
| x == y = remove xs (y:ys)
| otherwise = remove (x:xs) (ys)
sieveOfSundaram :: Integer -> [Integer]
sieveOfSundaram n = let z = toBeCrossed n
in map (\x -> 2*x + 1) . remove [1..n] $ z
main = putStrLn "ok"
@yasar11732

Copy link
Copy Markdown
Author

hw4.hs:10:53: error:
    * No instance for (Fractional Integer)
        arising from a use of `collatzNext'
    * In the first argument of `iterate', namely `collatzNext'
      In the second argument of `(.)', namely `iterate collatzNext'
      In the second argument of `(.)', namely
        `takeWhile (> 1) . iterate collatzNext'
   |
10 | fun2 = sum . filter even . takeWhile (>1) . iterate collatzNext

   |                                                     ^^^^^^^^^^^

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