Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More examples #12

Open
vks opened this issue Feb 24, 2016 · 8 comments
Open

More examples #12

vks opened this issue Feb 24, 2016 · 8 comments

Comments

@vks
Copy link
Contributor

vks commented Feb 24, 2016

I don't know what kind of example you want in the examples directory, but here are a few simple programs:

Tower of Hanoi

(define (move n from to via)
    (cond ((< n 1)
           (panic "expected integer larger than 0"))
          ((= n 1)
           (println "Move disk ~a from ~a to ~a." n from to))
          (else
           (do
            (move (- n 1) from via to)
            (println "Move disk ~a from ~a to ~a." n from to)
            (move (- n 1) via to from)))))

(move 3 "L" "M" "R")

FizzBuzz

(define (fizzbuzz a b)
  (if (<= a b)
    (do
      (let ((divisible-by-3 (= (rem a 3) 0))
            (divisible-by-5 (= (rem a 5) 0)))
        (cond
          ((and divisible-by-3 divisible-by-5)
            (println "FizzBuzz"))
          (divisible-by-3
            (println "Fizz"))
          (divisible-by-5
            (println "Buzz"))
          (else
            (println "~a" a))))
      (fizzbuzz (+ a 1) b))))

(fizzbuzz 1 100)

Extensible FizzBuzz

(use list (map))

(define (is-divisible a b) (= (rem a b) 0))

(define tags '((3 "Fizz") (5 "Buzz")))

(define (evaluate i)
  (let ((find-desc (lambda (tag)
          (if (is-divisible i (first tag)) (last tag) "")))
        (desc (format "~{~a~}" (map find-desc tags))))
    (if (= desc "") (format "~a" i) desc)))

(define (fizzbuzz start end)
  (if (<= start end) (do
    (println "~a" (evaluate start))
    (fizzbuzz (+ 1 start) end))))

(fizzbuzz 1 100)
@murarth
Copy link
Owner

murarth commented Feb 24, 2016

The goal I have in mind for the files in the examples directory is more about demonstrating the Rust API used to manipulate the interpreter than demonstrating the language itself. Some examples that do both would also be desirable.

@silversquirl
Copy link

I'd be happy to write some examples when I get the hang of Ketos a bit more. I find examples are one of the best ways of learning, since trawling through docs can get a bit tiresome, especially with all the features Ketos has.

@zphixon
Copy link

zphixon commented Apr 8, 2017

I'd love to be able to use Ketos but I'm kind of lost right now. More examples would be appreciated.

@murarth
Copy link
Owner

murarth commented Apr 8, 2017

@cheezgi, what issues are you having trying to use Ketos?

@zphixon
Copy link

zphixon commented Apr 9, 2017

@murarth Mostly issues involving exposing my own struct to Ketos. I want to be able to store a vector of instances of my own Rust struct, but I'm not sure how to go about exposing it to Ketos.

@murarth
Copy link
Owner

murarth commented Apr 9, 2017

@cheezgi The ForeignValue trait is used to expose a Rust type to Ketos. examples/interop.rs shows some basic usage of this.

You can create a Ketos Value of any type implementing ForeignValue with the expression Value::Foreign(Rc::new(my_struct)). (Rc is used because you need to share ownership with the Ketos environment and because Ketos values are inherently shared within the interpreter.) So, if you want to store instances of your struct that are shared with Ketos, you could store a Vec<Rc<Foo>>. If you want to copy instances of Foo when passing values to Ketos, a simple Vec<Foo> would suffice.

If there's something specific that's tripping you up, I could help better if I saw a code snippet of what you're trying to do.

@zphixon
Copy link

zphixon commented Apr 9, 2017

@murarth I decided to expose a more opaque API instead, now I have some functions bound to call methods of a struct internally maintaining RefCell<Vec<T>> and share an instance of that with Ketos using Rc. I could probably make it a bit nicer if I used a type alias instead but that was the first solution that came to mind. Thanks for the help, your response cleared things up for me. I'm going to take a better look at the docs when I get a chance.

@wackbyte
Copy link
Contributor

wackbyte commented Jan 2, 2019

Maybe add Ketos examples to a new directory docs/examples?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants