Planet Scheme

April 01, 2020

GNU Guix

Deprecating support for the Linux kernel

After years in the making, Guix recently gained support for running natively on the GNU/Hurd operating system. That means you will soon be able to replace...

(kernel linux-libre)

with

(kernel hurd)
(initial-herd hurd)

...in your operating-system declaration and reboot into the future!

Running on the Hurd was always a goal for Guix, and supporting multiple kernels is a huge maintenance burden. As such it is expected that the upcoming Guix 1.1 release will be the last version featuring the Linux-Libre kernel. Future versions of Guix System will run exclusively on the Hurd, and we expect to remove Linux-Libre entirely by Guix 2.0.

The Linux kernel will still be supported when using Guix on "foreign" distributions, but it will be on a best-effort basis. We hope that other distributions will follow suit and adopt the Hurd in order to increase security and freedom for their users.

We provide a pre-built virtual machine image with the Hurd for download with SHA256 056e69ae4b5fe7a062b954a5be333332152caa150359c20253ef77152334c662.

Here is how to get started:

wget https://guix.gnu.org/guix-hurd-20200401.img.tar.xz
tar xf guix-hurd-20200401.img.tar.xz
guix environment --ad-hoc qemu -- \
    qemu-system-i386 -enable-kvm -drive file=guix-hurd-20200401.img,cache=writeback -m 1G

Log in as root without password. Then try hello, guix describe, or guix install linux-libre to run Linux in userspace... We are looking forward to your feedback!

About GNU Guix

GNU Guix is a transactional package manager and an advanced distribution of the GNU system that respects user freedom. Guix can be used on top of any system running the Hurd or the Linux kernel, or it can be used as a standalone operating system distribution for i686, x86_64, ARMv7, and AArch64 machines.

In addition to standard package management features, Guix supports transactional upgrades and roll-backs, unprivileged package management, per-user profiles, and garbage collection. When used as a standalone GNU/Linux distribution, Guix offers a declarative, stateless approach to operating system configuration management. Guix is highly customizable and hackable through Guile programming interfaces and extensions to the Scheme language.

by Jan (janneke) Nieuwenhuizen, Ludovic (civodul) Courtès, Marius (mbakke) Bakke, Ricardo (rekado) Wurmus at April 01, 2020 11:00 PM

March 27, 2020

Programming Praxis

List Rotation

[ I’ve been busy at work this week with virus stuff. I’m working at home, which is less productive than at the office. And we are making accommodations regarding the virus for our students, which requires a lot of urgent work. So, a quick little exercise today. ]

Given a length-n list like (a b c d e), the rotations of the list are the n lists (a b c d e), (b c d e a), (c d e a b), (d e a b c), and (e a b c d), in any order.

Your task is to write a program that takes a list and returns its rotations. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at March 27, 2020 09:00 AM

March 20, 2020

Programming Praxis

Homework

Today’s exercise is somebody’s homework:

Write a program that displays the digits from 1 to n then back down to 1; for instance, if n = 5, the program should display 123454321. You are permitted to use only a single for loop.

The questioner did not specify what should happen when n reaches 10, so we will specify 0 < n < 10.

Your task is to write the requested program; if you like, think of other ways to write that program. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at March 20, 2020 09:00 AM

March 17, 2020

Programming Praxis

CSV To HTML

Today’s exercise is another in my continuing escapades in “stealth programming” using awk. I frequently write programs that produce CSV files as output. Most of the time the output file is loaded into Excel by the user. Sometimes the CSV file must be printed as well as loaded into Excel, and I wrote a program to do that in a previous exercise. I recently had a request to produce the output in HTML format, so I wrote that program yesterday.

Your task is to write a that converts a CSV file to HTML output; use whatever conventions make sense to you. When you are finished, you are welcome to read a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at March 17, 2020 09:00 AM

March 13, 2020

Programming Praxis

Perfect Shuffle

A perfect shuffle, also known as a faro shuffle, splits a deck of cards into equal halves (there must be an even number of cards), then perfectly interleaves them. Eventually a series of perfect shuffles returns a deck to its original order. For instance, with a deck of 8 cards named (1 2 3 4 5 6 7 8), the first shuffle rearranges the cards to (1 5 2 6 3 7 4 8), the second shuffle rearranges the cards to (1 3 5 7 2 4 6 8), and the third shuffle restores the original order (1 2 3 4 5 6 7 8).

