12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <!DOCTYPE html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><meta name="keywords" content="GNU, Emacs, Libre Software, Hurd, Guile, Guix" /><meta name="description" content="GNUcode.me is a website focusing on libre software projects, especially the GNU project." /><link type="application/atom+xml" rel="alternate" title="GNUcode.me -- Feed" href="/feed.xml" /><a rel="me" href="https://fosstodon.org/@thegnuguy"></a><link type="text/css" href="css/footer.min.css" rel="stylesheet"></link><link type="text/css" href="css/header.min.css" rel="stylesheet"></link><link type="text/css" href="css/main.min.css" rel="stylesheet"></link><title>The Power of Scheme — GNUcode.me</title></head><body><header><nav><ul><li><a href="index.html">GNUcode.me</a></li><li><a href="services.html">Services</a></li><li><a href="about.html">About</a></li><li><a href="business-ideas.html">Business-ideas</a></li></ul></nav></header><h1>The Power of Scheme</h1><main><section class="basic-section-padding"><article><h3>by Joshua Branson — January 22, 2021</h3><div><p>I am currently building a sway service for GNU Guix. Some of the videos for
- that are
- <a href="https://video.hardlimit.com/my-account/video-playlists/06baf279-76ea-406a-a24d-2fcfefecbf35">here.</a></p><p>The following blog post is going to show one recent mental goof I had recently.
- Please bear in mind, that I am still learning scheme, and what I am saying is an
- attempt to think out loud (thought-vomit) and may not reflect reality. This is
- an attempt to clarify my thoughts and better understand scheme, particularly GNU
- Guile.</p><p>Consider this simplified scheme function (the original used <code>match-lambda</code>).</p><pre><code class="language-scheme">(define (proc x)
- (lambda (x)
- (display x)))</code></pre><p>A beginner scheme programmer (like me), would look at this procedure and assume
- that there is <em>no correct</em> way to call this procedure. For example:</p><pre><code class="language-scheme">(proc "Hello World\n") ;; compiles and runs but does not display Hello World.
- (proc) ;; This creates a compile error
- ((proc "Hello World")) ;; This create a compile error</code></pre><p>So I decided to try to be helpful. If Guile compiles the first line of code,
- but it <em>cannot</em> run correctly, then why compile it? So I filed a bug report with
- <a href="http://issues.guix.gnu.org/46014">GNU Guile developers</a>. (The GNU Guile devs
- are fantastic people by the way. We actually had a pretty cool exchange back
- and forth). Well, it turns out that the correct way to call the above procedure
- is this:</p><pre><code class="language-scheme">((proc <whatever value you want here>) "Hello World")</code></pre><p>Essentially the above <code>proc</code> is the same thing as this:</p><pre><code class="language-scheme">(define proc
- (lambda (x)
- (lambda (x)
- x)))</code></pre><p>So <code>proc</code> is a procedure that takes one argument, and returns a procedure that
- takes one argument. The two <code>x</code>s there are not related. One should probably
- write the above procedure as:</p><pre><code class="language-scheme">(define proc
- (lambda (x)
- (lambda (y)
- y)))</code></pre><p>This hopefully shows that <code>x</code> and <code>y</code> are two different values.</p><p>Occasionally as I am writing GNU Guile code, Guile will tell me that an error
- occurred, but fail to report the error line number. I suspect that has to do
- with macro shenanigans. Just today I discovered such an error and made a commit
- with it
- <a href="https://notabug.org/jbranso/guix/commit/7a003d066c2bf09370f8196651f82e636bfae0aa">here</a>,
- so that later I can simplify that file down to its bare essentials, and submit
- another guile report or talk to some guile people about it.</p><p>Here is the error in a simplified form:</p><pre><code class="language-scheme">(use-modules (guix records))
- (define-record-type* <sway-bindsym>
- sway-bindsym make-sway-bindsym
- sway-bindsym?
- (key-combo sway-bindsym-key-combo
- (default "")))
- (display sway-bindsym) ;; compile error at unknown location</code></pre><p>Here is the compile error output:</p><pre><code>;;; note: source file /home/joshua/prog/guile/test.scm
- ;;; newer than compiled /home/joshua/.cache/guile/ccache/3.0-LE-8-4.4/home/joshua/prog/guile/test.scm.go
- ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
- ;;; or pass the --no-auto-compile argument to disable.
- ;;; compiling /home/joshua/prog/guile/test.scm
- ;;; WARNING: compilation of /home/joshua/prog/guile/test.scm failed:
- ;;; Syntax error:
- ;;; unknown location: source expression failed to match any pattern in form sway-bindsym
- ice-9/psyntax.scm:2800:12: In procedure syntax-violation:
- Syntax error:
- unknown location: source expression failed to match any pattern in form sway-bindsym</code></pre><p>What I needed to type at that last line was:</p><pre><code>(display (sway-bindsym)) ;; correct</code></pre><p><code>(sway-bindsym)</code> is a macro that I believe calls the macro
- <code>define-record-type*</code>. So I imagine that it is hard for guile to pinpoint,
- where the error is.</p><p>Anyway, I am really liking coding in GNU Guile. It is super fun and awesome. I
- just recently discovered <code>match-lambda</code>, and it's a fantastic GNU Guile macro to
- deal with pass around records.</p></div></article></section></main><footer><p>© 2020 Joshua Branson. The text on this site is free culture under the Creative Commons Attribution Share-Alike 4.0 International license.</p><p>This website is build with Haunt, a static site generator written in Guile Scheme. Source code is <a href="https://notabug.org/jbranso/gnucode.me">available.</a></p><p>The color theme of this website is based off of the famous <a href="#3f3f3f" target="_blank">zenburn</a> theme.</p></footer></body>
|