Reverse a String in Scheme

Published on 24 December 2018 (Updated: 24 December 2018)

Reverse a String in Scheme

In this article, we’re tackling Reverse a String in Scheme!

How to Implement the Solution

This snippet below shows the whole code you need to reverse a string in Scheme:

(define(reverse-string x)
  (list->string (reverse (string->list x))))
(display (reverse-string (read-line)))

As you can see, we can write a script to reverse a string in three lines of code. In the following sections, we’ll take a look at a breakdown of each of these lines.1

The Function Definition

(define (reverse-string x)

The keyword define binds a function’s definition to the specified name. This keyword is followed by the name of the function and the function’s argument. In this particular case, the argument x is the string we want to reverse.1

The Function Body

(list->string (reverse (string->list x))))

The whole magic happens in the second line. We will go through it step by step from the inside out, since this makes understanding it easier.

The most inner pair of parentheses calls the conversion of our string x to a list. Then, this list is reversed by calling the function reverse which is part of the standard library. Finally, the reversed list is converted back to a string. This is also the return value of the function reverse-string.1

The Display

(display (reverse-string (read-line)))

The first thing you see there, is the display function. You’ll probably remember it from our Hello World experience, so have a look there, if you need a quick refresher of what it does.

The argument to this function is the reverse-string function defined above. It receives the input from the command line, which is accessed with the function read-line, as its string argument. read-line reads a single line from the command line and converts it into a string, so it’s perfect for our application!

If you replace (read-line) with a string, the program prints that string reversed. To show you an example, the output of (display (reverse-string "Hello")) is olleh.1

How to Run Solution

The quickest way is probably to try use an online Scheme interpreter. Just copy the code above, drop it into the editor, fill in some input and hit run.

As an alternative, you can download CHICKEN Scheme and a copy of the solution file from Github. Assuming CHICKEN Scheme is on your path, you can run the script from a command line with the following command:

csi -s reverse-string.scm

This will run the Scheme file which will print out whatever you enter on the command line.1


References

  1. A. Wörner, “Reverse a String in Scheme,” The Renegade Coder, 16-Jul-2018. [Online]. Available: https://therenegadecoder.com/code/reverse-a-string-in-scheme/. [Accessed: 24-Dec-2018].  2 3 4 5