DEV Community

loading...

Learning Guile - Booleans

Red Nose Hacker
GNU enthousiast. Free Software & Hardware Craftsman I love you all. Just tell me how can I help ?
・1 min read

Guile Logo

The two boolean values are: "true" and "false". Respectively #t or #true and #f or #false in Guile.

In a conditional test context, "true" means any expression other than #f or #false.

Here is a small test suite that illustrates all this:

(use-modules (srfi srfi-64))

(test-begin "test-suite")

(test-equal "Truth"
  #t
  #true)

(test-equal "Falsness"
  #f
  #false)

(test-equal "Numbers are true"
  #t
  (if 12547
      #t
      #f))

(test-equal "Strings are true"
  #t
  (if "I am not false"
      #t
      #f))

(test-equal "Lists - even empty - are true"
  #t
  (if '()
      #t
      #f))

(test-equal "Symbols are not false"
  #f
  (not 'i-am-not-false))

(test-end "test-suite")

Create a /tmp/bool.scm file with the code below. Run the tests and if everything goes well, you should see the following result:

$ guile bool.scm 
;;;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;; or pass the --no-auto-compile argument to disable.
;;;; compiling /tmp/bool.scm
compiled /home/jeko/.cache/guile/ccache/3.0-LE-8-4.3/tmp/bool.scm.go
%%%% Starting test-suite (Writing full log to "test-suite.log")
# of expected passes 6

If you feel like it, you can tweak this file to experiment!

Discussion (0)