Sum items in list Racket
Asked Answered
G

4

6

I'm working with simple lists in Racket, and I was doing a function to sum the elements of a list.

But I would like to know if there is any simpler way to do this.

I did this function:

(define (mySum L)
  (if (empty? L) 0
      (+ (first L) (mySum (rest L))))
  )

output:

(mySum '(1 2 3 4))
10

I wanted to know if anyone knows a simpler way to do this. I explain myself, for example: This is another function I did:

(define (myAppend L1 L2)
  (if (empty? L1) L2
      (cons (car L1) (myAppend (cdr L1) L2)))
  )

But this function can be done more simply by doing just this:

(define (myAppend L1 L2)
  (append L1 L2)
  )

My problem is to know if there is a simpler way to do the sum of items in a list. Thanks

Guttural answered 6/1, 2017 at 18:28 Comment(0)
B
7

If all you're going for is a short program,

(define (mySum L)
  (apply + L))

is shorter. Whether it is "simpler" is a matter of interpretation: The way you've written it seems "simpler" in that it is defined purely based on the structure of lists, whereas (apply + L) relies on both a semi-magical function (apply) and a semi-fancy behavior of + (the fact that it takes a variable number of arguments) that itself has to do something similar to what you've already written.

(Incidentally, I wouldn't call your myAppend example simpler, either -- the first one actually defines the function you want, whereas the second just refers to the behavior you want, which in turn had to be defined elsewhere. If you're doing this as part of a practical program, (apply + L) and using the built-in append are certainly the way to go, but if this is for educational purposes then I think the initial versions of both functions are superior.)

Botha answered 6/1, 2017 at 18:43 Comment(1)
Thank you for sharing your knowledge!Guttural
G
5

Another way to approach it would be

(define (my-sum lst)
 (foldr + 0 lst))

This uses built-in function foldr and is fairly short. Foldr consumes a function (+), base-case (0) and a list (lst). I've attached a link to a visual representation of foldr, as well as a link that explains it really well.

link

More information here

Gapeworm answered 7/1, 2017 at 15:31 Comment(1)
Some interesting discussion on fold here also #39018663.Baptize
S
3
(apply + '(elements_of_list))

:)

Supersonic answered 8/5, 2017 at 15:38 Comment(1)
Would be better if you explained why and how this code solves the problem. Formatting the code is also nice. Is that :) part of the code?Letta
B
2

One can also use for/sum:

(define (my+ L)
  (for/sum ((i L))
    i))

(my+ '(1 2 3 4))  ; => 10

'named let' is a version similar to recursive functions but may be easier to understand:

(define (myplus L)
  (let loop ((L L)
             (s 0))
    (cond
      [(empty? L) s]
      [else (loop (rest L) 
                  (+ s (first L)))] )))

'cond' can also be replaced by 'if' here, since there are only 2 cases. The keyword 'else' can also be omitted.

Baptize answered 7/1, 2017 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.