Object oriented approach to functions in Scheme

imageHello. In this article I would like once more to highlight the issue of object-oriented programming in the language Scheme, as it is regarded in the book "Structure and interpretation of computer programs".
Then offer those who never programmed in Scheme, download DrRacket and try the steps go through the examples in this article. Experienced programmers Scheme, Racket... this article will be very boring, as it is written for beginners and people wanting to "touch" racket.

And so in order. Let's warm up for the start of the DrRacket IDE.
Create a new program file. Save it.
Insert at the beginning of the file Directive indicates the language:
the
#lang racket

Define a function:
the
(define (func1 x) x) accepts on input x, returns x

To use the function:
the
(func1 10) ; the result will be 10


Now define another function:
the
(define (func2) "func2")

And now try to define a variable such that it applies a first function to the second and returns the second function:
the
(define x (func1 func2)) ; now x is an object instance of the function func2

You need to use x as a function:
the
(x) ; return flow "func2"

Here we used the possibility that the function may return functions as their result can be defined as variables.
Next, let's create an object that encapsulates the internal variables and other features:
the
(define (MyObject field1 field2) ; the object which had the design of two variables field1 and field2 
(let ((f1, field1) ; initialize the internal variable f1, using field1
(f2 field2)) ; ...
(define (get-f1) f1) ; return the value of the internal field f1
(define (get-f2) f2) ; ...
(define (set-f1 x) (set! f1 x)) ; assign f1 the value of x
(define (set-f2 x) (set! f2 x)) ; ...
; next is the most interesting
(define (dispatch m) ; function scheduling functions in our object
(cond ((eq? m 'get-f1) get-f1) ; if m is equal to get-f1, it returns a function get-f1
((eq? m 'set-f1) set f1) ; ...
((eq? m 'get-f2) get-f2) ; ...
((eq? m 'set-f2) set-f2) ; ...
)
)
dispatch)) ; here we are in function MyObject returned by the function scheduling 

Well, the object is defined, now make instance of MyObject:
the
(define Obj1 (MyObject, "Hello "" world!!! ")) ; now the instance of Obj1

Then just use the instance as you want:
the
(display ((Obj1 'get-f1))) ; here f-Oia display something like printf and the double brackets
; ((Obj1 'get-f1)) need to write in order to calculate the function get-f1
(display ((Obj1 'get-f2))) ; similarly
(newline) ; newline
; the result will be "  Hello  world!!! "

Create a new instance of the same object:
the
(define Obj2 (MyObject, "Hello "" habra!!! ")) ;
; result: "Hello Hello habra!!! world!!!"

Try to execute:
the
(display ((Obj1 'get-f1))) 
(display ((Obj1 'get-f2))) 
(newline) 
(display ((Arg2, 'get-f1))) 
(display ((Arg2, 'get-f2))) 
(newline)
; the result will be "Hello world!!!"
; and "Hello habra!!!" 

What struck me — each instance returns the one and same function, but their behavior is not the same.
That is, in Scheme, unlike C++, the functions take variables and functions to compute from its instance.
This feature greatly helps the organization of the list of functions to change the internal States of different instances of one or more objects:
the
(define func-list (list (Obj1, 'get-f1) (Arg2 'get-f1) (Arg2 'get-f2) (Obj1, 'get-f2)))

Print each function from this list:
(map (lambda (x) (display (x))) func-list)
; result: "Hello Hello habra!!! world!!!"
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Fresh hay from the cow, or 3000 icons submitted!

Knowledge base. Part 2. Freebase: make requests to the Google Knowledge Graph

Group edit the resources (documents) using MIGXDB