UNPKG

js-slang

Version:

Javascript-based implementations of Source, written in Typescript

6 lines (5 loc) 2.71 kB
export declare const scheme1Prelude = "\n;; the basics\n(define pi math-pi)\n(define e math-e)\n"; export declare const scheme2Prelude = "\n;; the bulk of our stuff goes here\n(define ($filter pred xs acc)\n (cond \n ((null? xs) (reverse acc))\n ((pred (car xs)) ($filter pred (cdr xs) (cons (car xs) acc)))\n (else ($filter pred (cdr xs) acc))))\n\n(define (filter pred xs)\n ($filter pred xs '()))\n\n(define ($map f xs acc)\n (if (null? xs)\n (reverse acc)\n ($map f (cdr xs) (cons (f (car xs)) acc))))\n\n(define (map f xs . xss)\n (if (null? xss)\n ($map f xs '())\n ;; if map is variadic, we use the variadic version\n ;; BUT do note that this may not utilise continuations\n ;; properly!\n (apply r7rs-map f (cons xs xss))))\n\n;; fold is defined as fold-left\n(define ($fold f acc xs)\n (if (null? xs)\n acc\n ($fold f (f acc (car xs)) (cdr xs))))\n\n(define (fold f init xs . xss)\n (if (null? xss)\n ($fold f init xs)\n (apply r7rs-fold f init (cons xs xss))))\n\n(define (fold-left f init xs . xss)\n (if (null? xss)\n ($fold f init xs)\n (apply r7rs-fold-left f init (cons xs xss))))\n\n(define ($fold-right f init xs cont)\n (if (null? xs)\n (cont init)\n ($fold f init (cdr xs) (lambda (acc) (cont (f (car xs) acc))))))\n\n(define (fold-right f init xs . xss)\n (if (null? xss)\n ($fold-right f init xs (lambda (x) x))\n (apply r7rs-fold-right f init (cons xs xss))))\n\n(define (reduce f ridentity xs)\n (if (null? xs)\n ridentity\n ($fold f (car xs) (cdr xs))))\n\n(define (reduce-left f ridentity xs)\n (if (null? xs)\n ridentity\n ($fold f (car xs) (cdr xs))))\n\n(define (reduce-right f ridentity xs)\n (if (null? xs)\n ridentity\n ($fold-right f (car xs) (cdr xs) (lambda (x) x))))\n\n(define ($append xs ys cont)\n (if (null? xs)\n (cont ys)\n ($append (cdr xs) ys (lambda (zs) (cont (cons (car xs) zs))))))\n\n(define (append xs ys . xss)\n (if (null? xss)\n ($append xs ys (lambda (x) x))\n (apply r7rs-append (cons xs (cons ys xss)))))\n"; export declare const scheme3Prelude = "\n;; destructive filter\n(define (filter! pred lst)\n (cond ((null? lst) '())\n ((pred (car lst)) (set-cdr! lst (filter! pred (cdr lst))) lst)\n (else (filter! pred (cdr lst)))))\n\n;; streams are already nicely implemented in the scheme stdlib,\n;; we leave them as is for now\n"; export declare const scheme4Prelude = "\n;; empty for now\n"; export declare const schemeFullPrelude = "\n(define call-with-current-continuation call/cc)\n";