Your task is to write a program that performs a perfect shuffle and use it to determine how many perfect shuffles are required to return an n-card deck to its original order; how many perfect shuffles are required for a standard 52-card deck? When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at March 13, 2020 09:00 AM

March 10, 2020

Programming Praxis

2SUM

We’ve done this in a previous exercise, but it’s a common problem both as an interview question and in programming classes, and I’ve seen in it several times in the last week, so now is a good time to do it again:

Given a list of integers and a target integer, find all the pairs of integers in the list that sum to the target integer, or report that there are no such pairs.

Your task is to write a program to find pairs of integers that sum to a target; you should write three programs, with time complexities of O(n²), O(n log n), and O(n). When you are finished, you are welcome to read a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at March 10, 2020 09:00 AM

March 06, 2020

Programming Praxis

Squares Of A Sorted Array

This problem has been going around the internet the last few days. I’ve seen it on several message boards, though I don’t know the original source:

Given an array of integers sorted in non-decreasing order, return an array of the squares of each number, also sorted in non-decreasing order. For instance, given (-4 -1 0 3 10), the desired output is (0 1 9 16 100).

Your task is to write a program to compute the sorted squares of a sorted array. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at March 06, 2020 10:00 AM

March 03, 2020

Programming Praxis

Two Easy Tasks

Many of the tasks I publish on this blog come from beginning-programmer forums. Here is a task taken from some sort of entrance examination:

Jason rings every multiple of 13 less than 500. He then crosses every multiple of 17 less than 500. How many numbers get both ringed and crossed?

The test is a multiple-choice examination with possible selections 10, 0, 1 and 4. The solution sheet shows the correct answer is 4. The questioner who posted this question was asking how to calculate the answer given on the solution sheet.

And here is another simple task:

Given positive integer n < 1018, find the sum of the integers from 1 to n, mod 109 + 7. Assume you are using a language that provides 64-bit arithmetic, so no intermediate results can be larger than 264.

Your task is to write a program to solve these two tasks. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at March 03, 2020 10:00 AM

March 02, 2020

Göran Weinholt

Loko Scheme 0.4.3

Loko Scheme 0.4.3 is now out with a few important fixes, new features and network card drivers for eepro100, rtl8139, virtio net and Linux tuntap devices. The include form is now available and #u8() is recognized. Hashtables are written using Racket’s #hasheq() syntax, and cycles are handled while printing records.

Read more about the drivers in the companion article Device Drivers in Loko Scheme.

March 02, 2020 12:00 AM

February 29, 2020

GNU Guix

GSoC 2020 and Outreachy May 2020 to August 2020 Status Report II

GSoC

We are happy to announce that GNU Guix participates in the Google Summer of Code (GSoC), under the aegis of the GNU project. We have collected project ideas related to GNU Guix. The list is far from exhaustive, so feel free to bring your own!

The GNU Project participation was announced on Feb. 20. Thanks for the GNU org admins for organizing this.

The application period is from March 16. to March 31. The final proposal submission deadline is March 31., 2020 at 20:00 CEST.

The student projects are announced on April 27., 2020. We will have to provide the number of slots requested to the GNU project, so that they can accumulate the numbers to pass on to Google. This takes some time, so please prepare the decision early, so we don't have to hurry when this information is requested. We kindly remind everyone involved not to communicate an intern selection decision before the official announcement.

Internship information

Information about GSoC internships related to the GNU Guix community.

Outreachy

We are happy to announce that GNU Guix offers a three-month internship through Outreachy, the inclusion program for groups traditionally underrepresented in free software and tech.

We currently propose three subjects to work on:

  1. Create Netlink bindings in Guile.
  2. Improve internationalization support for the Guix Data Service.
  3. Integration of desktop environments into GNU Guix.

The initial application deadline was on Feb. 25, 2020 at 4PM UTC, so initial applications are now closed. This means that prospective applicants how did not do the initial application yet will have to apply for a later Outreachy round.

The project list was finalized on Feb. 27. This means that no new proposals can be added for this round, so the list above is final.

Funding was confirmed for two internships this round. Thanks for the members of the spending committee, who are taking care of the financial side of this.

Co-mentor applications are still open, prospective mentors are encouraged to apply on the Guix community page.

The next phase is the contribution period: March 5, 2020 to April 7, 2020. Also the final application deadline is April 7, 2020 at 4PM UTC.

In this phase applicants are working with the mentors on the projects, and they have to register a contribution. A registered contribution is a mandatory requirement.

