Archivio

Posts Tagged ‘albero di Steinhaus’

Albero di Steinhaus in Scheme

17 Luglio 2010 Nessun commento
1 Star2 Stars3 Stars4 Stars5 Stars (Ancora nessun voto)
Loading...

Vedi un esempio dell’albero di Steinhaus con Scheme (lisp Language):


(define nxt
(lambda (sq op)
(let ((n (string-length sq)))
(if (= n 1)
""
(string-append
(if (op (string-ref sq 0) (string-ref sq 1))
(string-append (string #\0) (nxt (substring sq 1) op))
(string-append (string #\1) (nxt (substring sq 1) op))
)
)
))))

(define tri
(lambda (sq op)
(if (= (string-length sq) 1)
(list sq)
(cons sq (tri (nxt sq op) op))
)))

(tri "1101" char=? )

Altra soluzione potrebbe essere questa, più complessa da capire, ma più semplice da implementare!

(define nxt
(lambda (sq op)
(let ((n (string-length sq)))
(if (= n 1)
""
(string-append
(op (string-ref sq 0) (string-ref sq 1))
(nxt (substring sq 1) op))
))))

(define tri
(lambda (sq op)
(if (= (string-length sq) 1)
(list sq)
(cons sq (tri (nxt sq op) op))
)))
(tri "1101" (lambda (u v) (if (char=? u v) "0" "1")))
Categorie:Scheme Tag:
Guion Matteo
Privacy Info

Questo sito Web utilizza i cookie per consentirci di offrire la migliore esperienza utente possibile. Le informazioni sui cookie sono memorizzate nel tuo browser ed eseguono funzioni come riconoscerti quando ritorni sul nostro sito web e aiutando il nostro team a capire quali sezioni del sito web trovi più interessanti e utili.

È possibile regolare tutte le impostazioni dei cookie navigando le schede sul lato sinistro.