Skip to content

Instantly share code, notes, and snippets.

@ChillyBwoy
Created March 29, 2017 14:53
Show Gist options
  • Save ChillyBwoy/d4307e3eaa70ec1792a6de59f7311b40 to your computer and use it in GitHub Desktop.
Save ChillyBwoy/d4307e3eaa70ec1792a6de59f7311b40 to your computer and use it in GitHub Desktop.
Demo.hs
-- bar a b c = c + b - 2 * a
-- foo ... = bar ...
-- foo ... = ... bar ...
fib :: Integer -> Integer
fib n = fn n (0, 1)
where
fn 0 (a, b) = a
fn n (a, b) = fn (n - 1) (a + b, a)
-- [1,2,3,3,2,-1,-5,-10,-13,-13,-6,7,27,46,59,51,18,-49,-133,-218,-253]
seqA :: Integer -> Integer
seqA n = let
curr a b c = a + b - 2 * c
fn 0 (a, b, c) = a
fn 1 (a, b, c) = b
fn 2 (a, b, c) = c
fn n' (a, b, c) = fn (n' - 1) (a + b - 2 * c, a, b)
in fn n (3, 2, 1)
seqA' :: Integer -> Integer
seqA' n = let
f' 0 = 1
f' 1 = 2
f' 2 = 3
f' n = f' (n - 1) + f' (n - 2) - 2 * f' (n - 3)
in f' n
-- a0 = 1; a1 = 2;
-- an = 3 * an-1 – 2 * an-2 + 1 при n = 2, 3,...
seqB :: Integer -> Integer
seqB n = let
fn 0 _ p = p
fn 1 c _ = c
fn n c p = fn (n - 1) q c
where q = 3 * c - 2 * p + 1
in fn n 2 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment