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

Pages: 1-

Puzzling CONS definition from SICP

Name: Anonymous 2010-12-29 6:27

I tried to use it, but couldnt find a way to discern it from ordinary function.

(define (cons x y)
  (define (dispatch m)
    (cond ((= m 0) x)
          ((= m 1) y)
          (else (error "Argument not 0 or 1 -- CONS" m))))
  dispatch)

(define (car z) (z 0))

(define (cdr z) (z 1))

Name: Anonymous 2010-12-29 6:29

Read SICP.

Name: Anonymous 2010-12-29 6:33


(define (make-ordinary-function fn)
  (define (dispatch m . args)
    (cond ((= m 'type) 'function)
          ((= m 'funcall) (apply fn args))
          (else (error "Invalid funcall"))))
  dispatch)

(define (funcall fn . args) (apply fn 'funcall args))


it would be terrible ineffective and requires rewriting interpreter

Name: Anonymous 2010-12-29 8:15

>>1
It's not different from an ordinary function in your example. It's just an example that shows that a cons could be implemented as a closure. What exactly is a cons? Something that holds a pair of two objects somehow. The internals are irrelevant for that example. What's relevant is that you can access a cons through an interface (car/cdr at least), which is what you do up there. In a real implementation, cons is a real data structure, and you would be able to define all kinds of data structures and so on, for efficiency reasons, but if not for that, you could just use closures for most data structures and it would work just fine.

Name: Anonymous 2010-12-29 8:26

>>4
This implementation doesn't give you pair?, which is used in several examples.

Name: Anonymous 2010-12-29 8:32

>>5
And without `pair?` you cant even write `map` and `fold`.

Name: Anonymous 2010-12-29 8:39

Program like you've got a pair

Name: Anonymous 2010-12-29 8:42

Have you read you SICP today?

Name: Anonymous 2010-12-29 9:48

>>1
Here some code I wrote in my free time, it implements a simple message passing style oop. You can even implement inheritance with a little of effort, by using the super class' 'mmap method.


(define-syntax λm
  (λ (stx)
    (syntax-case stx ()
      ((λm args v ...)
       (with-syntax ((self (datum->syntax #'λm 'self)))
         #'(λ (self . args) v ...)))
      ((λm (a ...) v ...)
       (with-syntax ((self (datum->syntax #'λm 'self)))
         #'(λ (self a ...) v ...))))))

(define mmap
  (λ (m t b)
    (cond ((null? m) (list (cons t b)))
          ((eq? t (caar m)) (cons (cons t b) (cdr m)))
          (else (cons (car m) (mmap (cdr m) t b))))))

(define make-dispatcher
  (λ (m)
    (define dispatcher
      (λ (z . a)
        (cond ((eq? z 'mmap) m)
              ((eq? z 'set-method) (set! m (apply mmap (cons m a))))
              ((assq z m) => (λ (h) (apply (cdr h) (cons dispatcher a))))
              ((eq? z 'get-class) "UNKNOWN")
              ((eq? z 'to-string) (symbol->string (dispatcher 'get-class)))
              (else (error (format "Undefined method \"~a\" for class \"~a\"" z (dispatcher 'get-class)))))))
    dispatcher))

(define make-cons
  (λ (x y)
    (define msg-map
      `((car . ,(λm () x))
        (cdr . ,(λm () y))
        (set-car! . ,(λm (n) (set! x n)))
        (set-cdr! . ,(λm (n) (set! y n)))
        (to-string . ,(λm () (format "(~a . ~a)" x y)))
        (get-class . ,(λm () 'Cons))))
    (make-dispatcher msg-map)))

(define make-linked-list
  (λ (v (n nil))
    (define msg-map
      `((val . ,(λm () v))
        (next . ,(λm () n))
        (set-val! . ,(λm (vn) (set! v vn)))
        (set-next! . ,(λm (nn) (set! n nn)))
        (get-class . ,(λm () 'Linked-List))))
    (make-dispatcher msg-map)))

(define nil
  (make-cons '() '()))
                       
(nil 'set-method 'get-class (λm () 'Nil))

(define cons?
  (λ (obj)
    (eq? (obj 'get-class) 'Cons)))
(define nil?
  (λ (obj)
    (eq? (obj 'get-class) 'Nil)))

(define x (make-linked-list 1
                            (make-linked-list 2
                                              (make-linked-list 3
                                                                (make-linked-list 4
                                                                                  (make-linked-list 5
                                                                                                    (make-linked-list 6)))))))
(x 'set-method 'traverse ; You can dynamically add methods.
   (λm ()
     (let loop ((x self)
                (r '()))
       (if (nil? x) (reverse r)
           (loop (x 'next)
                 (cons (x 'val) r))))))


I wrote this when I was an immature programmer, so it's a bit ugly.

Name: Anonymous 2013-08-31 22:50



                                、 l l|| ll | || l ll| l| | ||lll | ||
           /ヽ::'"´\                ミ
         /:::::/::::::://  _,,.. -─- 、.,        三 そんな馬鹿な…
        く{::::::{:::::::::{>'"´-──- 、., `' 、     ニ
     .ト、_,ノ \:;>''"´ / ,      `    ヽ.    Ξ 流し斬りが
  _.ノ , -  /  / ./   /|  | ヽ、 ヽ.    ',    ニ
   \ /   ,:' /  ;  |!  { | _.」_イ ハ  ';    ;   三 完全に入ったのに…
    ){  .; |  |!- 八  、 7ァh`ヽ |!   | |   |   彡,
    '⌒ヽ  |  、 | /rメ、\/ 弋リ ハ|   ∧/   |    ノ l l|| ll | || l ll| l| | || ll | |l
          、 \」lハlリ,        xx|_/ ||    |
         \__|ァ7x  , -= 、ι /   , ||   '、
          / 人   l    } /   / 八  、 \
         / / //\  、_.ノ ,' /レ'ハ  ヽ }r\}
          レ' |/ {//ヽ--rァ ''レ ト、/|/_」  ,ハノ  ,  '"´ ̄`' 、
            , '"´ ̄`l「\   | 「ヽ  ` く  /         ':,
           ./     '、\ \」 |::::::\/ ∨           }
         /       ∨>‐く7ヽ::::::ヽ  \         /
         .{        、//ア´l∧::r‐、ハ  }         /         _
        rゝ\       / ´二二Y、ハ:::(ア::}rイ ,>--‐ ''"       _,,. <ハ)、
          `7\`''ー 、 イ7  r--─ '::ヽ!:::::::::ム//     //`! _,,. </!,.>''"´ ||
          /  _,,.. - ''"   `_つ:::::::::::o::::::::|∨   , -‐'"ソ|| r</!,.>''"´    ノ }
        / '"´      / ̄:::::::::::::::::::::}!:::::::|_,,,.. r'-く`'く}::|| トl|"´       / ;
         {      ,. '"/:;:::::::::::::::::::::::::::o::::::::}::::::「 、\ヽ_ノヽ_.ノ        /   {
         `'' ー ''"´  ,.く:::::::::::::::::::::::::/=ヽ-ヘァ'",>ー‐'´           {    '、
          ,,.. ::''"/:::::`''ー--‐::''"::::::::::::::::::∨                )     ノ
   _,,.. -:::''"´::::::::::/::::::::::::::::::::::/::::::::::::::::::::::::::::ハ     

Name: Anonymous 2013-09-01 0:21



              ,.へ
             /   \
              _| ̄ ̄「 ̄|─- 「\, ‐ァ‐ 、r..、
     r-...、ァ'´,>'"´ `  ̄ ̄´     |::::::∨   ∨|
   /::::::::::/    /        、 |_/!     |/
  ,>、::::; '     //    /-─ヽ  ヽく:::||    |>
  /::::::::::/     '‐-|   l/ァ‐テ=t、  '; 八    、
   ̄「`7 ./  /ァt、 、  |  |h  リハ  |  ,\   ヽ
   | |  |   {イ hハ \|  ゝ‐゚' ノ  / /  '    ',
   |  、 '、 八 ゝ'  .     x∠, イ   {   |     |
   ;   \|へ,>          /|/、 ヽ、 、   、
   /  //  人    `     / ;/´ \}   \  \
  ;   /  ; ./  |> 、,  _,. イ| /          )   }
  |  {  レ       て|  /lレ' ̄ 7ァー- 、     {   /
  、  ヽ  r、     _r'7 | /    /r'     \  \ |
    \ ハ 「 ̄「| /´ }| _|/    /r'|/     ';   レ'
      ∨ |O) |l|  l {| /:::|\  /r' /       |
       /)_|l| / レ'{::_」  \{r' {   、___,ハ /|
      / /`!∧∨ /:::/       ヽ、r'´:::::::::::::::|K/
      | ' / }、_〉 ;:::::'         ∧r'"´ ̄',l\>
      |  ソ.`八 |::::|        /  ';    ∨

Name: Anonymous 2013-09-01 1:52



         _,.-rー、r─、...,,__
      ,r'"「 、_i,. -- ..L..7ヽ7-、_
     _rヘ'ゝ'´ `ヽ-ー-、,.--`、_/-iヽ.
     「γ´ /⌒´    ー-.、 ヽイヽ. ',
      7  / /、._/ ハ  i  、  ', ヽイ i
    ノ  ハ/rr!、ゝ.! ! ハ_!,.ィ  i  ', ハ _人__.
     /、 / .ハ' ヒ__ン レ' r!-=!、!ヘi、  ゝ '、  (_
    ´ .!イ ハ/!."       ヒ__,ンハ ハ ! /´゙〉 Y´ 
     /V !.人  `___    ""ハ,-'、ヘ.,' /i
    ,' / (Y.ノ゙ヽ.   `  ,.インハ. ',./ / ハ
    レ^iヽ/iY)´`7`T 7´ (y´ γ! ` /´〉ヽ_
      〉イ〈}>イ>.〈ハ / i Yノ、 .l !  -!_/ / ) 
    ,.イ 〈ハ7   Lムヽ」 }>イ」、 ゙ゝ-、_`フ´
    〈 r`ーハ!   レヘ__」    ,,.7`゙'ー─'´
   / __ ./l、   ⌒    _,,イ/  ___
  〈   `ヽ、!,-、        !、イ´- 7
   `ヽ、 ,'"、ヽ、).ハ、___,,,,,....イ´ゝ、  <
    ,!イ、!、ヽ)-'----─=r'rイヽ、\「ヽ
  -''"/  !7´      ノト-(  ヽ、`ヽ、
                Y    ヽ  ヽ.

Name: Anonymous 2013-09-01 3:23



             _,,,....,,,_  ,. '´ ̄` 、        />
     _」_   ,.  '"´     /   o , - ヽ      / r'
   ,X´   , '          { '⌒´ 、 、_} ,‐-、   く/| |
 `/    /     /      '、 イ、_,. =ァ' r'っノノ)、   |_| _「l___
 ┼  /    , '  l    ト、  ヽ\ `'´/ ̄`' 、)'ソ     └┐r‐‐ァ /
   ∠,'  l/,  |!   八-\-ハ ヽ /      \       .| |</
     {   l/|-‐八   ' ァ七Tヽl  }/ ̄`' 、  /        |_|
     '、   { lrl七ミ \|  乂_ノ ノ|  ,/     \/          [] []r、
     ノイ |八乂ノ      ""ノイ/      /´     _____  ノノ
        レ'l 7"     _   ´} .//    ,:'  /l_|       | ´
       |l人    ´     / l/´    /|   \r‐┐ ド ヤ 顔
       |  |> 、.,_    イ //     / 、     |_____| 「l_
       ,'  八   }`斤__//.'    , '   `' ー‐ ')        └‐┐r‐┘
       / ,' 、  ,ィr‐ァrァ'" レ{   ./     ー<         .ノ] | | [ヽ
      ,'/{  ∨レムヽ-┘ 、 , イ    _    `゙'' 、       |_| 
      {(  '、 ; | L☆ヽ」    / }    `ヽ      \       r┐
      ヽ} /_ヽ.| {  ○      { ノ      }ヽ.      ':,    r‐┘ ー┐
       / 、ヽ}∧  △     \‐- 、.,,   ノ  ',       `''ー 、  ̄7. 「´
      r'{ 、ソ}ヽハ  ×      >   ` 'く   }  ノ     }⌒ヽ) くノ|_|
      | lゝ'_ノ }く  /\    / 、      ':, ノ/     ;  _r、_
      ヽ.,_,,..イ/ `7   `'┬'   ':,   ,. = ', ´      /  |_r─┐|
        /   lァ  { , ==、 { ゝ==' '   -rァ }     /      ノ ノ
       .{   {=-  ' -= =-', -= =-}   , =,'_,,.. ‐''´       く/
        `ー-人=ァ  '、 ,==、 } ゝ=='ノ   /        r- 、
           r> 、.,_ヽ_,,.. '--‐ <  __ノ           | /r- 、
           `''ー-、___.ノ ァ‐ '´ ̄           レ' | /
               ヽ{   /                 O  レ
                }  ,'ヽ                  O
               r'ー‐'、:::::}
               }>rrく{ノ}::ノ
              ノ    r´
              (    ノ
              `''ー ''´

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