A Rough Introduction to Guile Scheme

Updates

  • Next week I will be streaming on this channel around 7AM to 9AM UTC on Monday and Wednesday! We’ll be working on improvements to my Scheme-inspired programming language Mesche.
  • Support the channel! Buy Mastering Emacs with this link https://www.masteringemacs.org/r/systemcrafters

A Rough Introduction to Guile Scheme

Today we’re going to discuss the language Scheme through the lens of Guile Scheme! This will mainly be geared toward those who have some experience with Lisp languages like Emacs Lisp or Common Lisp already. If there’s enough interest, I’ll be happy to create a video series going more in depth!

I’ll give you a rough (unprepared) walkthrough of the following topics:

  • Basic language characteristics
  • Defining variables and functions
  • Setting variables
  • let expressions
  • Looping constructs and higher-order functions
  • Interacting with the environment (console and file I/O)
  • Modules, importing, and exporting
  • Macros
  • Continuations

The code we wrote

(define-module (system-crafters guile-demo)
  #:export (find find-x))

(define my-var 15)

(set! my-var 20)

(define (find value lst)
  (if (pair? lst)
      (begin
        (display (string-append "Looking at: "
                                (number->string (car lst))
                                "\n"))
        (if (equal? (car lst) value)
            (begin (display "Found it!\n")
                   (car lst))
            (find value (cdr lst))))
      #f))

(let find-loop ((value 5)
                (lst '(1 2 3 4 5)))
  (if (pair? lst)
      (begin
        (display (string-append "Looking at: "
                                (number->string (car lst))
                                "\n"))
        (if (equal? (car lst) value)
            (begin (display "Found it!\n")
                   (car lst))
            (find-loop value (cdr lst))))
      #f))

(define find-x (lambda (value lst)
                 (if (pair? lst)
                     (begin
                       (display (string-append "Looking at: "
                                               (number->string (car lst))
                                               "\n"))
                       (if (equal? (car lst) value)
                           (begin (display "Found it!\n")
                                  (car lst))
                           (find value (cdr lst))))
                     #f)))

(letrec ([even? (lambda (n) (or (zero? n) (odd? (- n 1))))]
         [odd? (lambda (n) (and (not (zero? n)) (even? (- n 1))))])
  (even? 4))

(let ((x 5)
      (y (+ x 1)))
  (display (number->string y)))

((lambda (x y)
   (display (number->string y)))
 5
 (+ x 1))

(let* ((x 5)
       (y (+ x 1)))
  y)

((lambda (x)
   ((lambda (y)
      y)
    (+ x 1)))
 5)

(define my-map (lambda (func lst)))

(find-x 3 '(1 2 3 4 5 6))

;; Loading modules
(use-modules (ice-9 textual-ports)
             (ice-9 command-line))

(with-input-from-file "guile.scm"
  (lambda ()
    (display (get-string-n (current-input-port) 20))))
Subscribe to the System Crafters Newsletter!
Stay up to date with the latest System Crafters news and updates! Read the Newsletter page for more information.
Name (optional)
Email Address