Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Simple List Function: counting all the elements in a list

SSJ2Joseph (Programmer)
5 Mar 03 17:09
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.
Bertv100 (Programmer)
7 Mar 03 8:32
That's because you call the non-existing function 0 in the second line.

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.

SSJ2Joseph (Programmer)
8 Mar 03 14:51
I intend 0 to be a number not a function.  Is Scheme that weird that it thinks a number is a function name?
Bertv100 (Programmer)
8 Mar 03 17:24
A symbol that follows a non-quoted left parentheses must always denote a function.
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.

SSJ2Joseph (Programmer)
9 Mar 03 12:25
Now my function looks like this:

(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?
SSJ2Joseph (Programmer)
9 Mar 03 12:29
In the previous post the function should be

(define (elements theList)
  (cond ((null? theList) 0)
        ((list? (car theList)) (+ '0 (elements (car
          theList))))
        (else (+ '1 (cdr theList)))
))
Bertv100 (Programmer)
9 Mar 03 16:45
Since quoted numbers evaluate to themselves, you can drop the quote ('1 evaluates to the value one, but so does 1).

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 a list
  (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.

Bertv100 (Programmer)
9 Mar 03 16:52
Sorry, I didn't take a close look to your function; yes you're close.
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

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.

SSJ2Joseph (Programmer)
10 Mar 03 12:13
I think I've got it!!

(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.
Bertv100 (Programmer)
11 Mar 03 11:21
Correct, if you want to add some safety, you can use the error function:
(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.

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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close