scheme IO实现来自于CPS或者haskell的monod范畴思想

scheme中IO的实现类似于CPS在原来函数的基础上增加了一个控制变量。

之前曾经写过CPS的控制过程

original style

(define rember8
  (lambda (ls)
    (cond
      [(null? ls) '()]
      [(= (car ls) 8) (cdr ls)]
      [else (cons (car ls) (rember8 (cdr ls)))])))

cps style


;1
(define rember8
  (lambda (ls k)
    (cond
      [(null? ls) (k '())]                           ==> k
      [(= (car ls) 8) (cdr ls)]
      [else (cons (car ls) (rember8 (cdr ls)))])))

;2
(define rember8
  (lambda (ls k)
    (cond
      [(null? ls) (k '())]
      [(= (car ls) 8) (k (cdr ls))]                  ==>k
      [else (rember8 (cdr ls) (lambda (x) (cons (car ls) x)))]))) ==> rember8

;3
(define rember8
  (lambda (ls k)
    (cond
      [(null? ls) (k '())]
      [(= (car ls) 8) (k (cdr ls))]
      [else (rember8 (cdr ls) (lambda (x) (k (cons (car ls) x)))]))) ==>  k 

这个k的作用将在IO中体现出来。


(define o (open-output-file "greeting.txt"))
(display "hello" o)
(write-char #\space o)
(display 'world o)
(newline o)
(close-output-port o)


结果:

result

其他IO语法

  • open-input-file

;;打开文件句柄 用于读操作
(define in10 (open-input-file "greeting.txt"))
;;利用read-char 读取单个字符char
(read-char in10)
(read-char in10)
(read-char in10)
;;利用read 读取一个字word
(read in10)
(read in10)
;;利用read-line读取一行字符
(read-line in10)
(read-line in10)
(read in10)

;;结果
    #\h             ==>read-char
    #\e             ==>read-char
    #\l             ==>read-char
    'lo             ==>read
    'world          ==>read
    ""              ==>read-line
    "made in DaXi"  ==>read-line
    #<eof>          ==>read
  • open-input-string
 (define i (open-input-string "hello world"))
 (read-char i)
    #\h       ==>read-char
 (read-char i)
    #\e       ==>read-char
 (read-char i)
    #\l       ==>read-char
 (read i)
    'lo       ==>read
 (read i)
    'world    ==>read
 (read i)
    #<eof>    ==>read
  • open-output-string

;;;定义字符串变量
(define op (open-output-string))
(write 'hello op)
(write-char #\, op)
(display " " op)
(display "world" op)

;;获取存取的值
(get-output-string op)


;;结果
    "hello,world"    ==> get-output-string

其中o的作用其实就是类似于k的作用,都是体现者一种程序的控制一种续延(具体未讲的十分清楚,待以后补充load等)。

令狐冲
令狐冲
Engineer of offshore wind turbine technique research

My research interests include distributed energy, wind turbine power generation technique , Computational fluid dynamic and programmable matter.

Related

本站访问量: