Python's Reduce Function - Written in Scheme -


evening!

i need write reduce function in scheme works built-in reduce function in python. it's easy write reduce function in scheme:

(define (reduce fn lis identity)   (if (null? lis)     identity     (fn (car lis)         (reduce fn (cdr lis) identity)))) 

however, code isn't identical python reduce, takes 2 arguments (the function , list of items reduce).

can help me write scheme function works way?

(>(reduce * '(2 4 5 5)) => 200, our professor's example.)

thanks much, guys , gals. helpful <3

eta: mr. levien , mr. jester-young, thank much. provided perfect amount of information me figure problem out on own. *hug

that easy. accumulator initialized first element of list:

(define (py-reduce f ls) (fold f (car ls) (cdr ls)))  (py-reduce * '(2 4 5 5)) => 200 

feel free use reduce function fold srfi-1 (like reduce, fold-right ...) or use own. list needs 1 element @ least.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -