Re: problem with (rot) and (link)?

2012-11-06 Thread Laurent Artaud

Hello.
>

The problem is that 'rot' is a "destructive" function, i.e. it modifies
its list argument. You could use 'copy' to avoid this.

I would write

(de split-number (N)
   (mapcar format (chop N)) )

(de all-rotations (N)
   (let SN (split-number N)
  (make
 (do (length SN)
(link (copy (rot SN)))
(println SN) ) ) ) )

(two minor optimizations: 'chop' can also directly work on a number, and
'L' is not needed in the loop).




Thanks for the hint for 'chop': I didn't even consider using it on something 
else than a string...
As for replacing the 'for' by a 'do', I will confess that I totally forgot about 
that one! (I did attempt a 'while' but didn't like the code I got...)


I'm ashamed to say that, after tests, I realized that I didn't need to 'link' 
the result of the 'rot', but '(format (pack (rot SN)))'...


Regards.

--
Laurent ARTAUD
--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: problem with (rot) and (link)?

2012-11-05 Thread Alexander Burger
Hi Laurent,

> I am playing around with Problem 35 on Project Euler and just stumbled on 
> this:
> 
> (de split-number (N)
>(mapcar format (chop (format N))) )
> 
> (de all-rotations (N)
>(let SN (split-number N)
>   (make
>  (for L (range 1 (length SN))
> (link (rot SN))
> (println SN) ) ) ) )
> 
> : (all-rotations 123)
> (3 1 2)
> (2 3 1)
> (1 2 3)
> -> ((1 2 3) (1 2 3) (1 2 3))
> 
> I can't understand why I do not get the same result as the println.

The problem is that 'rot' is a "destructive" function, i.e. it modifies
its list argument. You could use 'copy' to avoid this.

I would write

   (de split-number (N)
  (mapcar format (chop N)) )

   (de all-rotations (N)
  (let SN (split-number N)
 (make
(do (length SN)
   (link (copy (rot SN)))
   (println SN) ) ) ) )

(two minor optimizations: 'chop' can also directly work on a number, and
'L' is not needed in the loop).

Cheers,
- Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


problem with (rot) and (link)?

2012-11-05 Thread Laurent Artaud

I don't understand what is happening, here...

I am playing around with Problem 35 on Project Euler and just stumbled on this:


(de split-number (N)
   (mapcar format (chop (format N))) )


(de all-rotations (N)
   (let SN (split-number N)
  (make
 (for L (range 1 (length SN))
(link (rot SN))
(println SN) ) ) ) )

: (all-rotations 123)
(3 1 2)
(2 3 1)
(1 2 3)
-> ((1 2 3) (1 2 3) (1 2 3))


I can't understand why I do not get the same result as the println.

Did I (yet again) miss-balance the parenthesis? If so, I can't see where...

Thanks.

--
Laurent ARTAUD
--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe