This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

i dont understand this code

2 posts · 3450 views

hy everyone, for homework i had to make a faculty function that used multiplication.

so i made this :

(define (fac a)
(if (= a 1)
1
(* a (fac (- a 1)))))

and it works, but the solution gave this :

(define (fac n)
(product (lambda (x) x) 1 (lambda (x) (+ x 1)) n))

and i dont understand what happens here, could someone explain for me?

many thanks, a beginning student

Re: i dont understand this code

Obviously the product takes 4 arguments an expression, a lower bound, an incrementor and an upper bound. And it works just like a mathematics big pi notation. In this particular usage the expression is an identity function (it's returning the index), the lower bound is 1, the incrementor is a function incrementing by 1 and finally the upper bound is n.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.