Giter Site home page Giter Site logo

jopa-scheme's Introduction

Simple scheme like lang interpreter

How to run:

cargo run -p jopc -- examples/main.scm

You can also run a REPL:

cargo run -p repl

wasm version:

cd www/
./server.sh
open http://localhost:8000/index.html

Built-ins:

+ - / *
> < >= <= =
nil?
list?
length
or and not
if - (if (cond) () ())
do - execute blocks of code, returns last value
cons - (cons el list) - push to front
conj - (cons el list) - append to list
car  - (car list) - get list head
cdr  - (cdr list) - get list tail
concat - (concat "string" 1 (list 1 2 3)) - join elements to string
println
define - define function or variable
list   - construct new list (list 1 2 3)

Some examples

map:

(define (map fn lst)
  (do
    (define (mapaux fn lst acc)
      (if (nil? lst) acc
        (mapaux fn (cdr lst) (conj (fn (car lst)) acc))))
      (mapaux fn lst nil)))


(map (lambda (x) (* x x)) (list 2 3 4 5 6))

FizzBuzz:

(define (fizzbuzzaux n curr acc)
    (if (< curr n)
        (fizzbuzzaux n (+ curr 1)
            (if (= (% curr 15) 0)
                (conj "FizzBuzz" acc)
                (if (= (% curr 3) 0)
                    (conj "Fizz" acc)
                    (if (= (% curr 5) 0)
                        (conj "Buzz" acc)
                        (conj curr acc)))))
        acc))

(define (fizzbuzz n)
    (fizzbuzzaux (+ n 1) 1 nil))

(println (fizzbuzz 100))

Fibonacci:

(define (fib n)
    (if (<= n 2)
        1
        (+ (fib (- n 1))
           (fib (- n 2)))))

(fib 20)

;; faster version

(define (fastfib n)
  (do
    (define (fibaux n acc)
      (if (= (length acc) n)
        acc
        (fibaux
          n
          (cons (+
                  (car acc)
                  (car (cdr acc)))
                acc))))
      (reverse (fibaux n (list 1 0)))))

(fastfib 50)

Filter & reduce:

(define (filter pred coll)
  (do
    (define (filteraux pred coll acc)
      (if (nil? coll)
        acc
        (if (pred (car coll))
          (filteraux pred (cdr coll) (conj (car coll) acc))
          (filteraux pred (cdr coll) acc))))
    (filteraux pred coll nil)))

(define nums (list 1 2 3 4 5 6 7 8 9 10))

;; select even elements
(define even
  (filter
    (lambda (el) (= (% el 2) 0))
    nums))

(println even)

(define (reduce fn coll init)
  (if (nil? coll)
    init
    (reduce fn (cdr coll) (fn (car coll) init))))

;; calculate list product
(reduce (lambda (el acc) (* el acc)) nums 1)

;; hof test
(define (even? el) (= (% el 2) 0))

(filter even? nums)

(define odd? (lambda (el) (not (= (% el 2) 0))))

(filter odd? nums)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.