Arc Forumnew | comments | leaders | submitlogin
= conditional and if? def in DrScheme(mult. lines)?
2 points by globalrev 6057 days ago | 10 comments
if i want to do this: if x==0 return 1 else return 2

how do i write that in Arc?

(= 1 1) returns T in Common Lisp but in Arc you get "cant take car of 1".

also in DrScheme, when you press enter when using def it shifts row and is unindented. is there a way not to use the rectangecursor and have the text being indented?



2 points by tokipin 6057 days ago | link

as sacado wrote, you would write it:

  (if (is x 0)
      1
      2)
a nifty thing here is that the if expression returns the value of whichever expression is selected, so you can do:

  (prn (if (is x 0)
      1
      2))
which would be the equivalent of the algol:

  print if x==0
      then 1
      else 2
which would print either 1 or 2. same goes for any other form, including such things as case statements

also, check out the tutorial. despite it being being in .txt (probably for formatting purposes,) it's actually good:

http://ycombinator.com/arc/tut.txt

ken's reference site is also darn handy:

http://arcfn.com/doc/index.html

-----

1 point by sacado 6057 days ago | link

Yep. if actually behaves likes the ?: operator in C

  printf ("%d\n", x == 0 ? 1 : 2);

  (prn (if (is x 0) 1 2)

-----

1 point by jmatt 6057 days ago | link

I think a macro solves the problem where (is 0 0.0) returns nil. I think the reason they are not equal has something to do with precision and the representation of 0 versus 0.0.

  ;allows equivalent ints to be equal.
  ;i.e 0 = 0.0, 1 = 1.000000, etc.
  (mac == (a b)
        `(if (and (is (type ,a) 'int)
                   (is (type ,b) 'int))
               (is (+ ,a 0.0) (+ ,b 0.0))
               (is ,a ,b)))
produces

arc> (== 0 0.0)

t

arc> (== 123 123.00)

t

arc> (== "a" "a")

t

arc> (== "a" "b")

nil

arc> (== 1 9)

nil

arc> (== 1 1.00000001)

nil

Interesting quirk. Glad I know that (is a b) may not always work.

-----

1 point by sacado 6057 days ago | link

(= 1 1) is written (is 1 1) in Arc. The equal sign is used for affectation. You get an error as 1 cannot be set. So, for your first question,

  if x == 0 return 1 else return 2
is translated as

  (if (is x 0) 1 2)
As for DrScheme, I don't know, sorry :)

-----

4 points by dreish 6057 days ago | link

Although this does bring up an unfortunate characteristic of 'is':

  arc> (is 0 0.0)
  nil
It might be nice to have an == that does a numeric comparison, if floats and ints are always going to be considered unequal by 'is'.

-----

2 points by tokipin 6057 days ago | link

in the meantime i have devised a clever scheme i call "natural coercion":

  arc> (with (x 0 y 0.0)
          (is (+ x 0.0) (+ y 0.0)))
  t
it can be toolified as:

  (def eq args
      (apply is (map [+ _ 0.0] args)))

-----

2 points by eds 6057 days ago | link

I would have expected 'iso to work for that, but it also returns nil.

  arc> (iso 0 0.0)
  nil

-----

1 point by sacado 6057 days ago | link

You're right. This is really strange as 0.0 is considered as an 'int.

-----

3 points by rincewind 6057 days ago | link

This may be related to http://arclanguage.org/item?id=5720

-----

1 point by sacado 6056 days ago | link

Hmmm... very possible...

-----