Search code examples
listmultidimensional-arrayschemedeclarationusing

Passing a multidimensional list pointer in scheme


I am trying to declare a multidimensional list and use it by reference (under scheme)

(let* (
      (big-list (list '(0 0 8 4 255 255) '(0 0 16 6 255 255)))
      (small-list 0) (v1 0) (v2 0) (v3 0)
      ))

(set! small-list (car(big-list)))

(set! v1 (car(small-list)))

(set! v2 (cadr(small-list)))

etc..

As you wise and experienced schemers can see it is not working. I suspect that "big-list" obviously needs some special treatment in declaration and access (Illegal function!), and I am afraid I have scoured the web some but have been unable to find guidance.

Any kind soul please help ?


Solution

  • The syntax of let and let* is (let <bindings> <body>). The bindings are only available in the body. Your example becomes:

    (let* ((big-list (list '(0 0 8 4 255 255) '(0 0 16 6 255 255)))
           (small-list 0)
           (v1 0)
           (v2 0)
           (v3 0))
      (set! small-list (car big-list))
      (set! v1 (car small-list))
      (set! v2 (cadr small-list))
    
      (display "v1 ") (display v1) (newline)
      (display "v2 ") (display v2) (newline))
    

    Here (set! v1 (car small-list)) is moved into the body of the let.

    An alternative is to use define :

    (define big-list (list '(0 0 8 4 255 255) '(0 0 16 6 255 255)))
    (define small-list 0)
    (define v1 0)
    (define v2 0)
    (define v3 0)
    
    (set! small-list (car big-list))
    (set! v1 (car small-list))
    (set! v2 (cadr small-list))
    
    (display "v1 ") (display v1) (newline)
    (display "v2 ") (display v2) (newline)