Using lambdas and recursive functions (no loops) write a procedure in Racket that returns the n-th line of Pascal's tria

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Using lambdas and recursive functions (no loops) write a procedure in Racket that returns the n-th line of Pascal's tria

Post by answerhappygod »

Using lambdas and recursive functions (no loops) write a
procedure in Racket that returns the n-th line of Pascal's triangle
as a list.
At this point, I haven't even begun to add the lambdas. I'm
just trying to get the function to work. My ultimate goal is to
make a function that accepts the "n" value and returns that single
row of Pascal's triangle in the form of a list. Right now I'm
trying to get a function to accept two variables, a list, and a
number (row).
The way I was planning on solving this challenge was by taking
the second, (1 1), row of pascals triangle, adding a zero to the
list, reversing the list, then adding the two values to get the
next row. i.e. (0 1 1) + (1 1 0) gives (1 2 1), (1 2 1 0) + (0 1 2
1) = (1 3 3 1)... nth number.
Now I know that the second number in the returned list will
equal the nth number if done correctly. I wanted to test that in
the function to tell it to stop the recursion. However, I don't
think I've done that correctly. Please help!
#lang racket
; Using lambdas and recursive functions (no loops)
; write a procedure in Racket that returns the n-th
; line of Pascal's triangle as a list.
(define listn
(list 0 1 1))
(define (added-lists listn rowNum)
(define tmpList
(reverse listn))
(define newList
(map + listn tmpList))
(if (= rowNum 0)
'(1)
(if (= rowNum 1)
'(1 1)
(if (= (second newList) rowNum)
newList
(added-lists newList rowNum)))))
(define (pascals n)
(added-lists listn n))
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply