I am facing a problem on creating pascal triangle. The code is given below. Here sumLstElts will sum the elements on a row, putBetween will put the sum at the right side of the [1]. pascal n will give us the series of pascal sequences in a row.
sumLstElts (x:[])=[x]
sumLstElts []=[]
sumLstElts xs=[head xs+head(tail xs)]++sumLstElts (tail xs)
putBetween xs'= [1]++xs''
where xs''=sumLstElts xs'
pascal 0=[1]
pascal n= putBetween(pascal (n-1)) ++ pascal(n-1)
Would u pls help me to identify my error?
thnx in advance.
saugata
If you want just the n-th row,
is the right way,
putBetweenalready constructs the complete next row from a given. If you want to create the triangle as a list of rows, something likewould construct the triangle starting with row 0. If you want the part of the triangle in reverse order, longer rows first,
does that.
The problem with your attempt is that
putBetweenis applied to the entire result ofpascal (n-1), which is intended to contain not only then-1-st row, but also the previous rows. Sopascal 1gave the concatenation of the first row[1,1]with the zeroth[1], and whenpascal 2appliesputBetweento that, it’s not a row of Pascal’s triangle, so the result isn’t one either. You could also get the concatenation of the rows bybut in my opinion it’s better to have the triangle as a list of rows.