Accepted interns will be announced on April 27, 2020 at 4PM UTC. We kindly remind everyone involved not to communicate an intern selection decision before the official announcement.

Internship information:

Last year we had the pleasure to welcome Laura Lazzati as an Outreachy intern working on documentation video creation, which led to the videos you can now see on the home page.

Information about Outreachy internships related to the GNU Guix community.

Contributions

If you’d like to contribute to computing freedom, Scheme, functional programming, or operating system development, now is a good time to join us. Let’s get in touch on the mailing lists and on the #guix channel on the Freenode IRC network!

About GNU Guix

GNU Guix is a transactional package manager and an advanced distribution of the GNU system that respects user freedom. Guix can be used on top of any system running the kernel Linux, or it can be used as a standalone operating system distribution for i686, x86_64, ARMv7, and AArch64 machines.

In addition to standard package management features, Guix supports transactional upgrades and roll-backs, unprivileged package management, per-user profiles, and garbage collection. When used as a standalone GNU/Linux distribution, Guix offers a declarative, stateless approach to operating system configuration management. Guix is highly customizable and hackable through Guile programming interfaces and extensions to the Scheme language.

by Gábor Boskovits at February 29, 2020 03:00 PM

February 28, 2020

Programming Praxis

String Rotations

A string like abc has three rotations: abc, bca, and cab.

Your task is to write a program that computes all the rotations of a string. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at February 28, 2020 10:00 AM

February 25, 2020

Programming Praxis

Pythagorean Quadruple

A Pythagorean quadruple consists of four positive integers a, b, c and d such that abcd and a² + b² + c² = d². For instance, (2 3 6 7) is a Pythagorean quadruple because 2² + 3² + 6² = 4 + 9 + 36 = 49 = 7².

Your task is to write a program that counts the Pythagorian quadruples with a, b, c less than or equal to some given N, and compute the number of Pythagorian quadruples with N = 1000. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at February 25, 2020 10:00 AM

February 24, 2020

Greg Hendershott

The Big Switcheroo

The Big Switcheroo

:: Racket, Emacs

In my previous post I talked about how Racket Mode now will often want the back end command server, before actually needing a live REPL — but it has to start a REPL anyway. This was bothering me. So I went on to address that. Doing so entailed reversing the I/O model for the back end. As a bonus, that set up fairly short strokes to supporting multiple REPLs.

In the beginning, Racket Mode was a thin wrapper around xrepl. As a result, it used a comint-mode buffer to start Racket, and the REPL stdin/stdout was connected to the comint-mode buffer. The back end also started a little TCP server; connecting to that established the I/O for commands.

This was mostly fine, especially when racket-run tended to be the first command you would use.

However it might take a second or two for the TCP server to be ready to accept connections. As a result, the front end either needed to block (boo!) or use an Emacs timer to retry at, say, one second intervals until success. That meant some extra complexity. More basically, it meant more delay until a command request and response could occur.

Flip

So I decided to flip that around. Now, the stdin/stdout of the back end process is the command request/response channel. The same format of sexpr command requests and responses are sent — they just happen to go over ports that are stdin/stdout instead of TCP ports. The TCP server is for creating a REPL session, if/when that is desired. Make a TCP connection, read a session ID, et voila you have a new REPL session. The back end parameterizes the REPL’s current-{input output error}-mode to the TCP ports. And fortunately, comint-mode makes it as easy to connect the buffer to I/O from a TCP connection, as to stdin/stdout of a process.

The other thing is, a process can have only one stdin/stdout. But of course it can accept multiple TCP connections. Now that we use the latter for REPL I/O, this removes a basic barrier to an old feature request: Supporting multiple REPLs would now be possible.

Of course “possible” is different than “implemented and working correctly”. So at first I focused mainly on the back end: Make sure that it wasn’t using global variables when it could use per-thread parameters, for things related to the REPL. Add a lookup table from unique REPL session IDs to information about each REPL session. Have the front end obtain the session ID and supply it later with commands — but otherwise, for now, the front end still just created one REPL session at a time.

Multiple REPLs

After some time to get that working, I wanted to tackle supporting multiple REPLs in the front end. For a day or two, I procrastinated. I really did not want to write a bunch of “ceremonial” code related to REPL sessions. I did not want some whole UI for that, complete with more code and bugs.

I’m glad I hesitated, because I realized something simple. Emacs already has the concept that variable values can be global to all buffers — but also can be set so that a buffer has its own, local value.

