Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Scheme- summing elements in a list within a structure
Author Message
rikachama

Posted: Sun Nov 14, 2010 11:51 am   Post subject: Scheme- summing elements in a list within a structure

I know how to add up all the elements in the list, however I am trying to add up all the elements of a list that is in a structure but i keep getting errors.
-This would just be for adding the elements of a list byitself.
 Scheme: (define (sum k)     (cond       [(empty? k) 0]       [else (+ (first k) (sum (rest k)))]))

However, say I have a sturcture such as:

(define-struct struct1 (x y))
;;(make-struct1 "hello" (list 1 2 3 4 5))

with "hello" being x, and the list being y.
 Scheme: (define (sum k)   (cond     [(empty? (struct1-y k)) 0]     [else      (+ (first (struct1-y k)) (sum (rest (struct1-y k))))]))

I am selecting the list from the structure, yet I get the error struct1-y: expects argument of type <struct:struct1>; given (list 2 3 4 5)

jcollins1991

Posted: Sun Nov 14, 2010 12:37 pm   Post subject: Re: Scheme- summing elements in a list within a structure

Because after the first iteration you're passing your function a list instead of a structure, if you want to continue using your structure you have to change your function call to create a new structure instead:

 code: (sum (make-struct1 (struct1-x k) (rest (struct1-y k))))

Otherwise, you could just use a wrapper function that extracts the list from the structure. Final option (though if this is for school work you probably can't use it yet) is to use foldr and do everything with 2 lines of code:

 code: (define (sum k)     (foldr (lambda (x y) (+ x y)) 0 (struct1-y k)))
rikachama

Posted: Sun Nov 14, 2010 12:50 pm   Post subject: Re: Scheme- summing elements in a list within a structure

Thanks >< it makes sense now, but yeah I don't think I can use the 2nd solution. Thread can be closed now.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 3 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: