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

lol MIT

Name: Anonymous 2011-01-18 11:18

Input: print 5 == 5.0

Output: True1

--
1Scheme (the initial language taught at MIT before Python) took type to an extreme. 5.0 and 5 were not considered equal because one was an integer and the other was a float. You weren’t even allowed to do (4 + 6.0.) Python is more rational – it treats the two values as equal.

Name: Anonymous 2011-01-18 14:08

WARNING: BLOAT
#lang racket

(expand #'(and #f 1 2 3 4))
;; expands to:
; (if #f (if 1 (if 2 (if 3 4 #f) #f) #f)
;: should expand to:
; #f
;; Why?
;; Because #f is constant in that expression,
;;+there's no need to generate more code than
;;+needed.
;; If `and' encounters a constant #f during the
;;+expansion, it should just stop there.

(expand #'(or #t 1 2 3 4))
;; expands to:
; (let ((x #t))
;   (if x x
;       (let ((x 1))
;         ...)))
;; should expand to:
; #t
;; Why?
;; Read above, change `and' with `or' and `#f'
;;+with `#t'

(expand #'(and 1 2 3 #t 4 5))
;; expands to:
; (if 1 (if 2 (if 3 (if #t (if 4 5 #f) #f) #f) #f) #f)
;; should expand to:
; (if 1 (if 2 (if 3 (if 4 5 #f) #f) #f) #f)
;; Why?
;; A constant #t is, of course known to be true,
;;+evaluating it doesn't have any side effect, so
;;+that (if #t ... #f) can be optimised away.

(expand #'(or 1 #f 2 3))
;; expands to:
; (let ((x 1))
;   (if x x
;       (let ((x #f))
;         (if x x
;             (let ((x 2)) ...))))))
;; should expand to:
; (let ((x 1))
;   (if x x
;       (let ((x 2)) ...)))
;; Why?
;; Same as and.

;; These macros do the right thing.
(define-syntax (or stx)
  (syntax-case stx ()
    ((~) #'#f)
    ((~ #f t ...) #'(~ t ...))
    ((~ #t t ...) #'#t)
    ((~ t) #'(#%epression t))
    ((~ t r ...)
     #'(let ((x t))
         (if x x (~ r ...))))))

(define-syntax (and stx)
  (syntax-case stx ()
    ((~) #t)
    ((~ #t t ...) #'(~ t ...))
    ((~ #f t ...) #f)
    ((~ t) #'(#%expression t))
    ((~ t r ...)
     #'(if t (~ r ...) #f))))

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