Arc Forumnew | comments | leaders | submitlogin
My first Lisp program ever (in Arc of course).
4 points by coconutrandom 5437 days ago | 25 comments
Guess the number from 0 to 100. June 5, 2009 rev 1

  ;To run:
  ;arc> (load "guess.arc")
  ;arc> (guess)

  (def guess ()
      (with (tries 10 r (rand 100))
          (while (> tries 1)
              (= tries (- tries 1))
              (prn tries " tries")
              (pr "your guess: ")
              (= y (read))
              (prn)
              (if (< y r) (prn y " is too low" )
                  (> y r) (prn y " is too high" )
                  (do 
                      (prn "yay")
                      (pr "you got it in " (- 10 tries) " tries.")
                      (= tries 1))))))
                      
  (guess)
  
Any feedback would be cool. Optimization, best practice, derision?


5 points by pg 5437 days ago | link

This reproduces the behavior of yours (possibly too) exactly, including giving the user 9 guesses and saying "1 tries" if you get it right the first time. The return value is different though.

    (def guess ((o tries 1) (o r (rand 100)))
      (when (< tries 10)
        (pr "your guess: ")
        (let y (read)
          (prn)
          (if (is y r)
              (do (prn "yay")                  
                  (pr "you got it in " tries " tries."))
              (do (prn y " is too " (if (< y r) "low" "high"))
                  (guess (+ tries 1) r))))))

-----

1 point by coconutrandom 5437 days ago | link

Ahh... concise and readable. Thank you!

[edit] I just found that the "o" makes the arguments optional. That explained a lot.

-----

2 points by Adlai 5435 days ago | link

It's a good start -- it works, which always is a plus :)

However, a few comments about your version:

- You use an iterative 'while, rather than the tail-recursive approach that Paul Graham posted. Both end up eventually as tail-recursive code, however, I think that it's better, in this case, to write the tail-recursion directly. The reason for this is that in your version, you have to explicitly "break out" of the loop (by setting 'tries to 1), while in Paul's version, he just doesn't call the next "iteration".

- You should probably use 'prn for the last message -- when I run this, "nil" gets tacked on to the end of the message.

- This is more just a matter of convention, than an actual problem, but your indentation isn't completely standard. Usually, body code for macros is only indented by two spaces. Also, having several tokens on one line can get a bit confusing, and in a form like 'with, I think it helps to organize them. I think the start of your code could be more readable like this:

  (def guess ()
    (with (tries 10
           r (rand 100))
      (while (> tries 1)
        etc)))
As for some derisive best practice about optimization -- "Premature optimization is the root of all evil." (Donald Knuth)

-----

2 points by shader 5430 days ago | link

Does anyone know which forms are indented two spaces and which are indented to the beginning of the second item on the previous line?

For example, def, mac, with, while, etc. seem to be indented two spaces, but if, do, and most function calls are indented more.

-----

3 points by CatDancer 5430 days ago | link

If all the following lines go together as a group, and the arguments on the first line aren't part of that group, then I'll indent the following lines differently to emphasize the grouping:

  (each (k v) mytable
    (prn "my key is " k)
    (prn "and my value is " v))
It doesn't matter to me whether it's a macro or a function:

  (copy (obj a 1)
    'b 2
    'c 3)
I use the two space indentation a lot more with macros than I do with functions because many macros use the pattern of some arguments followed by a body and not many functions do, but I don't change my indentation depending on whether it's a function or a macro.

If the lines after the first line don't fall into their own group, then I'll line up the arguments:

  (+ '(a b c d)
     '(e f g h)
     '(i jk))

  (map (fn (x) ...)
       (generate-my-list ...))
For me the important consideration is using indentation to show which arguments go together.

-----

1 point by Adlai 5430 days ago | link

I think there's a general tradition in Lisp indentation, which works essentially as follows:

Function calls are indented with all the arguments starting at the same column, for example:

  (calculate-fn-of arg1
                   arg2
                   arg3)
Macro calls are indented with the arguments two spaces in, for example:

  (while (< x 10)
    (prn x)
    (++ x))
This is usually done to emphasize the "body" of the macro arguments. So in macros which take several arguments before the "body" code, you'd put those arguments either on the same line, or indented in some way to distinguish them from the body. For example:

  (with (var1   val1
         thing2 val2
         var3   expr3)
    (do
      (stuff)
      (with-vars)))
However, some forms are indented differently for clarity. This doesn't seem to be as standardized. An example would be Arc's 'if macro. It makes sense to indent it with all the conditions on one line, but where do the result forms go? Possibilities are:

  (if (test1)
       (response1)
      (test2)
       (response2)
      (response3))

  (if (test1) (response1)
      (test2) (response2)
              (response3))

  (if (test1)
      (response1)
      (test2)
      (response2)
      (response3))

  (if
    (test1)
      (response1)
    (test2)
      (response2)
    (response3))
I actually haven't seen the last form used at all, but it seems very clear to me. I'm guessing some convention will emerge over time.

-----

1 point by shader 5430 days ago | link

I just noticed that there is a file named CONVENTIONS in Anarki that states comment and indentation conventions for arc. They say pretty much what has been said so far, but may be worth looking at if you are interested.

-----

1 point by conanite 5430 days ago | link

I usually

  (if a b
      c d
        e)
if it fits, otherwise

  (if a
      b
      c
      d
      e)
The wavy-style if indentations make me seasick.

-----

1 point by Adlai 5430 days ago | link

I think that your first example is probably best in terms of compactness and clarity (associating each condition with its corresponding code). The completely straight indentation can be difficult to scan quickly if you have more than one condition. IMO, the only problem with your first example is when the conditions span more than one line or are very long.

-----

1 point by Adlai 5430 days ago | link

In terms of 'if, I was looking through some of pg's code just now, and noticed that he does use the wavy form of indentation. Thus, if you're big on proof by authority, the correct form of indentation for 'if would be the wavy one...

-----

3 points by conanite 5430 days ago | link

I was looking through some of pg's code just now

arc code or scheme code?

I once heard a great teacher say that appeal to authority is flawed rhetoric, so I never do it :)

-----

2 points by shader 5429 days ago | link

Never heard that one before ^^

However, in this case I agree with Adlai: pg had even planned on getting ppr to "properly indent long ifs". I presume that means to implement the wavy feature. It's also the convention for Anarki, according to the CONVENTIONS file.

So, here's what I'm thinking should be done for ifs:

1) If you only have three expressions, if-then-else, use:

  (if a
      b
      c)
1) If you like the format, and the expressions are very short use:

  (if a b
      c d
      ...
        e)
