Quantcast
Channel: How to add in a list in racket - Stack Overflow
Browsing latest articles
Browse All 7 View Live

Answer by Shawn for How to add in a list in racket

Another way in Racket is to use the for/sum comprehension to do all the work of adding and iterating. You just have to (recursively) give it the numbers to add together:(define (sum lloi) (for/sum...

View Article



Answer by ad absurdum for How to add in a list in racket

OP problem is an example of a more general class of problems dealing with nested lists, often solved by tree-flattening or list-flattening.With a nested list, each element is either a list or an atom...

View Article

Answer by Adam Štafa for How to add in a list in racket

A little late, but here's my take:(define (sum lloi) (cond [(empty? lloi) 0] [(number? lloi) lloi] [else (apply + (map sum lloi))]))The input list may contain numbers, empty lists or lists in the same...

View Article

Answer by alinsoar for How to add in a list in racket

(define (sum l) (foldl (lambda(x acc) (+ acc (if (pair? x) (sum x) x))) 0 l))> (sum '(1 2 3 (1 2 3)))12> (sum '(1 2 3))6

View Article

Answer by Alex Knauth for How to add in a list in racket

Here's how I would design such a function systematically using parts of the Design Recipe from How to Design Programs.The input is a list that may contain numbers and lists of numbers. For now I'll...

View Article


Answer by soegaard for How to add in a list in racket

Here is a start:(define (sum lloi) (cond [(empty? lloi) 0] [(number? (first lloi)) (+ (first lloi) (sum (rest lloi)))] [(list? (first lloi)) ???]))If (first lloi) is a list, you need to find its sum,...

View Article

How to add in a list in racket

For example:• (sum empty) ⇒ 0• (sum (list 1 2 3)) ⇒ 6• (sum (list 1 (list 2) 3 (list 4 5))) ⇒ 15What I have so Far. It calculates the sum of the numbers in the list. The test passes for some of the...

View Article
Browsing latest articles
Browse All 7 View Live




Latest Images