Arc Forumnew | comments | leaders | submitlogin
3 points by fallintothis 5153 days ago | link | parent

This isn't exactly like your code, since I wasn't sure what you wanted it to do in the case of three or more arguments, but the important part is the alternative to map.

  (extend + (n xs . rest) (and (number n) (acons xs) (no rest))
    (treewise cons [only.orig _ n] xs))

  arc> (+ 3 '((1 2) (3 4)))
  ((4 5) (6 7))
  arc> (+ 3 '(1 2 3))
  (4 5 6)
  arc> (+ 5 '((((((5)))))))
  ((((((10))))))
  arc> (+ 3 '(1 2 3) '(4 5 6))
  Error: "+: expects type <number> as 2nd argument, given: (1 2 3 . nil); other arguments were: 3 (4 5 6 . nil)"


3 points by rocketnia 5153 days ago | link

After hammering on your approach a little, here's what I get. ^_^ I like to maintain the fact that Arc's '+ is left-associative, so I don't give myself as much leeway with the additional arguments.

  ; We're going to have a meaning for number-plus-list, so we override
  ; the default number behavior, which assumes that only numbers can be
  ; added to numbers.
  (extend + args (let (a b) args (and number.a number.b))
    (let (a b . rest) args
      (apply + (do.orig a b) rest)))
  
  (extend + args (let (a b) args (and cdr.args number.a alist.b))
    (let (n xs . rest) args
      (apply + (map [+ n _] xs) rest)))
Saying (treewise cons [only.+ _ n] xs) is a bit more wordy than necessary here, but it's still a good way to accomplish (+ n xs) inline, without extending '+.

Oh, hey, if 'treewise could tell that it had a cyclic data structure, it would also be a more robust option... but that's not the case yet. Should it be?

-----

1 point by fallintothis 5152 days ago | link

I like to maintain the fact that Arc's '+ is left-associative

Good work!

if 'treewise could tell that it had a cyclic data structure, it would also be a more robust option

Do you mean cyclic structure like this?

  arc> (= xs (list 1))
  (1)
  arc> (do1 nil (= (cdr xs) xs))
  nil
  arc> (xs 1000)
  1
Never thought of that. I mean, the P part of the vanilla Arc REPL breaks on them, and I've never been compelled to use cycles. When are cyclic lists used? I guess if you're representing certain graphs?

In a more general sense, I wear my opinion about treewise on my sleeve: http://arclanguage.org/item?id=12115.

-----