Perl in Scheme: A DSL KW.PM 2007/10
Perl in Scheme: A DSL
Abram Hindle
Kitchener/Waterloo Perl Mongers Canada http://kw.pm.org/
{abez+kw}@abez.ca
Abram Hindle 1
Perl in Scheme: A DSL Abram Hindle Kitchener/Waterloo Perl Mongers - - PowerPoint PPT Presentation
Perl in Scheme: A DSL KW.PM 2007/10 Perl in Scheme: A DSL Abram Hindle Kitchener/Waterloo Perl Mongers Canada http://kw.pm.org/ { abez+kw } @abez.ca Abram Hindle 1 Perl in Scheme: A DSL KW.PM 2007/10 Overview Motivation Scheme
Perl in Scheme: A DSL KW.PM 2007/10
Abram Hindle
Kitchener/Waterloo Perl Mongers Canada http://kw.pm.org/
Abram Hindle 1
Perl in Scheme: A DSL KW.PM 2007/10
Abram Hindle 2
Perl in Scheme: A DSL KW.PM 2007/10
to be very flexible
– But I’m working in Scheme!
Abram Hindle 3
Perl in Scheme: A DSL KW.PM 2007/10
presentation form a DSL because they provide new syntax
Abram Hindle 4
Perl in Scheme: A DSL KW.PM 2007/10
Abram Hindle 5
Perl in Scheme: A DSL KW.PM 2007/10
– Numbers (integers, floats, rationals, etc.) – Symbols ’this-is-a-symbol – procedures (f x) – Strings “Hi!” – chars # A (Capital A) – pair (a . b)
Abram Hindle 6
Perl in Scheme: A DSL KW.PM 2007/10
– list (a b c) – booleans #t #f – Vector – ports
Abram Hindle 7
Perl in Scheme: A DSL KW.PM 2007/10
– Prefix notation, not infix. – (function-name param1 param2 param3) – (= (+ (* 1 2) 3) 6)
Abram Hindle 8
Perl in Scheme: A DSL KW.PM 2007/10
– Just about everything in scheme is made up of linked lists – (cons a b) makes a cons cell of a b
Abram Hindle 9
Perl in Scheme: A DSL KW.PM 2007/10
– car gets the head of a cons
Abram Hindle 10
Perl in Scheme: A DSL KW.PM 2007/10
– cdr gets the tail of a cons
Abram Hindle 11
Perl in Scheme: A DSL KW.PM 2007/10
– cadr - gets the head of the tail of a cons cell
Abram Hindle 12
Perl in Scheme: A DSL KW.PM 2007/10
– lambda produces an anonymous function – define produces a named function – lambda can be assigned in defines and lets
Abram Hindle 13
Perl in Scheme: A DSL KW.PM 2007/10
– (lambda (x) x) - identity of 1 argument – (define (identity x) x) - named function identity – (define (square x) (* x x))
Abram Hindle 14
Perl in Scheme: A DSL KW.PM 2007/10
– Functions which return lists of symbols which look like scheme code that become scheme code – Get around problems that simple function calls cannot
Abram Hindle 15
Perl in Scheme: A DSL KW.PM 2007/10
Abram Hindle 16
Perl in Scheme: A DSL KW.PM 2007/10
– R5RS is less than 60 pages
( require−extension posix ) ( require−extension srfi−1 ) ; ; l i s t s @@ ( require−extension srfi−13 ) ; ; s t r i n g s @@ Abram Hindle 17
Perl in Scheme: A DSL KW.PM 2007/10
( slurp−file f i l e ) ( with−input−from−file f i l e read−all−lines ) ) Abram Hindle 18
Perl in Scheme: A DSL KW.PM 2007/10
(ENV x ) ( cdr ( assoc x ( current−environment ) ) ) ) ) Abram Hindle 19
Perl in Scheme: A DSL KW.PM 2007/10
( define−macro pop ! ( lambda ( x ) ‘ ( i f ( null? , x ) # f ( l e t ( ( r ( reverse , x ) ) ) ( l e t ( ( k ( car r ) ) ) ( set ! , x ( reverse ( cdr r ) ) ) k ) ) ) ) ) Abram Hindle 20
Perl in Scheme: A DSL KW.PM 2007/10
( define−macro push ! ( lambda ( x y ) ‘ ( set ! , x ( reverse (cons , y ( reverse , x ) ) ) ) ) ) Abram Hindle 21
Perl in Scheme: A DSL KW.PM 2007/10
( define−macro u n s h i f t ! ( lambda ( x y ) ‘ ( set ! , x (cons , y , x ) ) ) ) Abram Hindle 22
Perl in Scheme: A DSL KW.PM 2007/10
( define−macro s h i f t ! ( lambda ( x ) ‘ ( i f ( null? , x ) # f ( l e t ( ( k ( car , x ) ) ) ( set ! , x ( cdr , x ) ) k ) ) ) ) Abram Hindle 23
Perl in Scheme: A DSL KW.PM 2007/10
( perl−argv) ( l e t ( ( args ( argv ) ) ) ( i f (and ( p a i r ? args ) ( string=? ” csi ” ( pathname−file ( car args ) ) ) ) ( cddr args ) ( cdr args ) ) ) ) Abram Hindle 24
Perl in Scheme: A DSL KW.PM 2007/10
( l e t ( ( ismy? ( lambda ( x ) (and ( p a i r? x ) ( eqv? ’my ( car x ) ) ) ) ) ) ( lambda x ( l e t loop ( ( l x ) ) ( i f ( p a i r ? l ) ( l e t ( ( head ( car l ) ) ) ( i f ( ismy? head) ( l i s t ’ l e t ( l i s t ( l i s t ( cadr head) ( caddr head ) ) ) ( loop ( cdr l ) ) ) ( i f ( p a i r? ( cdr l ) ) ( l i s t ’ begin ( car l ) ( loop ( cdr l ) ) ) ( l i s t ’ begin ( car l ) ) ) ) ) ) ) ) ) ) Abram Hindle 25
Perl in Scheme: A DSL KW.PM 2007/10
( define (begin−my−test) (begin−my (my a 99) (my b 2) ( set ! a (+ a b ) ) ; 103 ( print a ) ( set ! a (+ a b ) ) ; 105 ( print a ) (my c 3) (my a (+ a c ) ) ( print a ) a ) ) Abram Hindle 26
Perl in Scheme: A DSL KW.PM 2007/10
has one – R5RS doesn’t
( define (hash−cons−key hash−table key value ) Abram Hindle 27
Perl in Scheme: A DSL KW.PM 2007/10 (hash−table−update ! / d e fa u l t hash−table key ( lambda ( x ) (cons value x ) ) ’ ( ) ) ) Abram Hindle 28
Perl in Scheme: A DSL KW.PM 2007/10
(make−easy−hash) ( l e t∗ ( ( h (make−hash−table ) ) ( acc ( lambda ( x . y ) ( case x ( ’ get (hash−table−ref h ( car y ) ) ) ( ’ set (hash−table−set! h ( car y ) ( cadr y ) ) ) ( ’ has ( hash−table−exists? h ( car y ) ) ) ( ’ del ( hash−table−delete! h ( car y ) ) ) ( ’ cons (hash−cons−key h ( car y ) ( cadr y ) ) ) ( ’ a l i s t (hash−table−
>a l i s t
h ) ) ( else ( error ( l i s t ”Don ’ t know what t h i s i s : ” x ) ) ) ) ) ) ) acc ) ) Abram Hindle 29
Perl in Scheme: A DSL KW.PM 2007/10
( l e t ( ( h (make−easy−hash ) ) ) ( h ’ set ” l o l ” ” hy ” ) ( print ( h ’ has ” l o l ” ) ) ( print ( h ’ get ” l o l ” ) ) ( h ’ del ” l o l ” ) ( print ( h ’ has ” l o l ” ) ) ) Abram Hindle 30
Perl in Scheme: A DSL KW.PM 2007/10
( define (make−easy−n−key−hash) ( define (mk−key keys ) keys ) ( define (get−key y ) (mk−key ( car y ) ) ) ( define (get−arg y ) ( cadr y ) ) ( define (get−arg−no−key y ) ( car y ) ) ( l e t∗ ( ( h (make−hash−table equal ? )) ( acc ( lambda ( x . y ) ( case x ( ’ get (hash−table−ref h (get−key y ) ) ) ( ’ set (hash−table−set! h (get−key y ) (get−arg y ) ) ) ( ’ has ( hash−table−exists? h (get−key y ) ) ) Abram Hindle 31
Perl in Scheme: A DSL KW.PM 2007/10 ( ’ del ( hash−table−delete! h (get−key y ) ) ) ( ’ cons (hash−cons−key h (get−key y ) (get−arg y ) ) ) ( ’ a l i s t (hash−table−
>a l i s t
h ) ) ( ’get−nth−keys ( delete−duplicates ! ( else ( error ( l i s t ”Don ’ t know what t h i s i s : ” x ) ) ) ) ) ) ) acc ) ) Abram Hindle 32
Perl in Scheme: A DSL KW.PM 2007/10
( define (test−easy−n−hash) ( define h (make−easy−n−key−hash) ) ( h ’ set ’ ( ” abram” ” loves ” ) ” l i x i n ” ) ( h ’ set ’ ( ” abram” ” hates ” ) ” work ” ) ( h ’ set ’ ( ” l i x i n ” ” loves ” ) ” abram” ) ( l i s t ( h ’ get ’ ( ” abram” ” loves ” ) ) ( h ’ get ’ ( ” l i x i n ” ” loves ” ) ) ( h ’ get ’ ( ” abram” ” hates ” ) ) ) ) (test−easy−n−hash) Abram Hindle 33
Perl in Scheme: A DSL KW.PM 2007/10
( define ne ( lambda ( x y ) ( not (eq x y ) ) ) ) ( define eq string =?) ( define ne ( lambda ( x y ) ( not (eq x y ) ) ) ) ( define ( subst from to s t r ) ( string−substitute from to s t r ) ) ( define unlink delete−file∗) ( define l i n k file−link ) Abram Hindle 34
Perl in Scheme: A DSL KW.PM 2007/10
(read−lines−from−
< > f i l e l i s t
f ) ( define ( handle−file f i l e ) ( lambda ( filename ) ( with−input−from−file filename ( lambda ( ) (handle−each−line f ) ) ) ) ) ( define ( handle−stdin ) (handle−each−line f ) ) (cond ( ( p a i r ? f i l e l i s t ) (for−each handle−file f i l e l i s t ) ) ( ( or (and ( string? f i l e l i s t ) (eq f i l e l i s t ”
−
” ) ) ( l i s t ? f i l e l i s t ) ) ( handle−stdin ) ) ( ( and ( string? f i l e l i s t ) ) ( handle−file f i l e l i s t ) ) ( else ( error ( l i s t ”What i s ” f i l e l i s t ) ) ) ) ) Abram Hindle 35
Perl in Scheme: A DSL KW.PM 2007/10
– Taking some perl features and reimplementing them – Making functions so it is easy to follow the perl way – Barely scratched the surface – sometimes it is useful to program the perl way
Abram Hindle 36
Perl in Scheme: A DSL KW.PM 2007/10
– Chicken has regular expressions but there are add-ons for good syntax
and making an interface such that perl libraries could
Abram Hindle 37
Perl in Scheme: A DSL KW.PM 2007/10
be called.
Abram Hindle 38
Perl in Scheme: A DSL KW.PM 2007/10
Abram Hindle 39
Perl in Scheme: A DSL KW.PM 2007/10
Abram Hindle 40