如何在Clojure中将字符转换为int? [英] How to cast a character to int in Clojure?

查看:511
本文介绍了如何在Clojure中将字符转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Clojure中将字符转换为int?



我试图在clojure中写一个烂的13,所以我需要有东西把我的char int。我发现了一个叫(int)的东西,所以我把它放在:

 (int a)



Get:CompilerException java.lang.RuntimeException:无法解析符号:a在此上下文中,编译:( NO_SOURCE_PATH:13:1)



然后我输入:

 (int'a)

Get:ClassCastException clojure.lang.Symbol不能转换为`java.lang.Character clojure.lang.RT.intCast RT.java:1087)



然后:

  'a')

Get:ClassCastException clojure.lang.PersistentList不能转换为java.lang.Character clojure.lang.RT.intCast(RT.java:1087)



和:

 code>(rot13a)

Get:

  ClassCastException java.lang.String不能转换为java.lang.Character clojure.lang.RT.intCast(RT.java:1087)

那么什么是正确的方法呢?



btw ,我总是困惑与所有这些clojure语法。但我永远找不到任何来源只帮助我的问题。有什么建议么?谢谢!

解决方案

您正在寻找的是字符文字 \a 。字符文字由单个字符或16位Unicode代码点表示,前缀为 \ 读取器宏。

 (int \a);; => 97 

(int \0);; => 48

(int \\\0);; => 48



使用(int a) a 是一个符号。





使用(int' a) a 也是一个符号,但是因为你使用单引号('),运行时把它从字面上和尝试和失败,将 clojure.lang.Symbol 转换为 java.lang。字符





使用(rot13''a')'a'声明 a'作为符号。但是,额外的'前缀它使运行时处理声明 a'字面上的表达式。 'a'扩展为(引用a'),因此文字文字 ''a',扩展到列表(quote a')

 ''a';; => (quote a')

(second''a');; => a'



使用(rot13a ) a 是一个字符串。字符串不能转换为字符,但它们可以被视为字符集合。因此,(rot13(firsta))将按预期工作。


How to cast a character to int in Clojure?

I am trying to write a rot 13 in clojure, so I need to have something to cast my char to int. I found something called (int), so I put:

(int a)

Get: CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(NO_SOURCE_PATH:13:1)

Then I put:

(int 'a)

Get: ClassCastException clojure.lang.Symbol cannot be cast to `java.lang.Character clojure.lang.RT.intCast (RT.java:1087)

Then:

(rot13 ''a')

Get: ClassCastException clojure.lang.PersistentList cannot be cast to java.lang.Character clojure.lang.RT.intCast (RT.java:1087)

And:

(rot13 "a")

Get:

ClassCastException java.lang.String cannot be cast to java.lang.Character  clojure.lang.RT.intCast (RT.java:1087)

So what is the right way to do it?

btw, I always get confused with all these clojure syntax. But I can never find any source only help me with my problem. Any suggestions? Thank you!!

解决方案

What you are looking for is a character literal \a. A character literal is denoted by a single character, or 16-bit unicode code point, prefixed by the \ reader macro.

(int \a) ;; => 97

(int \0) ;; => 48

(int \u0030) ;; => 48


With (int a), a is a symbol. As such, the runtime tried and failed to resolve what that symbol was bound to.


With (int 'a), a is also a symbol, but, because you declared it a symbol with the single quote ('), the runtime took it literally and tried and faild to cast the clojure.lang.Symbol to a java.lang.Character.


With (rot13 ''a'), 'a' declares a' as a symbol. But, the extra ' prefixing it makes the runtime treat the expression that declared the a' literally. 'a' expands to (quote a'), so the "literal literal", ''a', expands to the list (quote a').

''a' ;; => (quote a')

(second ''a') ;; => a'


With (rot13 "a"), a is a string. Strings cannot be cast to characters, but they can be treated as collections of characters. So, (rot13 (first "a")) would work as intended.

这篇关于如何在Clojure中将字符转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