Malice <
gwma...@gmail.com> writes:
> Thanks for reply, which is insightful, but at the same time you came
> up with excellent answer, to all questions that I didn't ask - and no
> answer to question that I asked! That's something unusual :)
>
> You're right about alist thing. I mapped alist in my memory to a
> picture of list of lists - but it isn't equal, as you showed.
>
> Also, I'm really not concerning performance yet, because, following
> Knuth - "Premature optimization is the root of all evil"; I shouldn't
> really optimize my programs(apart from making obviously bad bad
> functions), if I don't know the language itself.
>
> This said, your function would be valid if I asked for it later. You
> can see that I said:
>
>> It also tells me about possibility of using REDUCE function, but it
>> hasn't been introduced yet, so I'm not using it.
>
> It may be unclear, but what I meant was that I *don't want* to use it
> - because right now, I want to focus on learning how to do it with
> remove-if and assoc.
I understand, but remove-if and assoc are NOT the functions to use to
implement this function.
Oh, right you can use them because as soon as you have one conditional
loop, you have turing equivalence. But that doesn't mean they're the
right tool to use.
For example, once upon a time, I had a system programming teacher who
bet with us we couldn't write a program in less of 30 lines to sum an
array of 1000 elements after having introduced us a few ASM-360
instructions, but not indexed addressing. We obviously found a
solution, by using the vector as a circular buffer, and summing the
first element.
--------------------------------------------------------------------------------
;; PROGRAMME : ADDITION DE MILLE NOMBRES
;; PROGRAMMEUR : BOURGUIGNON
;; DATE: 28 MARS 1983
;; D'APRES L'IDEE DE DELANNOY
NBR DS 1000PL2
TEMP DS PL2 TEMP EST PLACE OBLIGATOIREMENT JUSTE DERRIERE NBR.
SOM DC PL5'0' ON FAIT APPEL A UNE FILE FIFO DONT
TOT DC PL3'0' LES ELEMENTS SE DEPLACERONT.
APACHE AP SOM,NBR ON ADDITIONNE LE 1ER NOMBRE DE LA FILE
MVC TEMP,NBR ET ON LE PLACE A L'ENTREE DE LA FILE.
MVC NBR(256),NBR+L'NBR ON DECALE TOUS LES ELEMENTS
MVC NBR+256(256),NBR+256+L'NBR DE LA FILE PAR BLOCS DE 256 OCTETS,
MVC NBR+512(256),NBR+512+L'NBR SOIT 128 ELEMENTS VERS LA SORTIE.
MVC NBR+768(256),NBR+768+L'NBR
MVC NBR+1024(256),NBR+1024+L'NBR L'EX-PREMIER ELEMENT ETANT A
MVC NBR+1280(256),NBR+1280+L'NBR L'ENTREE, IL EST REINTEGRE A
MVC NBR+1536(256),NBR+1536+L'NBR LA FILE ET REPRENDRA SA PLACE
MVC NBR+1792(208),NBR+1792+L'NBR INITIALE AU BOUT DE 1000 ITERATIONS.
AP TOT,=P'1'
CLC TOT,=P'1000'
BC 7,APACHE
LA SORTIE DE LA FILE EST LA ZONE ADRESSEE
PAR NBR, L'ENTREE CELLE ADRESSEE PAR TEMP.
--------------------------------------------------------------------------------
(this earned us a bottle of Champagne).
In lisp that would be:
(defun sum-thousand (v)
(check-type v (vector * 1000))
(let ((temp)
(sum 0))
(loop repeat 1000
do (setf temp (aref v 0))
(incf sum temp)
(replace v v :start1 0 :start2 1)
(setf (aref v 999) temp))
sum))
(sum-thousand (coerce (iota 1000) 'vector))
--> 499500
(* 500 999)
--> 499500
But of course, it's not because it's possible to write it like that (and
you can also prove that it's correct, happy Pr. Knuth), that you should
write it like that. You can do it for the intellectual gymanistic, the
puzzle solving, but you should at the same time also go beyond the
exercise, and go find the right tool for the job, and implement it
correctly.
Which in lisp would be something like:
(defun sum-thousand (v) (reduce (function +) v))
and in assembler would involve indexed addressing.
> And I asked the same question on #lisp @ freenode. User jasom(if
> you're reading this - thanks again!) guided me to the proper
> answer. This is what I came up with:
>
> (defun high-card (hand)
> (assoc
> (find-if #'(lambda (e) (assoc e hand)) (reverse *all-ranks*))
> hand))
> If anyone will face the same problem, I hope this answer will help
> him.
>
> And thanks again, Pascal - while your post didn't answer my question,
> it provided useful insight on problem.