08.02.2015 Views

Programação Funcional e Concorrente com Scheme

Programação Funcional e Concorrente com Scheme

Programação Funcional e Concorrente com Scheme

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

notas de aula - Jerônimo C. Pellegrini<br />

Usaremos dois procedimentos para extrair as partes real e imaginária do número:<br />

( define real-part<br />

( lambda (c)<br />

( car c )))<br />

( define imag-part<br />

( lambda (c)<br />

( cadr c )))<br />

Estes procedimentos são o mínimo que precisamos para criar o tipo de dados abstrato<br />

“número <strong>com</strong>plexo”.<br />

Agora precisamos redefinir as operações aritméticas. Primeiro guardamos os procedimentos<br />

para soma e multiplicação:<br />

( define standard + +)<br />

( define standard * *)<br />

Criamos um procedimento que transforma números reais em <strong>com</strong>plexos:<br />

( define real- > <strong>com</strong>plex<br />

( lambda (x)<br />

(if ( <strong>com</strong>plex x)<br />

x<br />

( make-rect x 0))))<br />

E finalmente definimos um procedimento que soma dois <strong>com</strong>plexos. Usamos um let<br />

para extrair as partes reais e imaginária de ambos os números e damos nomes a elas de<br />

forma que x = (a + bi) e y = (c + di).<br />

( define <strong>com</strong>plex +<br />

( lambda (x y)<br />

( let (( <strong>com</strong>plex-x ( real- > <strong>com</strong>plex x))<br />

( <strong>com</strong>plex-y ( real- > <strong>com</strong>plex y )))<br />

( let ((a ( real-p <strong>com</strong>plex-x ))<br />

(b ( imag-p <strong>com</strong>plex-x ))<br />

(c ( real-p <strong>com</strong>plex-y ))<br />

Versão Preliminar<br />

(d ( imag-p <strong>com</strong>plex-y )))<br />

( make-rect (+ a c) (+ b d ))))))<br />

50<br />

[ 27 de outubro de 2010 at 15:47 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!