/scheme-misc

Miscellaneous utilities and libraries in Scheme

Primary LanguageSchemeOtherNOASSERTION

scheme-misc

This repository contains miscellaneous bits of scheme code I have written for my own use. There is no overriding theme to the code here.

This collection will grow over time as I write things I want to keep around.

Because this is mostly one-off code I write for specific purposes, it will not necessarily be pretty or well-optimized.

Contents

Utilities

ackermann.scm

One of the few things I wrote for MIT/GNU Scheme. Should work in Guile with minimal modification.

It’s a simple implementation of Ackermann’s function. I implemented the original function, not the one commonly used today.

baseutils.scm

This file contains functions related to converting between numerical bases (such as binary -> decimal).

  • required-digits b1 power b2

    Calculate the number of digits in base b2 to represent a number in base b1 with power digits.

  • convert-int-between-bases b1 nstr b2

    Converts a number in base b2 to base b2.

    The number must be provided as a string. This function supports bases up to base 37 (it could do higher, but we’d need to figure out what to use for digits after we run out of numbers). The return value is also a string.

monty-hall.scm

A script that runs simulations of the Monty Hall problem.

The script does the following:

  1. Choose one door out of three that has a prize
  2. Choose a door at random to be the player’s chosen door
  3. “Open” a door that is neither the prize nor the player’s choice
  4. Give the player the chance to change the chosen door or keep the first choice.

Thinking logically, it seems that the player changing his/her mind shouldn’t affect the outcome. However, the prize has a 2/3 chance of being behind the door the player didn’t choose.

This script was written to satisfy my curiosity. It can run from the command line. With no arguments, it runs 1000 trials each of the player changing his/her mind and not changing his/her mind. Given a numerical argument, it will run each simulation the given number of times. At the end of the simulation, it reports the win rate for each.

print-field.scm

A script that reads lines from standard input and prints the nth whitespace-delimited field. It’s basically similar to =awk ‘{print $n};’=, except completely inferior in every way.

This was written in response to a StackExchange question, and is really only useful as an example.

vectors.scm

This is an old script by me that contains classes for angles as well as 2D and 3D vectors.

I’m not guaranteeing anything about this script. I wrote it a long time ago, back when I was first experimenting with GOOPS (Guile’s object system) and was fairly new at Scheme. It looks useful as an example, but these days I’d put it into a library and comment it.

spauldo Library

This is my personal Scheme library for Guile. It won’t work for any other scheme implementation. Some parts might not work at all - I haven’t used some of this for quite some time, and some parts were written but never tested.

It’s mostly mathematics-related things. I prefer to use Scheme for all mathematics work because of its numerical tower. The exception is number crunching, where Scheme’s speed is a liability; for those cases, I use FORTRAN or C.

Honestly, I wouldn’t put this on a public site normally. It’s not code I’m proud of. I’m only putting it on here because I’m graduating and need to clean up a few years’ worth of school-related crud from my home directory.

Every now and again I go in here and try to clean some of this stuff up. Maybe one day, it’ll all work.

acceleration

A library for performing Aitken acceleration.

This was written for me to experiment with ideas in a numerical computations class (the actual code I turned in was in FORTRAN 77). Thus, it’s not very general.

An example of use is included at the bottom of the module.

algebra

Basic algebraic formulas, such as the quadratic formula and factorials.

  • natural?

    Predicate, returns true for nonnegative integers.

  • quadratic

    Given three coefficients for (ax² + bx + c), calculate the values of x. Returns both values in a list.

  • factorial

    Given n, return n!

    This function is tail recursive, so it won’t use a new stack frame on every iteration.

  • gcd

    Given two integers, return their greatest common divisor.

    Uses Euclid’s algorithm.

  • lcm

    Given two integers, return their least common multiple.

constants

The constants directory contains various libraries containing constants from math and science. Some I’ve used, some I’ve merely copied from Wikipedia for the sake of completeness.

I hope to use most of these one day.

  • Module spauldo constants electrical-constants
    • ke

Column’s constant

  • Module spauldo constants math-constants
    • π

Archimedes’ constant (the ratio of a circle’s circumference to its diameter).

  • φ

The golden ratio.

  • i

The unit imaginary number.

Be careful with this one, since i is often used as a variable.

  • e

Euler’s number

  • Module spauldo constants physics-constants
    • c

The speed of light in a vacuum, in meters per second.

  • Na

Avagadro’s constant

  • G

The gravitational constant

  • R̅(R followed by U+0305 COMBINING OVERLINE)

Gas constant

  • kB

Boltzmann constant

  • elem-charge

Elementary charge constant (e)

  • ε0

Vacuum permittivity constant

  • μ0

