[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Not for the squeamish (corrected

William James

2/20/2016 8:30:00 AM

Thomas A. Russ wrote:

> > I'm trying to work through the 1st Graham Book and I've come across an
> > exercise where he asks you to write a simple routine to walk a list of
> > numbers pair-wise and test that every pair matches (n, n + 1). The kicker is
> > that you are supposed to do this recursively, iteratively and using mapc and
> > return. I was pretty happy with my recursive solution and I thought the
> > iterative version was OK but the version with mapc and return is a mess.
> > It's not that it doesn't run and produce an answer, it's just that it's butt
> > ugly and I can't seem to fix that. I've got that feeling that there is
> > something obvious that will come to me in a second, but I've f***** around
> > with it for an hour and no joy. I keep producing variations on the same ugly
> > theme.
>
> Well, one thing to exploit is that the mapping functions can take
> lists of different length, and stop when they exhaust the smallest list.
> I also get away without the (block nil ...) by using return-from, but
> that may be cheating :)
>
> That would let you write:
>
> (defun diff-m (lst)
> (mapc #'(lambda (x y)
> (unless (= (- y x) 1)
> (return-from diff-m nil)))
> lst (cdr lst))
> t)

MatzLisp (Ruby):

def diff array
array.each_cons(2).all?{|a,b| a+1 == b}
end

diff [0,1,2,3,4]
==>true
diff [0,1,3,4]
==>false
diff [0,0,2]
==>false
diff [0,2,3]
==>false
diff []
==>true

--
[T]he number of Jews killed there was 800,000,000.... [T]he Talmud makes it all
clear by informing us that the blood of the holocausted Jews ran to the sea in
a huge tidal wave that swept boulders in its path and was so deep that it
reached the nostrils of the Romans' horses.
nationalvanguard.org/2014/09/disillusioned-part-1/