Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Type casting in Lisp

21 views
Skip to first unread message

Robert L.

unread,
Dec 9, 2017, 12:37:01 AM12/9/17
to
> (defun first-fibonacci-satisfying (test-fn)
> "Loop through the Fibonacci numbers, returning the first element
> that satisfies TEST-FN and the zero-based index of that element in the
> sequence."
> (loop for a = 1 then b
> and b = 1 then (+ a b)
> for index from 0
> when (funcall test-fn a)
> do (return (values a index))))
>
> ;; or, if you don't like LOOP
>
> (defun first-fibonacci-satisfying (test-fn)
> (do ((a 1 b)
> (b 1 (+ a b))
> (index 0 (1+ index)))
> ((funcall test-fn a) (values a index))))
>
> (first-fibonacci-satisfying (lambda (x) (> x 5))) ;; => 8, 5
> (first-fibonacci-satisfying #'evenp) ;; => 2, 2
>
> (first-fibonacci-satisfying
> (lambda (num)
> (>= (length (princ-to-string num)) 1000)))
> ;; =>
> 1070066266382758936764980584457396885083683896632151665013235203375314520
> 6046940406218891475824897926578046948881775919574843364666725699595129960
> 3046126274809248218614406943305123477444275027378175308757939166619214925
> 9186759553966422837148943113074699503439547001985432609723067290192870526
> 4472437261177158218255484911205250132014786129659313817922355596574520395
> 0613755146783754322911960212993404826070617539770684706820289548690266618
> 5435124521900369480641357447470911707619766945691070098024393439617474103
> 7369125032313655321647736970231677550515951735184605799549194109677783732
> 2966579658164651390348815425631018422419025984608800011018625555024549393
> 7113651657039447629584714548523425950428582425306083544435428212611008992
> 8637950480068943303097732178348645431132057656598684562886168087186938352
> 9735064398629764066000072356291790520705116407761481249188583094594056668
> 8339109350944456576357666151619317753792891661581327159616877487983821820
> 492520348473874384736771934512787029218636250627816, 4781

(define (find-fitting-fib test)
(let loop ((a 0) (b 1) (i 0))
(if (test b)
(values b i)
(loop b (+ a b) (+ i 1)))))

(find-fitting-fib (lambda (n) (> (string-length (number->string n)) 62)))

137347080577163115432025771710279131845700275212767467264610201
298

--
[T]hey always try to undermine our sense of identity ... while at the same time
they have ... stories, even in the New York Times, how Jews are ... like fourth
or fifth cousins.... It has just come out ... DNA testing companies ... admit
adding fake African ancestry to White profiles in order to screw with
"racists".... When you look at the people who own these companies, they're not
on our side---they're here to destroy us.
https://archive.org/details/Duke.20171208
http://archive.org/details/nolies

Robert L.

unread,
May 29, 2018, 2:05:40 AM5/29/18
to
(define (find-fitting-fib test)
(do.till (n (g.fib) i 0) (test n) => (values n i)))

(find-fitting-fib (lambda (n) (> (string-length (number->string n)) 62)))
===>
137347080577163115432025771710279131845700275212767467264610201
298

--
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to.
http://archive.org/details/nolies
0 new messages