Vacuum permeability constant (magnetic constant)

  • h

Planck constant

Reduced Planck constant

  • α

Fine structure constant

cycle

The cycle module contains functions for working with cycling iterated functions.

  • cycle-detect

    Given an iterated function and a starting point, find the point at which the function begins to cycle and the length of the cycle.

  • seq-run

    Given an iterated function, a starting point, and the number of values to produce, return a list of values in the sequence.

discrete

This was supposed to be a library of discrete math functions. The code inside was written before I ever took a discrete math class, and I was trying to generate the coordinates of the vertices for a truncated icosahedron (to do a 3D rendering of a soccer ball).

The two exported functions are meant to work with the vector module (or maybe the old vector module…). The one function that actually looks useful (get-all-permutations) isn’t even exported. Bah!

Needless to say, I haven’t tried to run any of this in years and should probably either throw it out or rewrite it in a proper, general fashion. Maybe you’ll find it useful for an example, but I mostly find it an embarrassment.

Oh, and it never worked. I ran short on time and used 3D software to export a soccer ball that I could import into JavaScript and render. Probably the hardest problem I worked on in that JavaScript class.

electronics

Functions useful for electronics. I am not an electronics engineer.

Strangely enough, I usually forget I have this and calculate this stuff in Emacs Lisp.

  • voltage-divider

    Takes vin (voltage connected to r1), r1, and r2 (connected to ground) and gives the voltage between r1 and r2.

  • ohm-i

    Takes voltage and resistance and returns current.

  • ohm-v

    Takes current and resistance and returns voltage.

  • ohm-r

    Takes voltage and current and returns resistance.

  • power-vi

    Takes voltage and current and returns power (in watts).

  • power-ir

    Takes current and resistance and returns power.

  • power-vr

    Takes voltage and resistance and returns power.

  • cap-charge

    Takes capacitance and voltage and returns charge (in Columns)

  • cap-energy

    Takes capacitance and voltage and returns energy (in Joules)

  • series-capacitors

    Takes any number of capacitances and returns the total capacitance of series capacitors.

  • parallel-capacitors

    Takes any number of capacitances and returns the total capacitance of parallel capacitors.

  • series-resistors

    Takes any number of resistances and returns the total resistance of series resistors.

  • parallel-resistors

    Takes any number of resistances and returns the total resistance of parallel resistors.

  • resistor-voltage-limit

    Takes the resistance and power rating of a resistor and returns the voltage required to meet that limit (i.e. how much voltage does it take to smoke this resistor).

The following functions have to do with RC circuits. Remember that I’m not an electrical engineer? I don’t remember how these work.

  • rc-time-constant
  • rc-charge-current
  • rc-discharge-current
  • rc-charge-voltage
  • rc-discharge-voltage
  • rc-charge-charge
  • rc-discharge-charge

memoization

Memoization of functions. I have tested this, and it does work.

Guile has built-in memoization functionality, but it’s not documented as far as I can tell. So instead, we can use the function make-memoized to create memoized versions of almost any function.

There are some caveats:

  1. The function shouldn’t have side effects. We could still memoize the return values just fine, but side effects would not occur after the first time the function was called.
  2. The function should always return the same value for the same arguments. This pretty much goes without saying.
  3. The arguments to the function should be comparable via eq?. Numbers and symbols are both comparable via eq?.
  4. The function should not return any pairs (i.e. anything that pair? would return #t for). That includes lists.

I may still make some modifications to remove caveat #4 (basically, instead of storing values, I could store closures that return those values). At this point though, it’s unnecessary.

Note that for simple functions, it would probably be much more efficient to just call the function. This is mostly useful for functions that take a lot of calculation.

primes

Functions related to prime numbers.

  • sieve-of-sundaram

    This is the prime number sieve created by S. P. Sundaram in 1934. It takes the maximum number to be considered as a prime and returns a vector where all prime indexes hold the value #t and all non-prime indexes hold the value #f.

    It’s not as well known as the sieve of Eratosthenes, but it’s a bit faster.

  • list-primes

    Takes a vector such as that returned by sieve-of-sundaram and returns a list of prime numbers.

sequence

Sequence memoization. It wasn’t fully working last I messed with it.

vector-math (and vector-math-old)

Some functions for vector math. I need to go through these and clean them up.

vector-utils

Utility functions for the vector data structure. No relation to mathematical vectors.

Bugs

If you find a bug, open an issue or send me a pull request. I would like the code to be as bug-free as possible.

If you want a feature, go ahead and ask. I make no promises. It helps if you send a pull request implementing the feature.

License

This code is licensed under the ISC license, which is similar to the BSD or MIT licenses. See LICENSE for details.