Recently I have been writing and repackaging a variety of Scheme libraries to work on R7RS implementations. The collection is a work in progress, but is currently tested on Chibi, Kawa and Larceny: with some exceptions, the included examples and tests work on all three implementations.

For details, installation, licensing etc, see:

* https://github.com/petercrlane/r7rs-libs

Documentation:

* in pdf http://peterlane.info/downloads/r7rs.pdf

* or html http://peterlane.info/r7rs/html/index.html

Currently, there are 7 sub-collections, with 165 libraries between them, and over 1700 exported functions/variables.

- Code:
`| Name | Libraries | Exports`

| autodiff | 17 | 146

| nltk | 6 | 76

| pfds | 16 | 183

| rebottled | 9 | 305

| robin | 9 | 113

| slib | 91 | 713

| weinholt | 17 | 190

| Total | 165 | 1726

Robin is written by me and includes a statistics library, a text library with a stemmer, similarity measures and formatting functions, and, from Ruby, abbrev and logger libraries.

The remaining collections are repackaged from R6RS or earlier Scheme versions to work with R7RS and are from various sources and by various authors.

The main repackaged collection is SLIB, which includes a variety of data structures, functions to manipulate strings and arrays, a

version of lisp's format command, various mathematical functions, an xml-parser, a database system, and a 3D modelling language, amongst many others.

The remaining collections include text processing (nltk), functional data structures (pfds), cryptographic functions (weinholt), pdf

output and regular expressions (rebottled), and automatic differentiation (autodiff).

Health warning: As not all parts of the different collections came with test suites, and even those which have test suites are not necessarily rigorously tested, I may well have broken things in conversion. All known problems are listed in the README.

Let me know of any problems via Github issues, here or email (see url below).

Statistics: Posted by peterlane — Mon Apr 10, 2017 6:55 am

]]>

christian urgese wrote:

(let* ((ff (cadr (file-glob "*.xcf" 1)))) (filename "") (image 1) (layer 0))

For example, write the start like this:

- Code:
`(let* ((ff (cadr (file-glob "*.xcf" 1)))) <-- you have one too many close brackets here, they should only close up to the one before 'ff'`

(filename "")

(image 1)

(layer 0)) <-- leaving this one to close off the one before the one before 'ff'.

Should the sccond while loop be 'within' the first while loop, as it is now, or in sequence with it? If in sequence, you need an extra close bracket after (set! filename ...)

The close brackets before and after (gimp-quit 0) do not match anything.

I suggest you put your code into a scheme aware editor, which will match up and indent according to your brackets.

Statistics: Posted by peterlane — Fri Feb 10, 2017 5:03 pm

]]>

(let* ((ff (cadr (file-glob "*.xcf" 1)))) (filename "") (image 1) (layer 0))

(while (not (null? ff))

(set! image (car (gimp-file-load RUN-NONINTERACTIVE (car ff) (car ff) )))

(set! layer (vector-ref (cadr (gimp-image-get-layers image)) 0))

(set! layers (car (gimp-image-get-layers image)))

(set! visible (car (gimp-drawable-get-visible layer)))

(set! filename (string-append (substring (car ff) 0 (- (string-length (car ff)) 4)) "_del.xcf"))

(while (>= layers 0)

(if (= visible 0) (gimp-drawable-delete layer))

(set! layer (vector-ref (cadr (layer image)) 0))

(set! layers (- layers 1))))

(gimp-file-save RUN-NONINTERACTIVE image image filename filename)

(gimp-image-delete image)

(set! ff (cdr ff))

)

(gimp-quit 0)

)

EOF"

Statistics: Posted by christian urgese — Wed Feb 08, 2017 1:01 pm

]]>

- Code:
`(define test 10)`

