ketos icon indicating copy to clipboard operation
ketos copied to clipboard

More examples

Open vks opened this issue 8 years ago • 8 comments

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)

vks avatar Feb 24 '16 12:02 vks

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.

murarth avatar Feb 24 '16 23:02 murarth

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.

silversquirl avatar Oct 25 '16 08:10 silversquirl

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

zphixon avatar Apr 08 '17 01:04 zphixon

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

murarth avatar Apr 08 '17 04:04 murarth

@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.

zphixon avatar Apr 09 '17 16:04 zphixon

@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.

murarth avatar Apr 09 '17 18:04 murarth

@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.

zphixon avatar Apr 09 '17 21:04 zphixon

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

wackbyte avatar Jan 02 '19 21:01 wackbyte