erlang语言基础知识(数值类型\变量)



 erlang 语言基础知识

1.1 数值类型

Eshell V7.1  (abort with ^G)

 

1> 2+1.

2> 4*5. 

20

3> 12-123.

-111

4> 9 /2 .     %而对就除数,Erlang并不关心输入的是浮点数,还是整数

4.5

5> 9 /2.0 .   %

4.5

6> 9 div 2.0 .   %整除,整除不能用浮点数,否则会报下面的错误

** exception error: an error occurred when evaluating an arithmetic expression

     in operator  div/2

        called as 9 div 2.0

7> 9 div 2 .  

4

8> 9 rem 2.     %获取整数除法的余数(模),可以使用rem(remainder,余数)。

1

 

9> 

 

 

 

 remainder英[rɪˈmeɪndə(r)]

 

美[rɪˈmendɚ]

 

 

如果想以其他不为10的基数表示整数,只需要以Base#Value这种形式输入数值即可,Base必须在2到36之间  像下面这样

 

15> 2#01010.

10

16> 2#0102.    %0102中的2超出了二进制的表示范围

* 1: syntax error before: 2

16> 3#0102.

11

17> 16#abc.

2748

18> 36#abc.

13368

19> 

在上面的例子中,我们把二进制,3 进制,16进制,36进制数转换成十进制数。

 

1.2  (琦)怪变量,不可改变的变量(一经赋值,不可改变)

  能做算术运算固然不错,但是如果不能保存计算结果的话,也没多大用处,但在函数式编程中,变量是不能变化的。

      在Erlang中,变量必须以大写字符开始。

24> A=1.     

1

25> A1=A2=A=1.

1

26> B=A+A.  %B=2

27> B=2.    %可以”假装“在赋值一次,只要这个值和变量已有的值完全一样,如果不一样,就会报错

2

28> B=3.  %一个变量只能被赋值一次,再次赋值,会报以下的错误,实际上就是模式匹配不一致引起的错误

** exception error: no match of right hand side value 3

29> B=B+1.

** exception error: no match of right hand side value 3

30>

%这个错误是=操作符有关,负责比较,并且在不相等是报错

30> 9=5+4.

9

 

31> 9=5+6.

** exception error: no match of right hand side value 11

 

32> 

 

 

 变量可以以下划线(_)起始,但是在Erlang中,这种变量按照惯例,仅用于不想关心其值的情况。

注意  _VarName这种特殊语法代表一个常规变量(Normal variable),而不是一个匿名变量,一般来说,当某个变量在子句中只使用了一次时,编译器会生成一个警告。但如果这个只用了一次的变量以下划线开头,就不会有错误消息。 

 

只有下划线(_),用在模式匹配时,则是把这个位置的数值,丢弃。

 

  注意》在shell中做测试时,如果不小心给某个变量赋错了值,可以通过f(Variable).函数把这个变量“删除”。f()则是清除所有变量。这两个函数是专门用在shell中使用的,在真实程序中,是不能使用的。

 

 

 

今年是大年初一,准备好好学习一下Erlang的相关知识。如果上面的内容对您有帮助,欢迎打赏一下,下面是我的二维码,请微信扫一扫,因为IT都是diao si ,多少不限,有心鼓励即可,谢谢!




 
 

 

37> help().

** shell internal commands **

b()        -- display all variable bindings

e(N)       -- repeat the expression in query <N>

f()        -- forget all variable bindings

f(X)       -- forget the binding of variable X

h()        -- history

history(N) -- set how many previous commands to keep

results(N) -- set how many previous command results to keep

catch_exception(B) -- how exceptions are handled

v(N)       -- use the value of query <N>

rd(R,D)    -- define a record

rf()       -- remove all record information

rf(R)      -- remove record information about R

rl()       -- display all record information

rl(R)      -- display record information about R

rp(Term)   -- display Term using the shell's record information

rr(File)   -- read record information from File (wildcards allowed)

rr(F,R)    -- read selected record information from file(s)

rr(F,R,O)  -- read selected record information with options

** commands in module c **

bt(Pid)    -- stack backtrace for a process

c(File)    -- compile and load code in <File>

cd(Dir)    -- change working directory

flush()    -- flush any messages sent to the shell

help()     -- help info

i()        -- information about the system

ni()       -- information about the networked system

i(X,Y,Z)   -- information about pid <X,Y,Z>

l(Module)  -- load or reload module

lc([File]) -- compile a list of Erlang modules

ls()       -- list files in the current directory

ls(Dir)    -- list files in directory <Dir>

m()        -- which modules are loaded

m(Mod)     -- information about module <Mod>

memory()   -- memory allocation information

memory(T)  -- memory allocation information of type <T>

nc(File)   -- compile and load code in <File> on all nodes

nl(Module) -- load module on all nodes

pid(X,Y,Z) -- convert X,Y,Z to a Pid

pwd()      -- print working directory

q()        -- quit - shorthand for init:stop()

regs()     -- information about registered processes

nregs()    -- information about all registered processes

uptime()   -- print node uptime

xm(M)      -- cross reference check a module

y(File)    -- generate a Yecc parser

** commands in module i (interpreter interface) **

ih()       -- print help for the i module

true

 

38> 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值