So, assume a variable racket-repl-buffer-name. It only has meaning for a racket-mode buffer. It means, “what is the name of the racket-repl-mode buffer that commands like racket-run should use”. Initially such a variable could have a global value, *Racket REPL*. That default is the status quo behavior: All edit buffers take turns sharing the same, one and only REPL buffer.

Now, what if a racket-mode buffer for foo.rkt did a (setq-local racket-repl-buffer-name "*Racket REPL foo.rkt*")? Now commands issued with that buffer selected would use (creating if necessary) a REPL buffer of that name. If every edit buffer did this, you’d have one REPL per edit buffer (much like DrRacket).

You could also imagine wanting edit buffers for files that are in the same projectile project, to share a REPL buffer: “One REPL per project”. OK: setq-local their racket-repl-buffer-name to some common name that includes the project name or root directory.

And so that is the mechanism I went with. A customization variable racket-repl-buffer-name-function points to a function that will set the racket-repl-buffer-name variable value for a racket-mode edit buffer when it is created. I defined three such functions, for the three strategies described above. And best of all, as a user you can supply your own such function.

I also added a kill-buffer-hook: When you kill a racket-mode edit buffer, and it is the last one using a racket-repl-mode buffer, we y-or-n-prompt offering to kill the REPL buffer, too. I figure that will help keep things cleaner, especially for the case where people want 1:1 edit:REPL buffers.

As a result, there is no Big UI about “sessions”. There are just edit buffers, and names of the REPL buffer(s) each should use. Because it rides directly on Emacs semantics for variables and buffers, there is non-trivial code that I did not need to write — and “out of sync” bugs that I could not create.

That cleaving feeling

Of course there are some bugs lurking, about which I don’t yet know. Someday I’ll probably feel discouraged about this code, for reasons I don’t yet know. A lot of programming “innovation” amounts to trading an old set of problems for a new set. Sometimes the new problems aren’t better, they just feel better, at first, because novelty.

All stipulated and admitted. Even so, I also feel this was one of those situations where, finally, I saw a way to define a problem so that it aligns with the material I had to work with: Just a few gentle taps, and the material cleaves along the lines I need and want. That is a pretty awesome feeling. While it lasts.

Availability

The stuff I’m describing in this blog post initially lived on a new-comm branch. I did just merge it back to the check-syntax branch. It’s not yet merged to master. I’m still reviewing documentation, dogfooding, and such. I’m coming up on two full months working on this, so, I’d like to merge to master in the near future.

by Greg Hendershott at February 24, 2020 12:00 AM

February 21, 2020

Programming Praxis

Formatted Dates

Regular readers of this blog know that I work with a team of programmers, sysadmins and database administrators to maintain a large legacy database application, running on Oracle and HP-UX, and Scheme is nowhere in sight. Lately I have been “stealth programming” by writing awk programs in shell wrappers, because shell programming is a normal part of our environment. One thing I have been doing is formatting reports with awk. That frequently requires a formatted date string, either for today or some other day; gawk provides the strftime function to format dates, but Posix awk, which is what HP-UX provides, doesn’t. So I wrote my own.

Your task is to write a function that formats dates; use any convention you like to determine how the date is formatted. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

by programmingpraxis at February 21, 2020 10:00 AM

February 20, 2020

Joe Marshall

Stupid pattern matching tricks

There are a few pattern matching constructs in Common Lisp. For instance, destructuring-bind matches list structure against a tree of variable names and binds the variables accordingly. Macros can destructure their argument list. Even functions have simple keyword matching. These constructs don't give access to their pattern matchers as first-class objects, but perhaps you want that. You can construct a simple pattern matcher by wrapping one of these constructs in the appropriate macro.