(define test-proc (lambda (n) (+ n n))

Then you have this abbrivation for the special case where the value is a procedure:

- Code:
`(define (test-proc n) (+ n n))`

It's the same as the other expression making the variable test-proc. If you look at your definition you see that you do both. So in reality you have.

- Code:
`(define proc`

(lambda (n) ; this is the first layer. The procedure the variable proc will become

(lambda (n) ...) ; this is the procedure the procedure proc will return

Looking at the arguments you have n both places so the previous will definitely be overshadowed by the second. Perhaps you didn't want it to return a new procedure? In that case just remove one:

- Code:
`(define (func n)`

(cond ((= n 0) 0) ;;If n=0, return 0

((= n 1) 2) ;;If n=1, return 2

((= n 2) 37) ;;If n=2, return 37

((odd? n) (+ (func (- n 3) 1))) ;;If n is odd, return f(n-3) + 1

(else (+ (func (- (/ n 2) 1)) 7)))) ;;Else return f((n/2) - 1) + 7

I noticed you are checking if the procedure odd? is the same number as n. This will never work as odd? is not a number at all but a procedure that checks if it's argument is an odd number. (odd? n) perhaps what you wanted?

In DrRacket, after pressing CTRL+i, to fix identation, I noticed the identation was off and thus that means your parentheses most likely are not correct and your program will fail (or worse, do something else than you think).

I changed it so that else comes on the level of cond. Now you can call it like this:

- Code:
`(func 10) ; ==> 16`

As for an example of procedures that return procedures. Imagine you have a procedure that sums two squares and divides:

- Code:
`(define (sum-square a b)`

(+ (* a a)

(* b b)))

(define (divide-square a b)

(/ (* a a)

(* b b)))

Now you see a pattern there so you must be able to abstract that so it becomes smaller:

Now you could do this:

- Code:
`(define (proc-square proc a b)`

(proc (* a a)

(* b b)))

;; new version of sum-square

(define (sum-square a b)

(proce-square + a b))

;; divide the square version

(define (divide-square a b)

(proc-square / a b))

But you could also make use of closures and do it like this:

- Code:
`(define (proc-square proc)`

(lambda (a b)

(proc (* a a)

(* b b))))

;; new version of sum-square

(define sum-square (proce-square +))

;; divide the square version

(define divide-square (proc-square /))

In this case it's a matter of taste, but in procedures where you need to use map or fold and need a procedure this wuld be the simplest and you don't even need to use define but pass it directly.

Statistics: Posted by sylwester — Tue Oct 25, 2016 6:22 am

]]>

(define (func n)

(lambda (n)

(cond ((= n 0) 0) ;;If n=0, return 0

((= n 1) 2) ;;If n=1, return 2

((= n 2) 37) ;;If n=2, return 37

((= n odd?) (+ (func (- n 3)) 1) ;;If n is odd, return f(n-3) + 1

(else (+ (func (- (/ n 2) 1)) 7))))) ;;Else return f((n/2) - 1) + 7

But when I make a call, it doesn't compute it. For example, when i type (func 17), it returns "#<procedure>". So how do I make it compute a call?

Statistics: Posted by NewToLisp — Thu Oct 20, 2016 2:37 pm

]]>

Can anyone review my code? This is exercise 1.28 from SICP.

Description:

"Exercise 1.28: One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; Rabin 1980). This starts from an alternate form of Fermat’s Little Theorem, which states that if n is a prime number and a is any positive integer less than n, then a raised to the n-1-st power is congruent to 1 modulo n. To test the primality of a number n by the Miller-Rabin test, we pick a random number a < n and raise a to the n-1-st power modulo n using the expmod procedure. However, whenever we perform the squaring step in expmod, we check to see if we have discovered a “nontrivial square root of 1 modulo n,” that is, a number not equal to 1 or n-1 whose square is equal to 1 modulo n. It is possible to prove that if such a nontrivial square root of 1 exists, then n is not prime. It is also possible to prove that if n is an odd number that is not prime, then, for at least half the numbers a < n, computing a^n-1 in this way will reveal a nontrivial square root of 1 modulo n. (This is why the Miller-Rabin test cannot be fooled.) Modify the expmod procedure to signal if it discovers a nontrivial square root of 1, and use this to implement the Miller-Rabin test with a procedure analogous to fermat-test. Check your procedure by testing various known primes and non-primes. Hint: One convenient way to make expmod signal is to have it return 0."

https://mitpress.mit.edu/sicp/chapter1/node17.html

Here is my code:

