Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- ;; Given a list of numbers, return a list where the i-th element corresponds to the multiplication of the initial list except its i-th element.
- ;; Example:
- ;; [1,2,3,4] -> [24, 12, 8, 6]
- ; index :: [Number] -> [(Integer, Number)]
- ; indexes a list of numbers
- (define (index lNum)
- (for/list [(i (in-naturals))
- (n (in-list lNum))]
- (cons i n) ; tuple '(i . n)
- )
- )
- ; filter-i :: [(Integer, Number)] -> [(Integer, Number)]
- ; filters an indexed list, indexed-l, by removing
- ; the element with index i
- (define (filter-i indexed-l idx)
- (filter [lambda (a) (not [eq? (car a) idx])] indexed-l)
- )
- ; product-ex-self :: [Number] -> [Number]
- ; produces a list of products of elements of list, l,
- ; in which, in each product, one of the list elements
- ; is excluded
- (define (product-ex-self lNum)
- (let [(idx-l (index lNum)) ; indexed list
- ]
- ;--IN--
- (for/list [(ix (in-list idx-l))]
- (apply * (map cdr [filter-i idx-l (car ix)]))
- )
- )
- )
- (define mylist (list 1 2 3 4))
- (print (product-ex-self mylist))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement