the-power-of-scheme.html 5.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <!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
  2. that are
  3. <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.
  4. Please bear in mind, that I am still learning scheme, and what I am saying is an
  5. attempt to think out loud (thought-vomit) and may not reflect reality. This is
  6. an attempt to clarify my thoughts and better understand scheme, particularly GNU
  7. Guile.</p><p>Consider this simplified scheme function (the original used <code>match-lambda</code>).</p><pre><code class="language-scheme">(define (proc x)
  8. (lambda (x)
  9. (display x)))</code></pre><p>A beginner scheme programmer (like me), would look at this procedure and assume
  10. that there is <em>no correct</em> way to call this procedure. For example:</p><pre><code class="language-scheme">(proc &quot;Hello World\n&quot;) ;; compiles and runs but does not display Hello World.
  11. (proc) ;; This creates a compile error
  12. ((proc &quot;Hello World&quot;)) ;; This create a compile error</code></pre><p>So I decided to try to be helpful. If Guile compiles the first line of code,
  13. but it <em>cannot</em> run correctly, then why compile it? So I filed a bug report with
  14. <a href="http://issues.guix.gnu.org/46014">GNU Guile developers</a>. (The GNU Guile devs
  15. are fantastic people by the way. We actually had a pretty cool exchange back
  16. and forth). Well, it turns out that the correct way to call the above procedure
  17. is this:</p><pre><code class="language-scheme">((proc &lt;whatever value you want here&gt;) &quot;Hello World&quot;)</code></pre><p>Essentially the above <code>proc</code> is the same thing as this:</p><pre><code class="language-scheme">(define proc
  18. (lambda (x)
  19. (lambda (x)
  20. x)))</code></pre><p>So <code>proc</code> is a procedure that takes one argument, and returns a procedure that
  21. takes one argument. The two <code>x</code>s there are not related. One should probably
  22. write the above procedure as:</p><pre><code class="language-scheme">(define proc
  23. (lambda (x)
  24. (lambda (y)
  25. 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
  26. occurred, but fail to report the error line number. I suspect that has to do
  27. with macro shenanigans. Just today I discovered such an error and made a commit
  28. with it
  29. <a href="https://notabug.org/jbranso/guix/commit/7a003d066c2bf09370f8196651f82e636bfae0aa">here</a>,
  30. so that later I can simplify that file down to its bare essentials, and submit
  31. 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))
  32. (define-record-type* &lt;sway-bindsym&gt;
  33. sway-bindsym make-sway-bindsym
  34. sway-bindsym?
  35. (key-combo sway-bindsym-key-combo
  36. (default &quot;&quot;)))
  37. (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
  38. ;;; newer than compiled /home/joshua/.cache/guile/ccache/3.0-LE-8-4.4/home/joshua/prog/guile/test.scm.go
  39. ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
  40. ;;; or pass the --no-auto-compile argument to disable.
  41. ;;; compiling /home/joshua/prog/guile/test.scm
  42. ;;; WARNING: compilation of /home/joshua/prog/guile/test.scm failed:
  43. ;;; Syntax error:
  44. ;;; unknown location: source expression failed to match any pattern in form sway-bindsym
  45. ice-9/psyntax.scm:2800:12: In procedure syntax-violation:
  46. Syntax error:
  47. 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
  48. <code>define-record-type*</code>. So I imagine that it is hard for guile to pinpoint,
  49. where the error is.</p><p>Anyway, I am really liking coding in GNU Guile. It is super fun and awesome. I
  50. just recently discovered <code>match-lambda</code>, and it's a fantastic GNU Guile macro to
  51. 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>