Posts tagged Programmierung

Die Programmiersprache F#

:: IT, Programmierung

Im Rahmen einer Studienleistung für eine Fort- und Weiterbildung habe ich an der FernUniversität in Hagen eine Ausarbeitung zu der Programmiersprache F# angefertigt. Vielleicht hilft die Darstellung der einen oder anderen, sich einen kurzen Überblick zu verschaffen. Zu finden ist sie hier.

Using Racket Minimal and raco

:: IT, Racket, Programmierung

I use Racket Minimal on my smart phone (this describes how to compile the run time for an ARM based system). It’s is a very small installation of Racket (about 36 MB after installation). After installation one only needs to install the packages that are really neded. But this can be a bit tricky because a lot of packages want to install their documentation and other stuff and bring a whole bunch of files on your drive as dependencies.

Some of the packages are divided up into a "-lib", "-doc" (and sometimes "-test") as laid out in the documentation. With these packages it’s easier to only install the implementation.

A small script of mine used only basic modules and relied on rackunit for the tests. On a mobile device the start up time of such a program can be critical. Therefore it is wise to only require the needed packages and to have the source code being compiled to byte code. One could do this with raco setup (which is included in Minimal Racket) but I wanted to have raco make (which is not part of Minimal Racket) available.

The commands of raco are added via a raco-commands variable in packages’ info.rkt file. I looked through the packages of my “full install” and found the package compiler-lib which adds some commands (make, exe, pack, unpack, decompile, test, expand, read, distribute, demodularize) to raco and relies on only a few other packages. As a result the source and binary files need about 3.8 MB on my phone which is okay for me.

To sum up: After a simple raco pkg install compiler-lib I could easily use raco make and raco test to play with my program on my phone.

I played with CHICKEN Scheme, Docker and Alpine Linux

:: IT, Programmierung

I am looking forward to meet LISP people at the 32c3’s LISP assembly. The last days I played a bit with different Scheme implementations including CHICKEN scheme. The main feature of CHICKEN is that it compiles the Scheme code to C and then creates dynamic libraries and binaries with the C compiler. I thought that combining these binaries with a minimal Docker container could give me a very small deployment. So here are my steps:

How to use GET Bucket location on Amazon S3 with Racket

:: IT, Racket, Programmierung

In Racket I want to iterate over my buckets in Amazon S3. They are located in different regions. So how do I get my bucket’s location/region? In the API Reference there is a call GET Bucket location. I use Greg’s AWS library for Racket and this library authenticates its calls with signature version V4. But V4 requires the user to know the region to correctly sign the request. So I need to know the region to ask Amazon S3 for the region where the bucket is located. Otherwise Amazon S3 responds with an error:

<?xml version="1.0" encoding="UTF-8"?>
 <Message>The authorization header is malformed; the region 'us-east-1'
is wrong; expecting 'eu-central-1'</Message>

After some search on the net I found a post on Stackoverflow that helped to solve that issue: If I use the URL format (instead of the normally used virtual host format) I could get the location of any bucket. Every region responds with a LocationConstraint answer.

Therefore a code snippet for Racket could be:

