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

A student's question

Name: Anonymous 2011-05-30 12:20

My teacher keeps drilling into our collective heads that using the break command is harmful, and that if we wish to terminate a loop early it would be far better to create a boolean variable, set it to 0, and add a condition to the head of the loop, and set the boolean variable to 1 when a break is needed. Note that this way you actually have to check this every single loop (where it is not needed almost every time) as well as waste a command to reset the boolean in case it was set to true.

Is there a reason to this?

Name: Anonymous 2011-05-30 21:17

>>78
Sorry, I misread,
(define current-function
  (make-parameter #f))

(define-syntax-rule (this-function)
  (object-name (current-function)))

(define-syntax-rule (defun f args . body)
  (define (f . args)
    (parameterize ((current-function f))
      . body)))

(defun f () (this-function))
(f)

Name: Anonymous 2011-05-30 21:17

>>78

$ mzscheme
Welcome to MzScheme v372 [3m], Copyright (c) 2004-2007 PLT Scheme Inc.
    (define this-function
      (make-parameter #f))

    (define-syntax-rule (defun f args . body)
      (define (f . args)
        (parameterize ((this-function 'f))
          . body)))

    (defun f () (this-function))
    (f)

stdin::78: application: bad syntax in: (defun f args . body)
reference to undefined identifier: defun
reference to undefined identifier: f

Name: Anonymous 2011-05-30 21:18

>>79
You can probably have a phaseless syntax-case system.

Name: Anonymous 2011-05-30 21:18

>>81

$ mzscheme
Welcome to MzScheme v372 [3m], Copyright (c) 2004-2007 PLT Scheme Inc.
Sorry, I misread,
(define current-function
  (make-parameter #f))

(define-syntax-rule (this-function)
  (object-name (current-function)))

(define-syntax-rule (defun f args . body)
  (define (f . args)
    (parameterize ((current-function f))
      . body)))

(defun f () (this-function))
(f)reference to undefined identifier: Sorry
stdin::5: unquote: not in quasiquote in: (unquote I)
reference to undefined identifier: misread
stdin::16: unquote: not in quasiquote in: (unquote (define current-function (make-parameter #f)))
reference to undefined identifier: define-syntax-rule
stdin::160: application: bad syntax in: (defun f args . body)
reference to undefined identifier: defun

Name: Anonymous 2011-05-30 21:19

>>82
v372
Are you stuck in 2007 or something?

Name: Anonymous 2011-05-30 21:20

Welcome to Racket v5.1.1.5.
#;> (define current-function
      (make-parameter #f))
#;>
    (define-syntax-rule (this-function)
      (object-name (current-function)))
#;>
    (define-syntax-rule (defun f args . body)
      (define (f . args)
        (parameterize ((current-function f))
          . body)))
#;>
    (defun f () (this-function))
#;> (f)
'f
#;>

Name: Anonymous 2011-05-30 21:20

>>85
Have you heard about backward-compatibility?

Name: Anonymous 2011-05-30 21:21

>>86
You mean this shit isn't standard?

Name: Anonymous 2011-05-30 21:22

>>86

$ mzscheme
Welcome to MzScheme v372 [3m], Copyright (c) 2004-2007 PLT Scheme Inc.
(define current-function
      (make-parameter #f))
(define-syntax-rule (this-function)
      (object-name (current-function)))
reference to undefined identifier: define-syntax-rule

Name: Anonymous 2011-05-30 21:22

>>87
Yes, MzScheme is still supported for backward-compatibility (just like r5rs, just like r6rs, just like #lang scheme), but Racket is not MzScheme.

Name: Anonymous 2011-05-30 21:25

>>88
syntax-case is r6rs standard, (define-syntax (head . args) . body) is a widespread extension to define-syntax, (define-syntax-rule (f . pat) . body) is sugar for (define-syntax f (syntax-rules () (pat . body))), make-parameter and parameterize are R6RS, R7RS and SRFI-I-don't-remember, object-name is non-standard.

Name: Anonymous 2011-05-30 21:29

>>91
That is why I love CL. It's so much simplier!

Name: Anonymous 2011-05-30 21:34

>>92
CLISP has its own non-standard extensions, SBCL has its own non-standard extensions, ClozureCL has its own non-standard extensions. Racket is a language derived from Scheme, use a r5rs-by-default implementation like Chicken.

Name: Anonymous 2011-05-30 21:35

>>93
LispWorks has its own non-standard extensions, CMUCL has them too, and that's why Hemlock is (was?) CMUCL-only.

Name: Anonymous 2011-05-30 21:38

>>92
>>92

(defun f () 2)
(f) ; 2
(defmacro m () '(f))
(m) ; 2
(flet ((f () 3)) (m)) ; 3


(define (f) 2)
(f) ; 2
(define-syntax m (lambda (stx) (quote-syntax (f))))
(m) ; 2
(let ((f (lambda () 3))) (m)) ; 2

Name: Anonymous 2011-05-30 21:41

The jews are after me.

Name: Anonymous 2011-05-30 22:04

>>95
Your indentation is PIG DISGUSTING!

Name: Anonymous 2011-05-30 23:11

>>93
CLISP has its own non-standard extensions, SBCL has its own non-standard extensions...
...but only Scheme/Racket manages to make a whole new language from extensions!

Name: Anonymous 2011-05-30 23:19

>>95
Macro in LISP.

(defmacro aif (cond then else)
  `(let ((it ,cond))
     (if it ,then ,else)))


"Macro" in Scheme/Racket.

#lang racket
(require racket/stxparam)
 
(define-syntax-parameter it (lambda (stx) (raise-syntax-error 'anaphora "missed context" stx)))
 
(define-syntax-rule (aif cond then else)
  (let ([temp cond])
    (syntax-parameterize ([it (make-rename-transformer #`temp)])
                         (if temp then else))))

Name: Anonymous 2011-05-31 0:02

100 get, niggers.

Name: Anonymous 2011-05-31 1:08

>>77
yoba

(设置-定義機能 呼ぶ () (これ-機能))
(呼ぶ)

Name: Anonymous 2011-05-31 2:49

>>35
it uses goto which is by some considered bad. Why? Goto makes programs harder to read and analyze.

also, why not just:

for(int ii=0; ii<1000; ii++) {
    for(int jj=0; jj<1000; jj++) {
        for(int kk=0; kk<1000; kk++) {
            if(wantToBreak) {
                kk=1000;
                jj=1000;
                ii=1000;
            }
        }
    }
}

"breaks" from nested loops without the need of defining extra variable / checking it every loop

Name: Anonymous 2011-05-31 3:39

>>36
The guy who asked the question here. I guess I'll actually need to learn some LISP to figure what is up with that code, but regardles of that I thank you for the attempt.

Name: Anonymous 2011-05-31 3:45

>>102
Your C/C++ indendation is horrible! Use something like:

for (int ii=0
    ;ii<1000
    ;ii++)
   {for (int jj=0
        ;jj<1000
        ;jj++)
       {for (int kk=0
            ;kk<1000
            ;kk++)
           {if (wantToBreak)
              {kk=1000;
               jj=1000;
               ii=1000;}}}}

Name: Anonymous 2011-05-31 4:45

>>104
Why you little schemer

Name: Anonymous 2011-05-31 5:07

>>104
And this, my friends, is why people hate lisp.

Name: Anonymous 2011-05-31 5:09

>>106
He is not >>98-99

Name: Anonymous 2011-05-31 8:23

>>102
Changing the loop variable is argued by some to be even less readable. It's also rather stupid performance-wise, since you're forcing a comparison and I doubt the compiler would be able to replace that with a single jump (feel free to prove me wrong on this).

Name: Anonymous 2011-05-31 8:47

>>108
prove me wrong
$ cc -arch i386 -O3 -fomit-frame-pointer -std=gnu99 -S -o- -xc -
void prog()
{
    int wantToBreak=0;
    for(int ii=0; ii<1000; ii++) {
        for(int jj=0; jj<1000; jj++) {
            for(int kk=0; kk<1000; kk++) {
                if(wantToBreak) {
                    kk=1000;
                    jj=1000;
                    ii=1000;
                }
            }
        }
    }
}
^D    .section    __TEXT,__text,regular,pure_instructions
    .globl    _prog
    .align    4, 0x90

_prog:
    ret



.subsections_via_symbols

Name: Anonymous 2011-05-31 8:48

>>108
I doubt the compiler would be able to replace that with a single jump (feel free to prove me wrong on this).
Let's try!

[/prog/ 0]=> cat goto.c
#include <stdio.h>
main(){int i,j,k;
     for(i=0;i++<1000;)
      for(j=0;j++<1000;)
           for(k=0;k++<1000;)
            if(i==j==k==500)goto breakall;
            else printf("%d:%d:%d",i,j,k);
breakall: return 0;}
[/prog/ 0]=> cat faggot.c
#include <stdio.h>
main(){int i,j,k;
     for(i=0;i++<1000;)
      for(j=0;j++<1000;)
           for(k=0;k++<1000;)
            if(i==j==k==500)i=j=k=1000;
            else printf("%d:%d:%d",i,j,k);
     return 0;}
[/prog/ 0]=> gcc -O3 goto.c -S
[/prog/ 0]=> gcc -O3 faggot.c -S
[/prog/ 0]=> diff -u goto.s faggot.s
--- goto.s    2011-05-31 14:41:25.000000000 +0200
+++ faggot.s    2011-05-31 14:41:30.000000000 +0200
@@ -1,4 +1,4 @@
-    .file    "goto.c"
+    .file    "faggot.c"
     .section    .rodata.str1.1,"aMS",@progbits,1
 .LC0:
     .string    "%d:%d:%d"
@@ -46,7 +46,6 @@
     addl    $1, %edi
     cmpl    $1001, %edi
     jne    .L2
-.L7:
     leal    -12(%ebp), %esp
     xorl    %eax, %eax
     popl    %ebx


Same with -O2, -O1 e no optimization flag. I didn't expect gcc to be this smart, actually.

Clang with no optimization, instead, sets them to 1000, and optimizes to a jmp in -O1.


[/prog/ 1]=> gcc --version
gcc (GCC) 4.6.0 20110513 (prerelease)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[/prog/ 0]=> clang --version
clang version 2.9 (tags/RELEASE_29/final)
Target: i386-pc-linux-gnu
Thread model: posix

Name: Anonymous 2011-05-31 8:49

>>109
Use a printf to not optimize the whole loop away.

Name: Anonymous 2011-05-31 9:00

>>110
I didn't expect gcc to be this smart
I didn't expect /prog/rammers to be this stupid.  Try actually adding a break condition next time.

Name: Anonymous 2011-05-31 9:03

>>112
i++<1000;
j++<1000;
k++<1000;
            if(i==j==k==500)i=j=k=1000;

They are not enough?

Name: Anonymous 2011-05-31 11:15

>>111
Yeah. fputs() requires less computational power than printf()

Name: Anonymous 2011-05-31 12:05

>>114
I don't see any fputs call in >>109, stop trolling.

Name: Anonymous 2011-05-31 14:37

>>115
The comment was made in respond number 111 you fucking retard. Again, do you have ADD? Are you Jewish? Are you a jew with ADD? If so, then HCN might be for you!

Name: Anonymous 2011-05-31 14:38

>>116
YHBT FAGGOT

Name: Anonymous 2011-05-31 14:42

>>117
Shut up princess.

Name: Anonymous 2011-05-31 15:02

>>118
fuck off and die, fag

Name: Anonymous 2011-05-31 15:03

The jews are after me.

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