About

This blog is for learning to think with Guile Scheme:

U for Guile
and and
I for Me

Working with Scheme Language will make me more intelligent: I for me.

Learning through examples will make Guile more ubiquoitous: U for guile.

Writings about Guile and Me, might work towards discoveries for “U” and “I”.

Recent Discoveries

Years ago I spent a week with The Schemer's Guide and I read a lot of Paul Graham's On Lisp too. Maybe all that time is starting to take effect now that I'm motivated to work in Guile, but I'm also wondering if well-graded or well-strutured classroom work with lambda , cut and fold- could encourage a quiet approach that lets the mind comprehend complex processes.

cut

The book, プログラミング Gauche (p.84), by the Kahua Project and Shiro Kawai got me started with cut. Modules are needed if you want to play with the ideas in Guile Scheme.

(use-modules (rnrs lists))    ;; find
(use-modules (srfi srfi-26)) ;; cut

(define nums '(1 2 3 4 5 6 7))
(find (lambda (n) (< 3 n)) nums)
(find (cut < 3 <>) nums)

Here is a useful example that uses cut for brevity: let's batch convert image files:

(use-modules (ice-9 ftw)) ;; scandir
(use-modules (ice-9 regex)) ;; string-match
(use-modules (srfi srfi-26)) ;; cut

(define bmps (scandir “./” (cut string-match “jpg$” <>)))

(define bmp2svg
  (lambda (bmp)
    "convert a .jpg  file to .svg file using system calls to ImageMagick's `convert' and then to `potrace'."
    (let ((pnm (replace-file-ext bmp "pnm"))
	  (svg (replace-file-ext bmp "svg")))
      (system* "convert" bmp pnm)
      (system* "potrace" "-b" "svg" "--tight" "-M" ".02" "--opaque" pnm "-o" svg))))
 ;; should really (delete-file pnm) after the potrace call, pnm files are huge.

(for-each bmp2svg bmps)

;; converting the svgs to pngs makes for clearer images that you would get converting directly from jpg files.

(define svg2png
  (lambda (fsvg)
    (let ((fpng (replace-file-ext fsvg "png")))
      (system* "convert" fsvg "-background" "white" "-layers" "merge" "-geometry" "x250" "-bordercolor" "white" "-gravity" "southwest" "-pointsize" "10" "-fill" "grey" "-annotate" "+0+0" "MioSato" fpng))))

(define svgs (scandir “./” (cut string-match “.svg$” <>)))
(for-each svg2png svgs)

;; convenience procedure to work with filenames
;;  I had scripts like this working in Emacs-Lisp using f.el or something but haven't discovered ready-made utilities like that for guile

(define get-file-ext
  (lambda (fname)
    "Get the extension from a filename with string-split and reverse"
    (let ((len (string-length fname)))
      (car (reverse (string-split fname #\.))))))

(define get-ext-len
  (lambda (fname)
     "Get length of file extention using get-file-ext"
    (string-length (get-file-ext fname))))

(define get-file-base
  (lambda (fname)
    "Get basename of file using string-length, get-ext-len, and substring."
    (let ((flen (string-length fname))
	  (elen (get-ext-len fname)))
      (substring fname 0 (- flen (1+ elen))))))

(define add-file-ext
  (lambda (bname fext)
    "Add to a basename a period '.' and a file extension."
    (string-append bname "." fext)))

(define replace-file-ext
  (lambda (fname next)
    "Remove from a filename its extension and replace the extension, using `get-file-base and `add-file-ext'."
    (let ((base (get-file-base fname)))
      (add-file-ext base next))))

I use the image-converstion procedures to blog gdm lessson worksheets. (fn:1)

fold-left fold-right

Programming Gauche also offer brain-training exercises with fold:

(use-modules (rnrs lists)) ;; fold-left , fold-right
(define nums '(1 2 3 4 5 6 7))

(fold-left    - 0 nums) ;; => -28

(fold-right - 0 nums) ;; => 4

I got the feeling that work with fold could lead to an appreciation of recursion, or an approach to following processes with your mind. After a few hours of thought with cut and fold I find that I use them more than I suspected. They turned out to be more than just attention-training exercises to help me get through an awful day of proctoring standardized tests.

Kent Dybvig's TSPL4 and Kahua's Gauche books, notebook, Guile Manual printouts, and PC keyboard

On-line Resources

FootNotes

#Guile #Scheme #ImageConversion #ImageMagick #potrace