(define (square x) (* x x))

(define (expmod base exp m)

(define (expmod-iter a base exp)

(define squareMod (remainder (square base) m))

(cond ((= exp 0) a)

((and (not (= base (- m 1)))

(not (= base 1))

(= squareMod 1))

0)

((even? exp)

(expmod-iter

a

squareMod

(/ exp 2)))

(else

(expmod-iter

(remainder (* a base) m)

base

(- exp 1)))))

(expmod-iter

1

base

exp))

(define (miller-rabin-test n)

(define (try-it a)

(= (expmod a n n) 1))

(try-it (+ 1 (random (- n 1)))))

(define (fast-prime? n times)

(cond ((= times 0) true)

((miller-rabin-test n)

(fast-prime? n (- times 1)))

(else false)))

http://paste.lisp.org/display/323708

Did I follow the exercise? Is this correct? Are there bugs? Thanks for any feeback!

Statistics: Posted by morbidCode — Fri Aug 26, 2016 2:49 am

]]>

Statistics: Posted by MadMuppet006 — Sun Jun 05, 2016 12:16 am

]]>

These are the same and makes a list of two elements

- Code:
`(cons '+ (cons + '()))`

`(+ . (,+ . ()))

`(+ ,+)

; ==> (+ #procedure:+)

(car `(+ ,+)) ; ==> +

(cadr `(+ ,+)) ; ==> #procedure:+

The elements you ended up with saves one cons by storing the two values in car and cdr, a single pair:

- Code:
`(cons '+ +)`

`(+ . ,+)

; ==> (+ . #procedure:+)

(car `(+ . ,+)) ; ==> +

(cdr `(+ . ,+)) ; ==> #procedure:+

Thus in your own procedure to use lists instead of dotted you would need to replace one cdr with a cadr. However if you don't you get a list of one procedure instead of a procedure.

Statistics: Posted by sylwester — Tue May 31, 2016 2:57 pm

]]>

- Code:
`(define a-list `((+ . ,0+)(* . ,0*)(- . ,0-)(/ . ,0/)(0^ . ,0^)))`

;;; where 0+ etc are prewritten procedures could have used + -

(define value

(lambda (n)

(if (atom- n)

n

(let ((op (car (cdr n))))

(let ((f (assq op a-list)))

(if f

((cdr f)(car n)(value (car (cdr (cdr n)))))

#f)))))

and managed to simplify it down to ..

- Code:
`(define value`

(lambda (n)

(if (atom- n)

n

((lambda (f)

(if f

(f (car n)(value (caddr n)))

#f))

(cdr (assq (cadr n) a-list))))))

this code is good for questions of (2 + (3 * 4)) type and things like (+ (2 + 3)(3 * 4)) are a seperate procedure in the litte schemer where I got the inspiration for this procedure from.

the real question for me now is why the association list has to be written the way it is with dot notation ..

thanks for all the help .. really appreciated

Statistics: Posted by MadMuppet006 — Sat May 28, 2016 5:17 am

]]>

In val you have double parentheses in an expression so the 0+ is applied with zero arguments and the result is then stored as f. It's most likely a number so then you are trying to apply it as a procedure.

Another thing I see is that your evaluator doesn't evaluate the first argument.. Thus '(+ (- 3 1) (- 3 2)) would fail.. In the default case your evaluator must find the procedure, then evaluate the second and third argument since both can be expressions. In the end you need to apply the values like this:

- Code:
`(define (evaluator expression)`

(if (atom? expression)

expression

(let ((proc (find-proc (car expression)))

(arg1 (evaluator (cadr expression)))

(arg2 (evaluator (caddr expression))))

(proc arg1 arg2))))

(evaluator '(+ (- 3 1) (- 3 2))) ;==> 3

The assoc:

- Code:
`(define *procedures* `((+ ,+)(* ,*)(- ,-)(/ ,/)))`

(define *sym* '+)

(assoc *sym* *procedures*) ; ==> (+ #procedure)

((cadr (assoc *sym* *procedures*)) 1 3) ; ==> 4

Statistics: Posted by sylwester — Thu May 26, 2016 8:05 am

]]>