views:

750

answers:

7

Hi, How to go through a list or fetch element from a list in scheme?

How can I name each element (like we do for variables in java) in a list?

Thanks in advance.

I want to compare every point in a list to another point. So, as we do in java or python-

for(int i;i<list.size();i++){
    if (list[i]> k){ 
        //do something
    }

}

How can I do similar thing in scheme?

A: 

Example

`(define func
  (lambda (a b)
    (if (> a b)
        (dosomething a))))`

In this case, "dosomething" would be some other definition, either predefined or not. Definition being analogous to a "function" let's say, such as square() or mult(), as examples.

I think a recursive one like this should work for the list needs:

(define (func list) (if (> (car list) k) (dosomething)) (func (cdr list)))

You can also write two definitions, have one contain a list and send it to the first example and return the result, compare, do something or not, and continue.

Sev
But where is list ? I am supposed to compare each element from list one by one. But here it seems only 2 values are compares. My main problem is to fetch element from list (like we do in java list[i]). How to do that?
fireball003
+5  A: 
(map (lambda (x) (if (< x k) (dosomething x) x)) list)
leppie
+2  A: 

You can use for-each:

    (let ((a-list (list 9 2 7 6 1 4 3 8 1))
          (k 4)
          (something display))
      (for-each (lambda (i) ; The 'name' of the list element is i
                  (if (> i k)
                      (something i) ; Do something with i
                      #f)) ; Do nothing
                a-list))

for-each is similar to map but the return value is unspecified. It is called for its side-effects only.

On a side note, It appears to me that you are learning scheme coming from a java background (which is ok). Scheme programs are often written in different style than in Java. Scheme is a functional language and looping constructs are not used quite as often. Pick up a book on Scheme, for example The Scheme Programming Language or How to Design Programs to learn how to write programs "the Scheme way"

Jonas
Thanks again Jonas. Yes, I learned java extensively, next C and then python. All hose languages have more or less same pattern while scheme is completely different. So, is the doc of scheme which doesn't seem friendly for those who come from different programming practice. However, I am leaving this flying practice and will be following the books.
fireball003
+3  A: 

leppie and Jonas give the right answer for iterating over a list in Scheme. However, if you need to get a single value in a list, use list-ref.

(let ((l '(1 2 3 4)))
  (list-ref l 2))

=> 3

Is mostly equivalent to the Java code

int[] l = new int[] { 1, 2, 3, 4 };
return l[2];
Nathan Sanders
A: 

You can use list-ref to pull out a value from a list by its index. But note that in Scheme lists are actually linked lists -- so (list-ref l 100) will need to trace through a 100 references. If you truly want random-access values, use a vector.

Eli Barzilay
+2  A: 

You should think differently when writing in a functional language. Actually you should forget the programming thinking (for loops, etc.), just define what you want. For example "I want to apply an-action to every odd number in my list." So you would write :

(map an-action
     (filter odd? my-list))

Or the condition can be element > 4

(define (higher-than-four? n) (> n 4))
(map an-action
     (filter higher-than-four?
             my-list))

For an action, you give any function :

> (map number->string
       (filter higher-than-four? my-list))
("5" "6" "7" "8" "9" "10")
Eino Gourdin
A: 

I am not sure if this is what you want, but this goes to the whole list (linked) and compare each element with the element that you pass as parameter:

(define (compare aList element)
      (if (> element (car aList)) 
         (display (car aList)) ;here do something great
         ;here the else if you have
        )
      (if (not (null? (cdr aList)))
               (compare (cdr aList) element)) 'ok)

The issue that this is a procedure, maybe it is helpful for someone.

greets

Christian