Skip to content

Instantly share code, notes, and snippets.

@cbilson
Created January 13, 2009 13:28
Show Gist options
  • Save cbilson/46446 to your computer and use it in GitHub Desktop.
Save cbilson/46446 to your computer and use it in GitHub Desktop.
module Polynomials
where
difs :: [Integer] -> [Integer]
difs [] = []
difs [n] = []
difs (n:m:ks) = m-n : difs (m:ks)
difLists :: [[Integer]]->[[Integer]]
difLists [] = []
difLists lists@(xs:xss) =
if constant xs then lists else difLists ((difs xs):lists)
where
constant (n:m:ms) = all (==n) (m:ms)
constant _ = error "lack of data or not a polynomial fct"
genDifs :: [Integer] -> [Integer]
genDifs xs = map last (difLists [xs])
nextD :: [Integer] -> [Integer]
nextD [] = error "no data"
nextD [n] = [n]
nextD (n:m:ks) = n : nextD (n+m : ks)
next :: [Integer] -> Integer
next = last . nextD . genDifs
continue :: [Integer] -> [Integer]
continue xs = map last (iterate nextD differences)
where
differences = nextD (genDifs xs)
degree :: [Integer] -> Int
degree xs = length (difLists [xs]) - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment