Posts tagged racket

Interfacing D functions from Racket

:: D, racket

This is how you can use a function written in D from a Racket script.

In a file called logc.d:

import std.stdio;

extern(C)
void hello()
{
  writeln("hi from D");
}

Compile to a shared library with:

$ ldc2 -shared -m64 logc.d

or with

$ dmd -shared -m64 -fPIC -defaultlib=libphobos2.so logc.d

Use the generated liblogc.so (or liblogc.dylib) from a Racket script:

(require ffi/unsafe)

(define logc-lib (ffi-lib "liblogc"))

(define logc-hello
  (get-ffi-obj "hello" logc-lib
               (_fun -> _void)))

Try the results:

> (logc-hello)
hi from D

This post is based on Call D from Ruby using FFI and tutorial using racket’s ffi

Minimal Racket web app

:: racket, web

This is the simplest module that responds to HTTP requests.

#lang racket/base

(require
 (only-in web-server/http
          response/full)
 (only-in web-server/http/request-structs
          make-header)
 (only-in web-server/servlet-env
          serve/servlet))

;; hello: request? -> response?
(define (hello req)
  ;; struct response/full
  (response/full
   200                          ;; code
   (string->bytes/utf-8 "OK")   ;; message
   (current-seconds)            ;; timestamp in s
   #f                           ;; mime or #f
   (list (make-header           ;; list of headers
          (string->bytes/utf-8 "Server")
          (string->bytes/utf-8 "Racket")))
   (list                        ;; body: list of bytes
     (string->bytes/utf-8 "Hello world!\n"))))

(module+ main
  (serve/servlet
   hello
   #:port 8080
   #:servlet-regexp #rx""))

Save it as hello.rkt and run it with $ racket hello.rkt

Check that it works:

$ curl -v http://localhost:8080/
*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 19 Dec 2017 18:13:14 GMT
< Last-Modified: Tue, 19 Dec 2017 18:13:14 GMT
< Content-Length: 13
< Server: Racket
<
Hello world!
* Connection #0 to host localhost left intact

Setting up Github Pages with Frog

:: racket, frog, blog

To set up a static blog in Github Pages using the Racket package Frog, you need to do the following:

  • download and install Racket (duh!)
  • install Frog with raco:
$ raco install frog
  • create a folder and initialize a Frog project there:
$ mkdir new-blog
$ cd new-blog
$ raco frog --init
  • customize the .frogrc file. Set at least scheme/host to https://your-username.github.io, and title & author to whatever you want.

  • add some content of your own:

$ raco frog -n "New post"
# ... output says something like:
~/new-blog/_src/posts/2016-10-03-new-post.md
# ... edit that file with your preferred editor ...
when you are finished editing, preview your glorious site with

$ raco frog -bp
  • initialize an empty Git repository on the project folder, and tell git to ignore the Frog machinery and your source directory
$ git init .
$ cat << EOF > .gitignore
/.frog/
/.frogrc
/_src/
EOF
  • commit everything, except for what we just ignored
$ git add .
$ git commit
  • on github.com, create a new repository with the name your-username.github.io. Once created, add it as a remote to your local repo, and push your local master to it.
$ git remote add origin <your new repo>
$ git push -u origin master
  • after some seconds, your new blog should be available as http://your-username.github.io

Now, each time that you want to add a new post, it’s just a matter of

$ raco frog -n "Another post"
# ... edit that post ...
$ raco frog -b
$ git add .
$ git commit -a
$ git push

Racket and vim-syntastic

:: racket, vim

The current Syntastic checker for Racket syntax in Vim is the racket executable itself. This means that the current buffer is executed by Racket whenever it is checked for syntax, and this can have unintended consequences, e.g. entering an infinite loop, if you happen to open the wrong file.

In order to just get the syntax errors, add this to your .vimrc:

let g:syntastic_racket_racket_args="--load"