Arc Forumnew | comments | leaders | submitlogin
3 points by drcode 5614 days ago | link | parent

> sequential statement thinking will not work

Actually, the best arc code IMO uses a fifty-fifty mix of functional and sequential thinking....

> my "set x" functions were clobbering each other

As a beginner you probably want to use '= instead- That's the more general command. Most arc lispers will only use 'set in rare cases.

> (def ugh (string) ...

Avoid naming the variable 'string: It will probably be OK, but since arc has functions and variables in the same namespace (google "lisp-1") you're clobbering the 'string function.

Here's how I'd rewrite this code (as far as I understand it, which isn't very far, I'm afraid so this is a very very rough approximation :-)

  (def ugh (str) 
   (let x 5
    (forlen i str 
       (let v (trunc:/ 12 (+ i 1))
         (if (is #\A str.i)
	     (++ x v)
	     (-- x v)))
    x))
The short of it is, you wouldn't necessarily get rid of the set (the '++ and '-- are just variants of 'set)

If you really, really wanted to get rid of the "sets" you could write something as the following, which is in pure functional style (you might not want to do this, though... there's better languages for experimenting with a pure FP style, like Haskell)

(Again, very rough, untested code)

  (def ugh (str) 
   (apply +
    5
    (map (fn (c i)
             (let x (trunc:/ 12 n)
                 (if (is #\A c)
                     x
                     (- 0 x))))
         str
         (range 1 len.str))))
If writing in this "pure" style really interest you, I'd suggest switching from arc to Haskell first. After 6 months of Haskell, writing code without "sets" will become second nature. (At that point, you'll also realize what a pain in the ass Haskell programming is and will return to arc :-)

Oh, and the posting trick is to surround your code by empty lines and have it indented two spaces.



1 point by skenney26 5614 days ago | link

Another twist is to use the 'on interator which binds its first argument to each successive element and implicitly binds 'index to the index of that element.

  arc> (on c "arc" (prn index ":" c))
  0:a
  1:r
  2:c
  nil

  (def ugh (str)
    (let x 5
      (on c str
        (let v (trunc (/ 12 (+ index 1)))
          (if (is c #\A)
              (++ x v)
              (-- x v))))
      x))

-----

1 point by drcode 5614 days ago | link

ah... I should be using 'on more often....

-----