#lang web-server/insta
(define (start request)
'(html
(head (title "My blog"))
(body (h1 "Under construction")
(p "Imagine a rocking website here")
(p "Should be pretty froody"))))
Showing posts with label scheme. Show all posts
Showing posts with label scheme. Show all posts
Monday, June 7, 2010
Racket
Racket is the new name for DrScheme. Here is a simple webserver in Racket:
Unhygenic macros in PLT Scheme
You need language "pretty big" for this to work. The following is a demonstration of "anaphoric if"
(defmacro aif (a b c)You may need to
`(let ((it ,a))
(if it ,b ,c)))
(aif #f 2 3) ; returns 3
(require mzlib/defmacro)
Wednesday, March 17, 2010
Chicken scheme installation report (mingw-msys)
The following is an installation report of attempting to compile chicken from git using mingw-msys on 17-Mar-2010. The topmost entry in the git log is: commit 590e58d9cfaed30d1b79abcc8bab4c13b80691ce Author: felixDate: Fri Mar 12 07:51:21 2010 +0100 same for chicken-status and chicken-uninstall Here are the steps I followed to create chicken: git clone http://chicken.wiki.br/git/chicken-core.git cd chicken-core make PLATFORM=mingw-msys confclean make PLATFORM=mingw-msys PREFIX=/usr/local bootstrap make PLATFORM=mingw-msys PREFIX=/usr/local CHICKEN=./chicken-boot make PLATFORM=mingw-msys PREFIX=/usr/local install During the execution of the last command, it is reported: Error: (directory) cannot open directory: "/usr/local/lib/chicken/5" However, this doesn't really seem to be a problem, because if I type: ls /usr/local/lib/chicken/5 I obtain the listing: chicken.import.so posix.import.so srfi-14.import.so csi.import.so regex.import.so srfi-18.import.so data-structures.import.so scheme.import.so srfi-4.import.so extras.import.so setup-api.import.so srfi-69.import.so files.import.so setup-api.so tcp.import.so foreign.import.so setup-download.import.so types.db irregex.import.so setup-download.so utils.import.so lolevel.import.so srfi-1.import.so ports.import.so srfi-13.import.so The interpreter works OK. If I type: csi I am greeted with the prompt: CHICKEN (c)2008-2010 The Chicken Team (c)2000-2007 Felix L. Winkelmann Version 4.4.1 windows-mingw32-x86 [ manyargs dload ptables ] compiled 2010-03-17 on EMEAGBRABSLT001 (MINGW32_NT-5.1) Compilation is problematical, though. If I have the file hello.scm: (write-line "chicken scheme says hello") and type: csc hello.scm I obtain the response: The system cannot find the path specified. Error: shell command terminated with non-zero exit status 1: ""\usr\local\bin\chicken.exe" "hello.scm" -output-file "hello.c"" The problem is due to the use of backslashes. It should be noted that I am typing the commands from MSYS shell, where forward slashes are expected. If I run csi and csc from a normal DOS shell, then I get the same responses. In DOS, although the backslashes are correct, the filepath isn't. It's actually in: C:\msys\1.0\local\bin One philosophical problem to resolve is whether chicken development should be done from a DOS shell, or a MSYS shell. I am inclined to say the latter, as that is the normal development environment for MINGW.
Monday, March 15, 2010
block/return in scheme
Continuations in Scheme - they're not necessarily all that hard. You can use them to prematurely return from a function, as in the following example:
In the above example, it would print "One ", and bail out by returning the value #t. The remainder of the lambda is never executed. Note that 'return' is a paramater, so you could choose whatever name you wanted to.
It is useful to have some syntactic sugar for the above. A standard name is 'let-cc', defined as follows:
I can then type things more conveniently:
Note that my choice of use of the word 'return' is still arbitrary. It's very flexible, because I can have any number of 'named' blocked, and return to whatever part of the program I am interested in.
Suppose I want to give up some flexibility, and that I want 'return' to be part of my syntax. I want a block/return (similar to Lisp's block nil) construction. 'return' means that I want to return from the block. To put it in more concrete terms, I want to be able to write:
A macro which satisfies this is:
The above was written for Chicken Scheme. Other Schemes might not support it - it's certainly do-able in other schemes - but the implementation might be rather hairier (check out the cls link below, for example).
Further reading:
(call-with-current-continuation (lambda (return) (begin (display "One ") (return #t) (display "Two ") #f)))
In the above example, it would print "One ", and bail out by returning the value #t. The remainder of the lambda is never executed. Note that 'return' is a paramater, so you could choose whatever name you wanted to.
It is useful to have some syntactic sugar for the above. A standard name is 'let-cc', defined as follows:
(define-syntax let-cc (syntax-rules () ((let-cc variable . body) (call-with-current-continuation (lambda (variable) . body)))))
I can then type things more conveniently:
(let-cc return (display "One ") (return #t) (display "Two ") #f)
Note that my choice of use of the word 'return' is still arbitrary. It's very flexible, because I can have any number of 'named' blocked, and return to whatever part of the program I am interested in.
Suppose I want to give up some flexibility, and that I want 'return' to be part of my syntax. I want a block/return (similar to Lisp's block nil) construction. 'return' means that I want to return from the block. To put it in more concrete terms, I want to be able to write:
(block (write-line "this is printed") (return "this is returned") (write-line "this is never printed"))
A macro which satisfies this is:
(define-syntax block (lambda (x r c) (let ((body (cdr x))) `(call-with-current-continuation (lambda (return) ,@body)))))
The above was written for Chicken Scheme. Other Schemes might not support it - it's certainly do-able in other schemes - but the implementation might be rather hairier (check out the cls link below, for example).
Further reading:
- comp.lang.scheme - discussion on writing block/return macros, started by me
- Wikipedia - discusses continuations, and how you might write a Pythonesque 'yield' statement
- Hygenic macros - a discussion
- Chicken modules and macros - Reference documentation for Chicken v4.x
Subscribe to:
Posts (Atom)