chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] xc: chicken-based “bc” replacement


From: Alejandro Forero Cuervo
Subject: [Chicken-users] xc: chicken-based “bc” replacement
Date: Fri, 21 Nov 2008 18:12:26 +0100
User-agent: Mutt/1.5.13 (2006-08-11)

I grew frustrated with being unable to type numbers with suffixes such
as “1.4Pi” in bc.  As part of my work I was very often evaluating in
bc expressions such as

  1.8*1024^5/(6.9*1000^2)

when I would much rather have been typing

  1.8Pi/6.9M .

I was also annoyed at seeing “293713019.17633669565217391304” as the
result, instead of a far easier for me to parse “280.11Mi” (of course,
the prefered output depends of what I'm computing; sometimes I *will*
want to see the full result).

I decided to write a simple bc replacement using Chicken.  I had the
following goals:

1. Have a syntax for expressions that requires very little typing.  In
other words, make it fast to evaluate a lot of expressions.

1.1. Allow me to type inputs in multiple formats.  For instance,
understand “5d3h20s” or “23:12:03” (time) and convert that to 442820.
Similarly for things like “3Gi” (3*2^50) or “3G” (3*10^9).

2. Have a syntax for expressions that most people would understand.
For example, I use infix binary operators and express procedure
application as “proc(arg0, ..., argn)”.  This will allow me to show
the expressions I use to evaluate certain things to people that I work
with.  Basically, have a syntax as “universal” as possible.

3. Allow me to specify the format in which I want the output.

4. Allow me to call functions defined in Scheme.  Also allow me to
set variables and reuse their values.

I started doing this two days ago and I'm already quite happy with the
results.  I haven't implemented everything, but I'm already using it
quite often.

The following is an example session.  For readability, I've prepended
a “=> ” to each result and appended an extra newline.

  29
  => 29.00

  8.3Pi/12M
  => 742.67Mi

  output_num=output_base_1000
  => #<procedure (? num)>

  8.3Pi/12M
  => 778.75M

  output_num=output_float
  => #<procedure (output-int num)>

  8.3Pi/12M
  => 778747435.57

  output_num=output_base_1024
  => #<procedure (? num)>

  days=1+(2*3^4/5)^6/(77%8)
  => 220.65Mi

  result=2*days/1Mi
  => 441.30

  result-result
  => 0.0

  mean(10, 20, 30, result/10)
  => 26.03

“mean” is defined in a file loaded (~/.xcrc) at startup as:

  (define (mean . args)
    (/ (apply + args) (length args)))

Of course, code from eggs can be made available.

In case someone finds this useful, I've decided to make it available
at:

  http://address@hidden/azul-home/src/xc/trunk/xc.scm

Be warned that this is only the result of two days of hacking.  It
comes with no warranties of any kind.  I don't even trust it that much
yet.

Cheers!

Alejo.
http://azul.freaks-unidos.net/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]