Deutsch English Français Italiano |
<vapdj5$3tkts$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" <Nobody447095@here-nor-there.org> Newsgroups: comp.lang.lisp,comp.lang.scheme Subject: Re: Lisp newbie needs help Date: Thu, 29 Aug 2024 09:04:42 -0000 (UTC) Organization: A noiseless patient Spider Lines: 42 Message-ID: <vapdj5$3tkts$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Thu, 29 Aug 2024 11:04:42 +0200 (CEST) Injection-Info: dont-email.me; posting-host="04737eacfc9f816e64fd3f986b66e8f3"; logging-data="4117436"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19d4GXzcTs6IzXKuRV8XjS3" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:JCcEsQgTK3OzmAF6poB7glOLcVg= Bytes: 1878 > (defun my-test () > (loop for number = (1+ (random 6)) > as sum = number then (+ sum number) > until (= number 1) > do (format t "~&~D thrown. Sum: ~D" number sum) > finally (format t "~&One thrown."))) Gauche Scheme (use srfi-1) ;; unfold (use srfi-27) ;; random-integer (define (my-test) (fold (^(n sum) (print n " thrown. Sum: " (inc! sum n)) sum) 0 (cdr (unfold zero? ($ + 1 $) (^_ (random-integer 6)) -1))) (print "One thrown.")) gosh> (my-test) 2 thrown. Sum: 2 2 thrown. Sum: 4 6 thrown. Sum: 10 One thrown. gosh> (my-test) One thrown. Explanation of "unfold": Function: unfold end-test key gen-next-seed seed :optional tail-gen (unfold zero? (lambda(n) (+ 800 n)) (lambda(n) (- n 1)) 7) ===> (807 806 805 804 803 802 801) (unfold zero? (lambda(n) (+ 800 n)) (lambda(n) (- n 1)) 7 (lambda(n) (list "The number" n "ended the unfolding."))) ===> (807 806 805 804 803 802 801 "The number" 0 "ended the unfolding.")