Ad
Fundamentals
Strings
Data Types
Algorithms
Logic

As you see, S combinator is quite useful.
Sadly, the type itself is quite long.

  • Split the test cases in 2.
Code
Diff
  • module Code (palindrome) where
    
    palindrome :: Eq a => [a] -> Bool -- Truly Parametric
    palindrome = (==) <*> reverse
    • #include <string>
    • bool isPalindrome(const std::string &word) {
    • return std::string{word.rbegin(),word.rend()}==word;
    • }
    • module Code (palindrome) where
    • palindrome :: Eq a => [a] -> Bool -- Truly Parametric
    • palindrome = (==) <*> reverse

This should be O(logn) algorithm modulo multiplication of biginteger multiplication.
Wish biginteger multiplication was O(n) for n digits, then this would be O(n log n)...

EDIT: Tested on ghci, it takes more time to print fib 1000000 than to compute it.

Code
Diff
  • module Example where
    
    import Data.Semigroup (stimesMonoid)
    
    -- | Fib deciding pair
    data FibPair = FibPair !Integer !Integer
    instance Semigroup FibPair where -- Combines m-th and n-th to (m+n)-th
      FibPair fm_1 fm <> FibPair fn_1 fn =
        FibPair (fm * fn + fm_1 * fn_1) (fm * (fn + fn_1) + fm_1 * fn)
    instance Monoid FibPair where
      mempty = FibPair 1 0 -- (-1)-th and 0-th
    
    fib :: Integer -> Integer
    fib = (\(FibPair _ n) -> n) . (`stimesMonoid` FibPair 0 1)
    • module Example where
    • import Data.Semigroup (Endo(..),stimes)
    • import Data.Semigroup (stimesMonoid)
    • -- | Fib deciding pair
    • data FibPair = FibPair !Integer !Integer
    • instance Semigroup FibPair where -- Combines m-th and n-th to (m+n)-th
    • FibPair fm_1 fm <> FibPair fn_1 fn =
    • FibPair (fm * fn + fm_1 * fn_1) (fm * (fn + fn_1) + fm_1 * fn)
    • instance Monoid FibPair where
    • mempty = FibPair 1 0 -- (-1)-th and 0-th
    • fib :: Integer -> Integer
    • fib = fst . (`appEndo` (0,1)) . (`stimes` Endo ( \ (a,b) -> (b,a+b) ))
    • fib = (\(FibPair _ n) -> n) . (`stimesMonoid` FibPair 0 1)
Fundamentals
Functional Programming
Declarative Programming
Programming Paradigms
Higher-order Functions
Functions
Control Flow
Basic Language Features
  • Added 3 more test cases
  • Follwing problem description:
    Counting from 1 to n, then back to 1
  • Wait, why am I doing this?
Code
Diff
  • module NumberPrint where
    
    numberprint :: Int -> Integer
    numberprint n = read . concat $ show <$> [1 .. pred n] <> [n, pred n .. 1]
    • module NumberPrint where
    • numberprint :: Int -> Integer
    • numberprint = read . (>>= id) . ((++) <*> (reverse . init)) . (\x -> show <$> [1..x])
    • numberprint n = read . concat $ show <$> [1 .. pred n] <> [n, pred n .. 1]
Games
Algorithms
Logic
Combinators
Functional Programming
Combinatory Logic
Functions
Declarative Programming
Programming Paradigms
Computational Science
Theoretical Computer Science
Control Flow
Basic Language Features
Fundamentals
Design Patterns

Combinators are useful.
I'd like a safe tail function though.
EDIT: drop could be used instead.

<*> is S combinator.

  • For A <*> B, parameter is piped to both A and B
  • and then, they are applied
  • i.e. A <*> B = \x -> (A x) (B x)
Code
Diff
  • module Platforms where
    
    platforms :: [Integer] -> Integer
    platforms = sum . fmap abs . (zipWith (-) <*> drop 1)
    • module Platforms where
    • platforms :: [Integer] -> Integer
    • platforms (x:y:xs) = abs (x - y) + platforms (y:xs)
    • platforms _ = 0
    • platforms = sum . fmap abs . (zipWith (-) <*> drop 1)
Iterators
Control Flow
Object-oriented Programming
Basic Language Features
Fundamentals
Programming Paradigms
Monads
Data Structures
Functional Programming

Pattern matching on number is lame, so implemented using a list.

  • Pattern
    • on each step, 2 is multipled on each element
    • 1 is added in between
  • (iterate step initial !!) could be used to get result of application n times
  • List monad is great!
  • View pattern is unnecessary now.
Code
Diff
  • module Code (powerfulArray) where
    
    import Data.List
    
    powerfulArray :: Int -> [Int]
    powerfulArray = (iterate next [] !!) where next l = 1 : (do n <- l; [2 * n, 1])
    • {-# Language ViewPatterns #-}
    • module Code (powerfulArray) where
    • import Data.List
    • powerfulArray :: Int -> [Int]
    • powerfulArray 0 = []
    • powerfulArray (pred -> n) = arrayN ++ 2^n : arrayN
    • where arrayN = powerfulArray n
    • powerfulArray = (iterate next [] !!) where next l = 1 : (do n <- l; [2 * n, 1])