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 -<br />

Jerônimo C. Pellegrini<br />

A operação binária de soma agora deverá verificar se há algum elemento <strong>com</strong>plexo.<br />

Se houver, a soma <strong>com</strong>plexa será usada.<br />

( define binary +<br />

( lambda (a b)<br />

(if (or ( <strong>com</strong>plex a)<br />

( <strong>com</strong>plex b))<br />

( <strong>com</strong>plex + a b)<br />

( standard + a b ))))<br />

A operação de soma <strong>com</strong> número arbitrário de argumentos é apenas um list-reduce<br />

usando a operação binária:<br />

( define n-ary +<br />

( lambda args<br />

( list-reduce binary + 0 args )))<br />

(n-ary+ 2 3 (make-rect 1 5))<br />

(6 5)<br />

Se quisermos, poderemos modificar o procedimento padrão + para que funcione <strong>com</strong><br />

<strong>com</strong>plexos:<br />

(define + n-ary+)<br />

(+ 7 (make-rect 0 2))<br />

(7 2)<br />

Fazemos agora o mesmo para a multiplicação:<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 />

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

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

(+ (* b c) (* a d )))))))<br />

Versão Preliminar<br />

[ 27 de outubro de 2010 at 15:47 ]<br />

51

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

Saved successfully!

Ooh no, something went wrong!