3) If you don't like that form, or the expressions are longer, use:

  (if (a       )
        (b       )
      (c       )
        (d       )
      (e       ))
I'm not sure whether to use one or two spaces between clauses. The anarki conventions file uses two spaces. Elsewhere I've seen only one space. So, which is it? Now or never ;)

(I'm writing a new version of ppr with proper indentation of forms, in case you wanted to know why I'm so interested in the indentation question)

-----

2 points by Adlai 5429 days ago | link

I think two is better, because it a) clearly distinguishes it as indentation (rather than just an accidental #\space), and b) it's consistent with the indentation of macros & co.

-----

1 point by shader 5427 days ago | link

Two spaces it is.

Another question:

How do people indent do, and, or, etc.:

a)

  (do abc
      bcd
      cde)
or b)

  (do
    abc
    bcd
    cde)
Choice a is more like a function call, b is more like a macro. Should it be a community standard, or on a user by user basis?

-----

1 point by CatDancer 5427 days ago | link

I use A, for me the space taken up by the extra indentation is less than the extra line taken up for B.

I don't use indentation as a way to indicate whether I'm calling a function or using a macro.

-----

1 point by shader 5427 days ago | link

Next question:

Should case be indented like 'if:

  (case
    a
      (b     )
    c
      (d     )
    (e      ))
or in pairs like with:

  (case
     a (b    )
     c (d    )
     (e     ))
?

Should it depend on whether they fit on one line or not? If they don't fit, should it still be like b., just with the second part spilling onto the next line?

-----

1 point by CatDancer 5427 days ago | link

I use the second if it fits OK, or the first if they don't fit.

-----

1 point by absz 5425 days ago | link

Ditto, except I write

  (case
    a-short     (b ...)
    c-very-long (d ...)
                (e ...))
when everything is on one line, and

  (case
    a-short
      (b ...)
    c-very-long
      (d ...)
    ; else
      (e ...))
when it doesn't (though I go back and forth about the ; else).

-----

1 point by shader 5425 days ago | link

Your first version is the one currently implemented by ppr.arc. If you would like, you can write the indentation function for your second version (it can even include the "; else")

Otherwise I think that the first version is generally clearer, and the second is rarely needed, as the value of the case statement can't usually be very long. Could you give me an example where you use your second version?

-----

1 point by absz 5425 days ago | link

Sure; here's something from my tagged-unions.arc on Anarki (Arc 2). It's responsible for parsing the types specified for slots in the tagged union:

  (each type-frag types
    ; Assemble the ('type name) pairs into two lists, so that we can iterate
    ; through them.
    (case (type type-frag)
      ; If it's a symbol, then we're defining a new name; add a new set of
      ; values, and go.
      sym
        (do
          (zap [cons type-frag _] names)
          (zap [cons nil       _] values))
      ; Otherwise, we're adding a value to an existing variant.
      cons
        ; I changed my mind about the order (now it's (name pred?) instead of
        ; (pred? name)), so I'm reversing it here.
        (zap [cons (rev type-frag) _] (car values))
      ; else
        (err "Invalid member of tagged union declaration.")))
I should also add (just as another data point) that my multi-condition ifs look like that too, and single-condition ifs look like the following:

  (def macexn (n expr)
    " Macroexpand `expr' `n' times.  NB: `expr' *is* evaluated!
      See also [[macex1]] [[macex]] "
    (if (and (number n) (> n 0))
      (macexn (- n 1) (macex1 expr))
      expr))
I'm not hugely wedded to any of my conventions, though, and I haven't coded in Arc for quite a while anyway; this isn't intended to get you to change anything or convert people to my style. As I said, it's just another data point.

-----

1 point by Adlai 5430 days ago | link

Well, the 'if macro only exists (as a macro) in Arc.

However, the indentation actually isn't completely consistent. There are a bunch of places where pg does a straight indentation for 'if.

-----

1 point by pg 5430 days ago | link

I've never felt 100% sure about it though.

-----

1 point by Adlai 5430 days ago | link

Well, I think it adds just enough clarity to be worth the "seasickness". I guess it's also related to the indentation from CL 'cond:

  (cond ((condition1)
         (result-code1))
        ((condition2)
         (result-code2))
        (default-code))

-----

1 point by coconutrandom 5430 days ago | link

:D thank you! I didn't know both end up with tail recursion and the 'nil was also bugging me

I've already installed Arc on my webhost. Now I'm picking my way through the webserver and am translating this to a web app.

-----

1 point by shader 5430 days ago | link

The nil is the return value of the function. Since the last thing you did was an assignment, is 'nil. It only shows up like that on the repl, which prints both stdout and return values. It is true that printing a newline first would make it show up in the next line, but it doesn't really matter if you're making it into a web service since the nil won't be printed.

-----