## Simple List Function: counting all the elements in a list

## Simple List Function: counting all the elements in a list

(OP)

I'm just learning Scheme and I'm having a hard time with it. I'm trying to write a simple function that will count all the elements in a list. For example, the list '(1(2(3)4)5 6)) has a length of 4, but contains 6 elements. This is the function I've written so far, but when I try to use it I immediately get "Error: Bad function object:0"

(define (elements theList)

(if (null? theList) (0)

(

(if(list? (car theList)) (+ 0 (elements (car

theList)))

(

+ 1 (elements (cdr theList))

))

)

))

Someone help me out.

(define (elements theList)

(if (null? theList) (0)

(

(if(list? (car theList)) (+ 0 (elements (car

theList)))

(

+ 1 (elements (cdr theList))

))

)

))

Someone help me out.

## RE: Simple List Function: counting all the elements in a list

Regards,

Bert Vingerhoets

vingerhoetsbert@hotmail.com

http://student.vub.ac.be/~bvingerh/

Don't worry what people think about you. They're too busy wondering what you think about them.

## RE: Simple List Function: counting all the elements in a list

## RE: Simple List Function: counting all the elements in a list

Examples:

'(hello) => list with element 'hello

'(0) => list with element 0 ('0 evaluates to 0)

(display 'blah) => call function display with parameter 'blah

(0) => call non-existent function 0

You seem to have trouble understanding that every statement in Scheme can be regarded as a function and therefore has a result. Search in Google for "Teach yourself Scheme in fixnum days", it's a good and, most importantly, short manual for the beginner.

And lastly, the if-statement works as follows (example returns the length of the list):

(define (length lst)

(if (null? lst)

0

(+ (length (cdr lst)) 1)))

A last tip: the function you need can be written in 4 lines with a conditional.

Regards,

Bert Vingerhoets

vingerhoetsbert@hotmail.com

http://student.vub.ac.be/~bvingerh/

Don't worry what people think about you. They're too busy wondering what you think about them.

## RE: Simple List Function: counting all the elements in a list

(define (try theList)

(cond ((null? theList) 0)

((list? (car theList)) (+ '0 (elements (car

theList))))

(else (+ '1 (cdr theList)))

))

And now I'm getting the error "Bad function object:(1 (2 (3) 4) 5 6)" when I try using that as my list. I define it like this:

(define theList '(1(2(3)4)5 6))

which I believe is correct. Am I even close in writing this function?

## RE: Simple List Function: counting all the elements in a list

(define (elements theList)

(cond ((null? theList) 0)

((list? (car theList)) (+ '0 (elements (car

theList))))

(else (+ '1 (cdr theList)))

))

## RE: Simple List Function: counting all the elements in a list

Think of what your function is supposed to look like: it will be recursive (why?).

A recursive function always has 2 major parts: a finishing step (trivial step) and one or more constructive steps.

In your function the trivial step is that where you handle the empty list.

The two constructive steps are: a) the one where you have a sub-list of the given list and b) the one where you have something else (an atom).

My previous example:

(define (length lst) ;Function receives

alist(if (null? lst) ;trivial step: if the list is empty,

0 ;return 0

(+ 1 (length (cdr lst))))) ;if it's not empty, construct the result by adding the length of the first element (of which you are sure it's 1) to the length of the rest of the list (of which you aren't sure, so you apply the length function to it; it might help thinking that this length function has nothing whatsoever to do with the function you're working in)

Regards,

Bert Vingerhoets

vingerhoetsbert@hotmail.com

http://student.vub.ac.be/~bvingerh/

Don't worry what people think about you. They're too busy wondering what you think about them.

## RE: Simple List Function: counting all the elements in a list

And

I don't get a "bad function object" error, I get this:

(define (elements theList)

(cond ((null? theList) 0)

((list? (car theList)) (+ '0 (elements (car

theList))))

(else (+ '1 (cdr theList)))

))

(elements thelist)

+: expects type <number> as 2nd argument, given: ((2 (3) 4) 5 6); other arguments were: 1

Bert Vingerhoets

vingerhoetsbert@hotmail.com

http://student.vub.ac.be/~bvingerh/

Don't worry what people think about you. They're too busy wondering what you think about them.

## RE: Simple List Function: counting all the elements in a list

(define (try theList)

(cond ((null? theList) '0)

((list? (car theList)) (+ (try (car theList)) (try

(cdr theList))))

(else (+ '1 (try(cdr theList))))

))

I just wanna run through this through the experts. And if this indeed correct, then I want to thank Bert Vingerhoets for all the great help.

## RE: Simple List Function: counting all the elements in a list

(define (elements lst)

(cond

((not (list? lst)) (error "elements: expects argument of type <proper list>"))

((null? lst) 0)

((list? (car lst)) (+ (try (car lst))

(try (cdr lst))))

(else (+ 1 (try (cdr lst))))))

As an excercise in implementing recurrent functions, you can try to build a function to reverse a given list, a function to count all elements in the given list with support for dotted pairs, a function that generates a new list out of a given list with an element inserted at a certain position, and so on.

Bert Vingerhoets

vingerhoetsbert@hotmail.com

http://student.vub.ac.be/~bvingerh/

Don't worry what people think about you. They're too busy wondering what you think about them.