-
Notifications
You must be signed in to change notification settings - Fork 3
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
Return type of the object #46
Comments
A similar function is in #42 but that is more to match my implementation. |
Sure, why not. It's a useful procedure. Since we divide the cookbook into sections, it should be quite easy to navigate even with lots of pages. R7RS section 3.2. "Disjointness of types" lists all the standard predicates:
As you noticed, Scheme has no standard |
@lassik in Guile and Chicken there are no bytevector? is there a way to make the function works if the function is defined and not? Here is the code now: (define (type obj)
(cond ((number? obj) "number")
((pair? obj) "pair")
((null? obj) "nil")
((string? obj) "string")
((symbol? obj) "symbol")
((vector? obj) "vector")
((procedure? obj) "procedure")
((port? obj)
(cond ((input-port? obj) "input-port")
((output-port? obj) "output-port")
(else "unknown-port")))
((eof-object? obj) "eof")
((char? obj) "character")
((boolean? obj) "boolean"))) |
|
Unfortunatly seem like it is not in the document. As running
In the search result, there is not the path like |
In Chicken, using the SRFI 4 uses the term |
We can include an alternative recipe with |
@jcubic It is no needed, what we are talking about is that how chicken implementation bring the symbol |
Thanks.@lassik |
Yes, |
In R7RS, and many non-R7RS Scheme implementations as well, that can be done with (import (scheme base) (scheme write))
(cond-expand
(r7rs
(define type-of-alist-extra (list (cons bytevector? 'bytevector))))
(else
(define type-of-alist-extra '())))
(define type-of-alist
(append type-of-alist-extra
(list (cons boolean? 'boolean)
(cons char? 'character)
(cons eof-object? 'eof-object)
(cons null? 'null)
(cons number? 'number)
(cons pair? 'pair)
(cons port? 'port)
(cons procedure? 'procedure)
(cons string? 'string)
(cons symbol? 'symbol)
(cons vector? 'vector))))
(define (type-of obj)
(let loop ((alist type-of-alist))
(and (not (null? alist))
(if ((caar alist) obj) (cdar alist) (loop (cdr alist))))))
(define (writeln x) (write x) (newline))
(writeln (type-of 123))
(writeln (type-of "abc"))
(writeln (type-of #\x))
(writeln (type-of (lambda (x) x)))
(writeln (type-of 'abc))
(writeln (type-of #(1 2 3)))
(writeln (type-of '(1 2 3)))
(writeln (type-of '()))
(writeln (type-of (eof-object)))
(writeln (type-of (current-input-port)))
(writeln (type-of (bytevector 1 2 3))) |
That is much better because you can extend the types easily, I wanted to create a dictionary with types but forget about Alist. I do too much JavaScript recently. |
@lassik As we talked in the above, the standard way to run the top level script in Chicken is that |
Sorry. |
I have this function in my interpreter, but it does a lot more since it also returns types for JavaScript objects and also internal implementation objects.
Here is something for R7RS scheme:
Unfortunately, there is no way to test if an object is a record type.
@lassik Should we include functions like this or is it too simple? I think it would be nice to have this function so you don't need to write it yourself and can just copy/paste.
The text was updated successfully, but these errors were encountered: