As I love sharing code on my blog, and I love Chicken Scheme, I'm going to share a couple of handy and practical features that I've made use of recently.

Anyone that has used C++ for longer than a few minutes has probably noticed that literal constants (numbers and such) often have a suffix attached to them. The most common place that you'll see this is when writing floating-point decimals, ie. 1.0f.

They might seem a little pointless at first, but they have their uses; I swear that I once read of a way to add your own types into the language too.

But anyway, I searched for a way to do this in Chicken too, and it turns out it's really easy (of course!). Chicken provides a function called set-parameterized-read-syntax! that can emulate this functionality quite nicely.

Here's a real world case that I used recently, I wanted to be able to convert from inches into pixels, based on a known DPI (in this case 180). The code to set this up looks like this:

; This lets you type #12i anywhere in your code and have it replaced with 2160
(set-parameterized-read-syntax #\i (lambda (port n) (* n 180)))

Yeah that's really it, isn't Scheme wonderful :P

The other really useful feature is the regex-literals egg (download it with sudo chicken-install regex-literals), which gives Chicken Scheme the ability to use Ruby/Perl/Javascript style literal regexes; well almost, it allows you to type #/myregexhere/i (notice the leading #) and it will be converted into a regexp object. Luckily we can even take this a step further and remove the hash at the front. Code time!

; Import the egg first of all
(use regex-literals)

; Lucky for us, regex-literals provides us the function we need to read
; a full regex literal from a Scheme port, so all we have to do is just
; hook it up to the "/" character and it will work as expected!
(set-read-syntax! #\/ read-regex-literal)

; Now we can write inline regex with a familiar syntax :D
(pp (string-match /.*match[A-Za-z]+me.*/i "hello match world me :D"))

I know this a measly amount of code to be posting an entry for, but I hope that both of these features (among others) demonstrate just how practical Chicken Scheme can be in many situations, and shows that you don't always have to write a large amount of code to get cool things done! Mint!