←back to thread

88 points azhenley | 2 comments | | HN request time: 0.398s | source
Show context
CyberDildonics ◴[] No.45153090[source]
I would rather use a loop so I can debug it.
replies(2): >>45153162 #>>45154253 #
skrishnamurthi ◴[] No.45154253[source]
This isn't meant to be a good programming mechanism, it's meant to be an illustration of how to use the macro system.

But also, if you're processing non-linear data, you're going to want to do with a recursive function anyway. E.g., when dealing with a tree. Code below; can't seem to get multi-line code-formatting so it looks hideous:

#lang racket

(require "anon-rec.rkt") (require rackunit)

(struct mt ()) (struct node (v l r))

(define sum-tree (lam/anon (t) (cond [(mt? t) 0] [(node? t) (+ (node-v t) ($MyInvocation (node-l t)) ($MyInvocation (node-r t)))])))

(define t (node 5 (node 3 (mt) (mt)) (node 7 (node 9 (mt) (mt)) (mt))))

(check-equal? (sum-tree t) 24)

replies(2): >>45154770 #>>45155013 #
1. neilv ◴[] No.45155013[source]
For formatting code blocks on HN, prefixing each line with 4+ leading spaces works:

    (define sum-tree
      (lam/anon (t)
        (cond ((mt?   t) 0)
              ((node? t) (+ (node-v t)
                            ($MyInvocation (node-l t))
                            ($MyInvocation (node-r t)))))))
replies(1): >>45158685 #
2. skrishnamurthi ◴[] No.45158685[source]
Aaah, thanks Neil!