|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Scheme function
How can I use foldl to write a function for the running totals of a list, i.e.
(total (list 1 1 1)) => (list 1 2 3) Any hints? |
|
#2
|
||||
|
||||
|
Hey Net,
This should do the job; I haven't really tested it as I'm on windows and don't have Scheme installed. Code:
(define (total lst)
(reverse
(foldr (lambda (a b)
(cons (if (null? b)
a
(+ a (car b)))
b))
‘()
lst)))
Please let me know if you came up with something different .Take care, Mark. |
|
#3
|
|||
|
|||
|
you mean
Code:
(define (total lst) (reverse (foldl (lambda (a b) (cons (if (null? a) b (+ b (car a))) a)) '() lst))) |
|
#4
|
||||
|
||||
|
Quote:
Hi, I really did mean foldr - it is easy to mix the two functions up though. Please check out the fold page on wikipedia .http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29 Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Scheme function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|