Created
January 13, 2009 13:28
-
-
Save cbilson/46446 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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