---
# System prepended metadata

title: In Class Studio 5

---

# In Class Studio 5

## Question 1
The function list_ref can be applied to a list xs and a number n, and returns the n-th element of the list, starting counting at 0. So list_ref(list(1, 2, 3), 2) evaluates to 3. The position of an element in the list is called its rank; we say that the number 3 has rank 2 in the list. Write a Source function called every_second that takes a list as its only argument and returns a list containing all the elements of odd rank (i.e. every second element) from the input list.

```javascript
every_second(list("a", "x", "b", "y", "c", "z", "d"))

// Value: ["x", ["y", ["z", null]]] or list("x", "y", "z")

// Owen's answer

function every_second(items) {
    return is_null(items) || is_null(tail(items)) 
        ? null 
        : pair(head(tail(items)),
               every_second(tail(tail(items))));
}

```





## Question 2
Write a Source §2 function called sums that takes a list of numbers as its only argument and returns a list of numbers, containing two elements: the first is the sum of all evenranked numbers in the input list (ranks 0, 2, 4 etc), whereras the second element is the sum of all odd-ranked elements in the input list (ranks 1, 3, 5 etc).
Example call:

```javascript
sums(list(1,2,3,4,5));
// Value: [9, [6, null]] or list(9, 6)
```
We say that `sums` has the following type:
`sums`: (list of Number) -> (list of Number)

```javascript
//Your answer here
function every_second_sums(items) {
    function second_iter(items,n){
        if (is_null(items)){
            return 0;
        } else if  (n % 2 === 1){
            return head(items) + second_iter(tail(items),n+1);
        } else {
            return second_iter(tail(items),n+1);
        }
        
    }
    
    return second_iter(items,0);
}

function every_first_sums(items) {
    function second_iter(items,n){
        if (is_null(items)){
            return 0;
        } else if  (n % 2 === 0){
            return head(items) + second_iter(tail(items),n+1);
        } else {
            return second_iter(tail(items),n+1);
        }
        
    }
    
    return second_iter(items,0);
}

function sums(items){
    return pair(every_first_sums(items),pair(every_second_sums(items),null));
}
```