Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Flipping variable

Name: Anonymous 2010-11-05 16:50


if(foo == 0)
    foo = 44;
else
    foo = 0;



How do I change that into a single line that does the trick using arithmetic?

Name: Anonymous 2010-11-06 3:01

>>2
foo = !foo * 44;
Not guaranteed to work.

Name: Anonymous 2010-11-06 6:23

>>5
Would not work. consider foo = 1  in op's code and yours.

Name: Anonymous 2010-11-06 6:24

>>16
Explain yourself.

Name: Anonymous 2010-11-06 6:31

>>14
>I could do that in Scheme:

No. You can't, ``retard''
>(set! foo (if (= foo 0) 44 0))

is equivalent to foo = foo == 0 ? 44 : 0 (>>2), not
foo ?= 0 : 44; (>>13). Count how many times variable name "foo" is encountered in your expression, >>2-kun's expression and how many times in >>13-chan's expression.

Name: Anonymous 2010-11-06 6:56

Now extend it to
foo = foo == a ? b : c;
or even any relational operator in place of ==

It's quite easy.

Name: Anonymous 2010-11-06 6:57

>>19
However, that will definitely be optimised out.

Name: Anonymous 2010-11-06 8:38

inb4 Scheme macro that generalizes for any values of foo and 44

Name: Anonymous 2010-11-06 10:57

>>18
>>16-tan forgot to read section 6.5.3.3.5 of the C Standard today and speaks out of her ass. !x * 44 is guaranteed to work.

Name: Anonymous 2010-11-06 17:23

(module toggle-example ()

(import chicken scheme)
(use extras miscmacros srfi-69)

(define (toggle left right)
  (lambda (x)
    (select x
      ((left) right)
      ((right) left)
      (else (error "toggle" left right x)))))

(define s (toggle 44 0))

(define x 44)
(printf "x before: ~a~n" x)
(modify! x s)
(printf "x after: ~a~n" x)

(define v (vector 0 1 2 3))
(printf "v before: ~a~n" v)
(modify! (vector-ref v 0) s)
(printf "v after: ~a~n" v)

(define l (list 0 1))
(printf "l before: ~a~n" l)
(modify! (car l) s)
(printf "l after: ~a~n" l)

(define h (alist->hash-table '((key . 0))))
(printf "h before: ~a~n" (hash-table->alist h))
(modify! (hash-table-ref h 'key) s)
(printf "h after: ~a~n" (hash-table->alist h))

)

Name: !fAVMdKdIRo 2011-04-18 16:54

butthurt

Name: anus !an/us 2013-03-15 19:33

faggots

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List