(define (get-bucket-location bucket)
      ([s3-path-requests? #t])
    (define xpr (get/proc (string-append bucket "/?location") read-entity/xexpr))
    (and (list? xpr)
         (= (length xpr) 3)
         (third xpr))))

For example:

> (get-bucket-location "my-bucket-somewhere")

PS: I think official Amazon S3 documentation could be a bit more verbose on the issues with GetBucketLocation and signature V4.

Update: Greg added a bucket-location function to his great library

Running Racket on AWS Lambda

:: IT, Racket, Programmierung

I started to use AWS for some projects recently. But I only use few of their services. From time to time I look into some of there services and wonder if they are useful for my tasks. I looked into AWS Lambda, "… a compute service that runs your code in response to events and automatically manages the compute resources for you, making it easy to build applications that respond quickly to new information." Nowadays these “lambda functions” could be written in NodeJS or Java. When I was looking for a roadmap of the supported languages I found an interesting blog post by Ruben Fonseca. He explaind how to run Go code on AWS Lambda.

I tried the same with Racket and wrote a short Racket programm test.rkt:

#lang racket/base

(display (format "Hello from Racket, args: ~a~%" (current-command-line-arguments)))

Then I used raco to create a binary test:

raco exe --orig-exe test.rkt

I took the NodeJS wrapper from Ruben’s blog post and put it in a file main.js:

var child_process = require('child_process');

exports.handler = function(event, context) {
  var proc = child_process.spawn('./test', [ JSON.stringify(event) ], { stdio: 'inherit' });

  proc.on('close', function(code) {
    if(code !== 0) {
      return context.done(new Error("Process exited with non-zero status code"));


Then I put both files in a zip archive, created a new AWS Lambda function, uploaded the zip file and invoked the function:

Invocation of AWS Lambda function

Invocation of AWS Lambda function


PS: Only question is: When is AWS Lambda coming to the region eu-central-1, located in Frankfurt?

Upate (2016–03–15): AWS Lambda is now available in the EU (Frankfurt) region!

DateTime conversion can be tricky

:: IT, Programmierung

I wrote a small Lisp application and a JavaScript client gets some data from that application. All time stamps are returned as “Lisp” time stamps, i.e. an integer with seconds where zero equals Jan 01 1900.

In the JS client the time stamp is then converted to JS time stamps, i.e. millisconds where zero equals Jan 01 1970.

When testing the application I noticed that sometimes the displayed date is one day behind. For example in the data base I have Jan 05 1980 but in JavaScript I get a Jan 04 1980. But some other dates worked: A time stamp Jan 05 1970 was correctly converted to Jan 05 1970.

I had a look into the JavaScript code and found:

convA = function(ts) {
  tmp = new Date(ts*1000);
  tmp.setFullYear(tmp.getFullYear() - 70);
  return tmp.getTime();

It’s likely the developer thought: “Well, it’s millisecond instead of second. Therefore I multiply by 1,000. But then I am 70 years in the future and I have to substract 70 years and everything will be ok.”

After thinking a while I came to the conclusion: Of course not!

The developer made the assumption that there are as many leap years between 1900 and 1970 as between ts and ts+70. Obviously that assumption does not hold for all time stamps. And therefore sometimes the resulting JavaScript date is one day behind.

So a better solution would be to substract all seconds between 1900 and 1970 from ts, multiply by 1,000 and treat this as a JavaScript time stamp. Perhaps best would be to do the conversion in the Lisp process and only deliver a JavaScript-like time stamp.

I learned something about symbols and packages

:: IT, Programmierung

I am using Common Lisp for developing a web application. Several days ago a new part of this application didn’t worked as supposed and I spent a considerable large amount of time in finding the bug. It was a very simple problem with symbols where I mixed something up.

In the application the web server somewhen gets some JSON data from the browser. It is then converted to Lisp object using the CL-JSON package. This package converts JSON objects to a-lists and converts the member keys to symbols (see CL-JSON’s documentation. I then wanted to look something up in that a-list and failed.

I wrote a small test case to show the effect and explain what went wrong.

(ql:quickload '("hunchentoot" "cl-who"))
;; direct loading via ql only for demonstration purposes, normally I
;; would use a asdf:defsystem for that.

(in-package :cl-user)

(defpackage :my-app (:use :cl))

(in-package :my-app)

(defparameter *my-a-list* 
  '((foo . 100)
    (bar . 200)))   ;; in the real application this a-list is
		    ;; generated by a JSON-to-lisp conversion by
		    ;; CL-JSON; in CL-JSON the object member keys are
		    ;; converted to symbols.

(defun get-value (key)
  "Returns the value with KEY from *MY-A-LIST*."
  (cdr (assoc (intern (string-upcase key)) *my-a-list*)))

(hunchentoot:define-easy-handler (web-get-value :uri "/get-value") (id)
  (cl-who:with-html-output-to-string (*standard-output* nil :prologue t)
    (:p (cl-who:fmt "Value of ~a is: ~a" id (get-value id)))))

(defun start ()
  (hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port 4242)))

So on the REPL everything looks fine: MY-APP> (get-value "foo") 100 MY-APP> (get-value "bar") 200 MY-APP>

But when I used my web browser to give me these results as well I got something strange. For example here are some results when using curl: ~> curl http://localhost:4242/get-value?id=foo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ""> <p>Value of foo is: NIL</p>

I was puzzled: The value is NIL?

After some debugging I found out that the easy handler from Hunchentoot runs with *package* set to COMMON-LISP-USER (and not to MY-APP as I implicitly assumed). That means that assoc looked up COMMON-LISP-USER::FOO in the a-list where the keys are MY-APP::FOO and MY-APP::BAR. And this test fails. Therefore NIL is returned which is correct.

So I rewrote the get-value function to: (defun get-value (key) "Returns the value with KEY from *MY-A-LIST*." (cdr (assoc (intern (string-upcase key) (find-package :my-app)) *my-a-list*))) Now the symbols are interned in the same package and everything went well: ~> curl http://localhost:4242/get-value?id=foo <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ""> <p>Value of foo ist: 100</p> ~> curl http://localhost:4242/get-value?id=bar <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ""> <p>Value of bar ist: 200</p>

Therefore I was reminded to think about packages when interning symbols. A good guide to symbols and packages could be found in this document: The Complete Idiot’s Guide to Common Lisp Packages.