Skip to content

Instantly share code, notes, and snippets.

@umairsd
umairsd / pascal-triangle.hs
Last active August 29, 2015 14:24
Pascal's Triangle
-- Takes an array of integers, and adds pairs of numbers to create a new
-- array. For example, [1,2,3,4] changes to [(1+2), (2+3), (3+4)], resulting
-- in [3,5,7]
sumOfPairs :: [Integer] -> [Integer]
sumOfPairs [] = []
sumOfPairs (x:[]) = []
sumOfPairs (x:y:ys) = (x+y) : sumOfPairs (y:ys)
-- Given a row of Pascal's triangle, returns the next row
-- Index based list operations using folds
--
-- Author: Umair Saeed
-- Date: 05/17/2015
--
-- insert at an index, implemented via foldr
insertAt :: a -> [a] -> Int -> [a]
insertAt x ys n = foldr insertHelper [] $ zip [0..] ys
where