Understanding intern better

35 views
Skip to first unread message

Joel Shellman

unread,
May 31, 2012, 11:02:27 PM5/31/12
to qil...@googlegroups.com
Looking at these two examples:
(let X 5 (+ (intern "X") 2))

(defun test (X Y) (+ X (intern Y)))
(test 2 "X")

In shen-clojure, I get: "clojure.lang.Symbol cannot be cast to java.lang.Number"

However, this:
((intern "cn") "a" "b")

evaluates to "ab".

I realize that the first item in an expression is something to be
applied and so the interned symbol was applied in this second example.
But is that the only case? Will an (intern "something") ever be
evaluated to something the symbol is associated with instead of just
as a symbol in other cases? I just want to make sure I get it.

Dmitry Cherkassov

unread,
Jun 1, 2012, 6:18:27 AM6/1/12
to Qil...@googlegroups.com

I.e:

(3-) ((intern (cn "c" "n")) "a" "b")
"ab"

(4-) ((intern (cn "c" "xn")) "a" "b")
APPLY: cxn function is not defined

--
With best regards,
Dmitry

Dmitry Cherkassov

unread,
Jun 1, 2012, 5:58:19 AM6/1/12
to Qil...@googlegroups.com
Joel Shellman <jo...@mentics.com> writes:

> I realize that the first item in an expression is something to be
> applied and so the interned symbol was applied in this second example.
> But is that the only case? Will an (intern "something") ever be
> evaluated to something the symbol is associated with instead of just
> as a symbol in other cases? I just want to make sure I get it.

I think that in (F x) F will be repeatedly evaled until it yields primitive
function or function symbol bound with some function or to error if
there is no such binding.

Right?

Joel Shellman

unread,
Jun 8, 2012, 3:20:35 AM6/8/12
to qil...@googlegroups.com
Thank you for your very helpful response.

On Fri, Jun 1, 2012 at 8:02 AM, Dominik G <dgr...@googlemail.com> wrote:
> In your second example (intern Y) evaluates to the _global_ symbol X.
> This is different from the X used in the function.

This I think gets to the heart of the matter. So... my question then
is why does it evaluate to the global symbol instead of the local one?
Is this deliberate, is it based on some underlying principle?

If one takes too literally: "In Shen all symbols evaluate to
themselves, except they are in the first place of an s-expr." Then:

> (defun test (X Y) (+ X Y))

would be a type error because it's trying to add two symbols. So... in
this case, the X and Y in (+ X Y) are not evaluating to themselves,
they are being replaced by the values in the parameter list. Is it
because they are not "symbols" (though they are referred to as symbols
in the documentation), they are "parameter names"?

Joel Shellman

unread,
Jun 8, 2012, 1:37:32 PM6/8/12
to qil...@googlegroups.com
On Fri, Jun 8, 2012 at 8:24 AM, Joe Danieli <not.d...@gmail.com> wrote:
> On Friday, June 8, 2012 3:20:35 AM UTC-4, Joel Shellman wrote:
>>  Is it
>> because they are not "symbols" (though they are referred to as symbols
>> in the documentation), they are "parameter names"?
>
> That's correct. All symbols starting with an uppercase letter care
> considered variables.
>
> From the Shen specification (4.0): "As in Qi, a symbol is a variable if it
> begins in an uppercase letter."

But (intern "X") is an upper case symbol but apparently is not a
variable. So the variables are only bound at compile time?

But this:
(1-) (defun myadd (x y) (+ x y))
(2-) (myadd 5 4)
9
compiles and outputs as expected (at least on shen-clojure). But x and
y aren't upper case.

By the way, I should have stated at the top, my questions in this
thread were generally in regards to kl and not to Shen since I'm
working on a port.

Mark Tarver

unread,
Jun 9, 2012, 8:54:53 AM6/9/12
to Qilang
I'm hacking my way through a recording at the moment and finding my
Audacity recorder does not interface to my headset with anything like
the same quality that I got under XP. I might have to blow the dust
off my old desktop.  Sigh. However ....

Good that people are pitching in here.  Some comments.

* The variable/non-variable distinction really only applies to Shen
and not KLambda.  Lacking pattern-matching, KLambda really has no use
for the distinction and a variable is marked by either being in a
parameter or by being lambda or let-bound. Any case is acceptable.
* Applying an interned symbol to an argument will not work if the
symbol is not associated with a regular function; e.g. ((intern "let")
X 5 (* X X)) fails.
* An interned symbol will not be bound (or cannot be relied to be
bound) even if it lies in the scope of a binding operation. (let X 5
(+ (intern "X") 2)) will fail. You are trying add the symbol 'X' to
2.
* However wrapping 'value' does work: (let X 5 (+ (value (intern "X"))
2)) asks Shen to create a symbol 'X' and add the global value
associated with that symbol to 2.

Mark
Reply all
Reply to author
Forward
0 new messages