Message from discussion
lisp tree to clim tree
The group you are posting to is a
Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 |
Newsgroups: comp.lang.lisp
From: "WJ" <w_a_x_...@yahoo.com>
Date: 10 Nov 2012 15:06:11 GMT
Local: Sat, Nov 10 2012 10:06 am
Subject: Re: lisp tree to clim tree
hans wrote:
> How can I transform a "lisp tree" like
> (("Class: Osteichthyes")
> ("Class: Chondrichthyes"
> (("Order: Squaliformes")
> ("Order: Rajiformes")))
> ("Class: Mammalia"
> (("Order: Rodentia"
> (("Family: Sciuridae")
> ("Family: Muridae")
> ("Family: Cricetidae")))
> ("Order: Carnivora"
> (("Family: Felidae")
> ("Family: Canidae")
> ("Family: Ursidae"))))))
> into a "clim (or mcclim) tree" like
> (("Class: Osteichthyes")
> ("Class: Chondrichthyes"
> :items (("Order: Squaliformes")
> ("Order: Rajiformes")))
> ("Class: Mammalia"
> :items (("Order: Rodentia"
> :items (("Family: Sciuridae")
> ("Family: Muridae")
> ("Family: Cricetidae")))
> ("Order: Carnivora"
> :items (("Family: Felidae")
> ("Family: Canidae")
> ("Family: Ursidae"))))))
> thank you
Using pattern-matching in Racket:
(define tree
'(("Class: Osteichthyes")
("Class: Chondrichthyes"
(("Order: Squaliformes")
("Order: Rajiformes")))
("Class: Mammalia"
(("Order: Rodentia"
(("Family: Sciuridae")
("Family: Muridae")
("Family: Cricetidae")))
("Order: Carnivora"
(("Family: Felidae")
("Family: Canidae")
("Family: Ursidae")))))))
(define (climate tree)
(map (match-lambda
[(list (? string? x)) (list x)]
[(list (? string? x) more ...)
(list* x ':items (climate more))]
[x (climate x)])
tree))
(climate tree)
'(("Class: Osteichthyes")
("Class: Chondrichthyes"
:items
(("Order: Squaliformes") ("Order: Rajiformes")))
("Class: Mammalia"
:items
(("Order: Rodentia"
:items
(("Family: Sciuridae") ("Family: Muridae") ("Family: Cricetidae")))
("Order: Carnivora"
:items
(("Family: Felidae") ("Family: Canidae") ("Family: Ursidae"))))))
You must Sign in before you can post messages.
You do not have the permission required to post.
|