Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Iron Speed
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old November 22nd, 2006, 03:15 PM
NetBSD NetBSD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Canada
Posts: 229 NetBSD Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 7 h 53 m 57 sec
Reputation Power: 0
Send a message via MSN to NetBSD
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?

Reply With Quote
  #2  
Old December 19th, 2006, 11:01 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old December 19th, 2006, 11:39 PM
spoon! spoon! is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 247 spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level)spoon! User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 8 h 15 m 23 sec
Reputation Power: 77
you mean
Code:
(define (total lst)
  (reverse
   (foldl (lambda (a b)
	    (cons (if (null? a)
		      b
		      (+ b (car a)))
		  a))
	  '()
	  lst)))

Reply With Quote
  #4  
Old December 20th, 2006, 12:12 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
Originally Posted by spoon!
you mean
Code:
(define (total lst)
  (reverse
   (foldl (lambda (a b)
	    (cons (if (null? a)
		      b
		      (+ b (car a)))
		  a))
	  '()
	  lst)))


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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Scheme function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway