brundonsmith / rust_lisp

A Rust-embeddable Lisp, with support for interop with native Rust functions
234 stars 20 forks source link

`and`, `or` only return correct values with two arguments #19

Closed ghost closed 2 years ago

ghost commented 2 years ago

With rust_lisp 0.15.1, adapting the example from the README:

use std::{cell::RefCell, rc::Rc};

use rust_lisp::default_env;
use rust_lisp::parser::parse;
use rust_lisp::interpreter::eval;

fn main() {

    let expr = "(or Nil Nil T)";
    // create a base environment
    let env = Rc::new(RefCell::new(default_env()));

    // parse into an iterator of syntax trees (one for each root)
    let mut ast_iter = parse(expr);
    let first_expression = ast_iter.next().unwrap().unwrap();

    // evaluate
    let evaluation_result = eval(env.clone(), &first_expression).unwrap();

    // use result
    println!("{}", &evaluation_result);
}

The above, evaluating (or Nil Nil T), prints "F". Similarly, evaluating (and T T Nil) prints "T".

I don't know much about Lisp, and I'm not sure about what the reference implementation should be, but at least in Common Lisp, both and and or support a single argument, while in rust_lisp, this results in an error.

I'd be happy to have a go at generalising and and or and submitting a pull request.

brundonsmith commented 2 years ago

Fixed! Thanks for the report 🙂

The new version has also been published to crates.io

wodin commented 1 year ago

Hi

Other lisps actually also support 0 args for and and or:

Scheme:

> (and)
#t
> (or)
#f
> 

Common Lisp:

* (and)
T
* (or)
NIL
* 

Clojure:

user=> (and)
true
user=> (or)
nil
user=> 

ClojureScript:

(and)
true
(or)
nil
cljs.user=>