[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Type casting in Lisp

William James

11/9/2015 2:07:00 AM

Bill Atkins wrote:

> (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

MatzLisp (Ruby):

def find_fib
a = b = 1 ; i = 0
until yield(a)
a,b = b,a+b
i += 1
end
[a,i]
end

find_fib &:even?
==>[2, 2]

find_fib{|x| x.to_s.size >= 1000}
==>[10700662663827589367649805844573968850836838966321516650
1323520337531452060469404062188914758248979265780469488817759195
7484336466672569959512996030461262748092482186144069433051234774
4427502737817530875793916661921492591867595539664228371489431130
7469950343954700198543260972306729019287052644724372611771582182
5548491120525013201478612965931381792235559657452039506137551467
8375432291196021299340482607061753977068470682028954869026661854
3512452190036948064135744747091170761976694569107009802439343961
7474103736912503231365532164773697023167755051595173518460579954
9194109677783732296657965816465139034881542563101842241902598460
8800011018625555024549393711365165703944762958471454852342595042
8582425306083544435428212611008992863795048006894330309773217834
8645431132057656598684562886168087186938352973506439862976406600
0072356291790520705116407761481249188583094594056668833910935094
4456576357666151619317753792891661581327159616877487983821820492
520348473874384736771934512787029218636250627816, 4781]

--
The Authoritarian Personality extends beyond the attempt to pathologize
cohesive gentile groups to pathologizing adaptive gentile behavior in general.
The principal intellectual difficulty is that behavior that is critical to
Judaism as a successful group evolutionary strategy is conceptualized as
pathological in gentiles. --- Dr. Kevin MacDonald; "The Frankfurt School of
Social Research and the Pathologization of Gentile Group Allegiances"
1 Answer

Kaz Kylheku

11/9/2015 8:29:00 PM

0

On 2015-11-09, WJ <w_a_x_man@yahoo.com> wrote:
> MatzLisp (Ruby):
>
> def find_fib
> a = b = 1 ; i = 0
> until yield(a)
> a,b = b,a+b
> i += 1
> end
> [a,i]
> end
>
> find_fib &:even?
> ==>[2, 2]

A complete cluster fuck, demonstrating perfectly how Ruby is a worse-is-better
computer science flunk job.

When a coroutine or continuation yields, it means that its execution is
suspended, producing a value. Another context elsewhere can somehow resume it
(perhaps by calling a resume function) passing it a value which emerges
out of the yield.

What we have here is simply that a hidden argument is added to find_fib,
expected to be a function. yield just invokes that callback.

Some Ruby-pushing imbecile saw a generator or continuation example on someone's
programming blog and decided, "Bah, I can do that; it's just some kind of
implicit callback thingy". When he was later told, "No you aren't actually
doing that", he replied that the callback thingy satisfies 87% of all the use
cases, and it looks all generator-like with the yield word and hidden argument,
and so is good enough.