ClojureのcondをSchemeでも使いたい
30分プログラム、その621。ClojureのcondをSchemeでも使いたい。
ClojureのCondは余計な括弧を省略できるらしいです。
(define (f n) (cond (even? n) "this is even" (odd? n) "this is odd"))
括弧が本当に余計なのかどうかはよく分からないけれど、とりあえずマクロで実現しとこう。
使い方
(define (f n) (my-cond (even? n) "this is even" (odd? n) "this is odd"))
ソースコード
#! /opt/local/bin/gosh ;; -*- mode:scheme; coding:utf-8 -*- ;; ;; group.scm - ;; ;; Copyright(C) 2009 by mzp ;; Author: MIZUNO Hiroki / mzpppp at gmail dot com ;; http://howdyworld.org ;; ;; Timestamp: 2009/07/16 21:24:30 ;; ;; This program is free software; you can redistribute it and/or ;; modify it under MIT Lincence. ;; (define (split-at xs n) (if (or (= n 0) (null? xs)) (values '() xs) (receive (ys zs) (split-at (cdr xs) (- n 1)) (values (cons (car xs) ys) zs)))) (define (group n xs) (if (null? xs) '() (receive (ys zs) (split-at xs n) (cons ys (group n zs))))) (define-macro (my-cond . xs) `(cond ,@(group 2 xs))) (define (f n) (my-cond (even? n) "this is even" (odd? n) "this is odd"))