We'll want the result of our pattern match to be an alist mapping symbols to the objects they matched with. First, we'll need a function that takes a pattern and returns a list of the variables in the pattern. flatten will work nicely for destructuring-bind:
(defun flatten (pattern)
  (cond ((null pattern) '())
 ((symbolp pattern) (list pattern))
 ((consp pattern) (append (flatten (car pattern))
     (flatten (cdr pattern))))
 (t (error "Not a pattern"))))

CL-USER> (flatten '((a b . c) d e . f))
(A B C D E F)
Then we want to generate code that will make an alist:
CL-USER> `(list ,@(map 'list (lambda (var)
           `(cons ',var ,var))
               (flatten '((a b . c) d e . f))))
(LIST (CONS 'A A) (CONS 'B B) (CONS 'C C) (CONS 'D D) (CONS 'E E) (CONS 'F F))
Finally, we wrap a call to destructuring-bind with a macro:
CL-USER> (defmacro destructuring-pattern-matcher (pattern)
           `(lambda (form)
              (destructuring-bind ,pattern form
                (list ,@(map 'list (lambda (var)
                              `(cons ',var ,var))
                     (flatten pattern))))))
DESTRUCTURING-PATTERN-MATCHER

CL-USER> (destructuring-pattern-matcher ((a b . c) d e . f))
#<FUNCTION (LAMBDA (FORM)) {10027B143B}>
destructuring-pattern-matcher returns a pattern matcher as a first-class procedure we can call on a pattern to get an alist of bindings:
CL-USER> (defvar *matcher* (destructuring-pattern-matcher ((a b . c) d e . f)))
*MATCHER*

CL-USER> (funcall *matcher* '((1 2 3 4) 5 6 7 8))
((A . 1) (B . 2) (C 3 4) (D . 5) (E . 6) (F 7 8))

We can use this trick to get at the destructuring pattern match done by defmacro. First, we need a function that takes a macro lambda list and returns a list of the variables it binds. I won't reproduce the function here, it is too large, but here's a sample call:
CL-USER> (macro-lambda-list-variables 
            '((foo bar &optional (baz 'default baz-supplied-p) . more) quux
              &rest rest
              &key ((:key key-variable) 'key-default key-supplied-p) key2
              &aux (auxvar 'auxvalue)))
(FOO BAR BAZ BAZ-SUPPLIED-P MORE QUUX REST KEY-VARIABLE KEY-SUPPLED-P KEY2 AUXVAR)
If we were matching the list '(1 e) against the pattern (a b &optional c), we'd want to generate code something like this:
(MACROLET ((MACRO (A B &OPTIONAL C)
             (LIST 'LIST
     (LIST 'CONS ''A (LIST 'QUOTE A))
     (LIST 'CONS ''B (LIST 'QUOTE B))
                   (LIST 'CONS ''C (LIST 'QUOTE C)))))
  (MACRO 1 E))
We'll do this in stages:
(defun make-macro-pattern-matcher-body (pattern)
  `(list 
    'list
    ,@(map 'list (lambda (var)
     `(list 'cons '',var `',,var))
    (macro-lambda-list-variables pattern))))

(defun make-macro-pattern-matcher (pattern)
  (let ((body (make-macro-pattern-matcher-body pattern)))
    (lambda (form)
      `(macrolet ((macro ,pattern
      ,body))
  (macro ,@form)))))

(defmacro macro-pattern-matcher (pattern)
  (let ((matcher  (make-macro-pattern-matcher pattern)))
    `(lambda (form)
       (eval (funcall ',matcher form)))))
Now we can make a pattern matcher that works like the macro destructuring facility:
CL-USER> (setq *matcher* 
            (macro-pattern-matcher 
       ((foo bar &optional (baz 'default baz-supplied-p) . more) quux
               &rest rest
               &key ((:key key-variable) 'key-default key-supplied-p) key2
               &aux (auxvar 'auxvalue))))
#<FUNCTION (LAMBDA (FORM)) {10027B1D3B}>

CL-USER> (funcall *matcher* '((1 2 3 4) 5 :key 6 :key2 7))
((FOO . 1)
 (BAR . 2)
 (BAZ . 3)
 (BAZ-SUPPLIED-P . T)
 (MORE 4)
 (QUUX . 5)
 (REST :KEY 6 :KEY2 7)
 (KEY-VARIABLE . 6)
 (KEY-SUPPLIED-P . T)
 (KEY2 . 7)
 (AUXVAR . AUXVALUE))
You can do a similar trick with regular lambda lists, but while they have keywords, they don't destructure.

You have to be careful when writing the expansion for the binding alist. Too much quoting and you end up with the names rather than their values in the output:
((foo . foo)
 (bar . bar)
 …etc…)
not enough, you end up with the values of the values in the output:
CL-USER> (defvar e 22)
E

CL-USER> (funcall *matcher* '((1 2 e) 5))
((FOO . 1)
 (BAR . 2)
 (BAZ . 22) ; Wrong! Should be 'Eetc…)

by Joe Marshall (noreply@blogger.com) at February 20, 2020 01:56 AM