I need some help interpreting

Discussion of Scheme and Racket
Post Reply
MrT
Posts: 2
Joined: Thu Jul 01, 2010 10:05 pm

I need some help interpreting

Post by MrT » Thu Jul 01, 2010 10:12 pm

Hi

I use a program called Fluent, in which one can use scheme-snippets to automate certain things.

I have obtained a snippet that I use but I can't really understand what each line does. The snippet works, but I want to be able to modify it, and thus I need to understand it.

The code looks like this:

Code: Select all

(define filter
	(let ((mywallthreads '()) (mythreads '()) (mythreadtypes '()) (threadtype '()))
	
	(lambda ()
		(set! mythreads (map thread-name (get-face-threads)))
		(set! mythreadtypes (map thread-type (get-face-threads)))
		(set! mywallthreads '())
		(set! threadtype 'wall)		
		(do ((i 0 (+ i 1))		) 
		((= i (- (length mythreads) 1)))	
		( if (eq? threadtype (list-ref mythreadtypes i)) (set! mywallthreads (cons (list-ref mythreads i) mywallthreads )))
		)
		(vector->list (list->vector mywallthreads))
	)
	)
	
)  
(define report-wall-file
 (lambda (rep-type quantity savefile filename)
  (ti-menu-load-string (format #f "re surf ~a ~a ~a ~a ~a"
                        rep-type
                        (filter)
                        quantity savefile filename))))
The above is stored in a file called Report.scm

I call it by using the following

Code: Select all

/file/read-macros Report.scm
(report-wall-file 'area 'pressure 'yes 'output.srp)		
Can anyone help me interpreting this stuff?

Best regards from Sweden

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: I need some help interpreting

Post by nuntius » Fri Jul 02, 2010 8:36 pm

Scheme isn't my forte, but here's a go.

(define filter specifies that we are creating a function that takes no arguments.
(let ((mywallthreads '()) (mythreads '()) ... declares local variables mywallthreads, mythreads, ... and gives each an empty list as its initial value.
(lambda () ...) creates a new function with no name and no arguments (nothing in the parens). This forms a "closure" which captures the local variables defined by the let. So each time you call (filter), a new function will be created, each with its own empty lists.
(set! variable value) is an assignment roughly equivalent to variable=value or variable:=value in algol-style languages (including C, C++, and Java).
(map function list) calls (function value) for each value in the list. It returns a list of the return values from the function.
The do loop would look something like the following in C++.

Code: Select all

for(int i=0; i!=(mythreads.length()-1); i++) {
  if(threadtype==mythreadtypes.get(i))
  { mywallthreads.push_front(mythreads.get(i); }
}
I have no idea why they then convert the list to a vector and then back to a list before returning it as a value. Maybe this is a bizarre pattern to copy a list?

Hope that helps.

MrT
Posts: 2
Joined: Thu Jul 01, 2010 10:05 pm

Re: I need some help interpreting

Post by MrT » Mon Jul 05, 2010 3:05 am

Thanks a lot nuntius. I'll try to rewrite the code to do what I want and your input on the existing code will certainly help me.

Thanks again
Tobbe

Post Reply