Path: csiph.com!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Patricia Ferreira Newsgroups: pt.comp.programacao Subject: Re: sobre loops em common lisp Date: Tue, 05 Mar 2024 20:05:22 -0300 Organization: A noiseless patient Spider Lines: 48 Message-ID: <87jzmgqnyl.fsf@example.com> References: <87edcxza4m.fsf@example.com> <87le74la4b.fsf@lispclub.com> <87ttlst7px.fsf@example.com> <878r2wsa0o.fsf@example.com> <87v860qomo.fsf@example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: dont-email.me; posting-host="ab31dfe5a2356a88a3b13dd70f99699e"; logging-data="18695"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OZDgMNw2JJY+YB9caLpKU03sBlfbWe/A=" Cancel-Lock: sha1:kGWbXHOymICXHAgKjTcLTjHTd2A= sha1:lv2FdInpXW9+5HwH5bPYRYH3BYM= Xref: csiph.com pt.comp.programacao:270 Patricia Ferreira writes: [...] > Eis meu segundo. Gere um nome (de arquivo) aleatório e tente usá-lo. > Se calhar de alguém já tê-lo escolhido, tente de novo (com outro nome). > Feito isso, retorne-nos esse nome sortudo e pra que a gente possa > renomeá-lo pro seu nome final---accounts.lisp. > > (defun write-accounts () > (let ((name > (loop > (let* ((tmp (random-string 10)) > (name (format nil "~a.tmp" tmp))) > (when > (ignore-errors > (with-open-file > (s name > :direction :output > :if-exists :error > :if-does-not-exist :create) > (write *accounts* :stream s))) > (return name)))))) > (rename-file name "accounts.lisp") > *accounts*)) > > Tecnicamente temos um problema com esse procedimento. Se o rename-file > não obtiver sucesso, nem vamos saber. Não sei o que fazer se o rename não ocorrer, mas ficamos a versão final: (defun write-accounts () (let ((name (loop (let* ((tmp (random-string 10)) (name (format nil "~a.tmp" tmp))) (when (ignore-errors (with-open-file (s name :direction :output :if-exists :error :if-does-not-exist :create) (write *accounts* :stream s))) (return name)))))) (if (ignore-errors (rename-file name "accounts.lisp")) *accounts* (format t "could not rename ~a to account.lisp" name))))