All pastes #1932556 Raw Edit

Anonymous

public text v1 · immutable
#1932556 ·published 2010-09-04 12:14 UTC
rendered paste body
module Test
where

--It accepts n and k, prints numbers 1 to n, starting with +1 and toggling their sign after each triangular number
series 0 = []
series n =
	if True --isTriangular n
		then if odd n--(n + (getPrevTri n (n-1)))
			then [] --series (getPrevTri n (n-1)) ++ getSeries (odd (n + (getPrevTri n (n-1)))) ((getPrevTri n (n-1)) + 1) (n - (getPrevTri n (n-1)))
			else [] --series (getPrevTri n (n-1)) ++ getSeries (odd ((getNextTri n (n+1)) + (getPrevTri n (n-1)))) ((getPrevTri n (n-1)) + 1) (n - (getPrevTri n (n-1)))
			--The sign is negative for those numbers which follow an odd triangular number AND the triangular number previous to it is even
			--OR an even number AND the triangular number previous to it is odd.
				else []

getSeries sign start 0 = []
getSeries sign start n = 
	if sign == True
		then [start] ++ getSeries True (start+1) (n-1)
		else [-start] ++ getSeries False (start+1) (n-1)

--Checks whether n is a triangular number or not		
isTriangular 0 = False
isTriangular n =
	checkSum n 1
		
--Checks whether n is equal to sum of first few natural numbers, starting from k
checkSum n 0 = False
checkSum n k =
	if n == (k * k + k)/ 2
		then True
		else if n > (k * k + k)/ 2
			then checkSum n (k+1)
			else False

--Gets the triangular number just smaller than n, descending from k
getPrevTri 0 k = 0
getPrevTri n k =
	if k <= n
		then if isTriangular k
			then truncate k
			else getPrevTri n (k-1)
		else 0

--Gets the triangular number just greater than n, starting from k
getNextTri 0 k = 1
getNextTri n k =
	if k >= n
		then if isTriangular k
			then truncate k
			else getNextTri n (k+1)
		else 0

{-
*Test> :r
[1 of 1] Compiling Test             ( Test.hs, interpreted )
Ok, modules loaded: Test.
*Test> series 16
[]
it :: [a]
-}