erlang 自定义函数的初步应用

简介: 一、模块内调用 1> AA=fun(BB)-> io:format("this is test args ~s~n",[BB]) end.#Fun2> AA(aa).this is test argsaaok 3> BB=fun()-> io:format("this is BB FUN ~n",[]) end.

一、模块内调用

1> AA=fun(BB)-> io:format("this is test args ~s~n",[BB]) end.
#Fun<erl_eval.6.17052888>
2> AA(aa).
this is test argsaa
ok

3> BB=fun()-> io:format("this is BB FUN ~n",[]) end.      
#Fun<erl_eval.20.17052888>
4> BB().
this is BB FUN
ok

5> spawn( BB).  
this is BB FUN
<0.62.0>

6> spawn(fun()-> AA(cc) end).
this is test argscc
<0.67.0>

7> Fun =fun(A,B)-> io:format("the product is:~w~n",[A*B]) end.
#Fun<erl_eval.12.80484245>
8> Fun2 =fun(Fun,A,B) ->Fun(A,B) end.
#Fun<erl_eval.18.80484245>
9> Fun2(Fun,2,4).
the product is:8
ok

 

 

二、跨模块调用

-module(mod_user).
-export([test/0]).
test()->
    io:format("hello test ~n").

 

1> mod_user:test().
hello test
ok
2> spawn(fun mod_user:test/0).
hello test
<0.35.0>
3> spawn(mod_user,test,[]).
hello test
<0.37.0>


备注:2 只能用于空参数的函数,否则只能用3代替。

 

三、需要写到文件中

  test(A,B)-> io:format("the product is:~w~n",[A*B]) .
  test_fun(Fun,A,B)-> Fun(A,B).
  test_import()-> test_fun(fun test/2,2,3).

 需要导出test_import/0,然后在控制台调用module:test_import().

相关文章
|
3月前
|
算法 Python
【Python基础】- 自定义函数和匿名函数
【Python基础】- 自定义函数和匿名函数
41 0
|
7月前
|
安全 Go
Golang 语言中的内置函数 make 和 new
Golang 语言中的内置函数 make 和 new
17 0
|
12月前
|
存储 Rust 测试技术
用Python绑定调用C/C++/Rust库
Python绑定可以让Python程序调用C/C++/Rust编译的库函数,从而让我们在不重复造轮子的前提下,兼具Python和C/C++二者的优点。
211 0
用Python绑定调用C/C++/Rust库
|
Python
【Python】自定义函数、lambda函数与内置函数
【Python】自定义函数、lambda函数与内置函数
156 0
【Python】自定义函数、lambda函数与内置函数
|
索引 Python
Python 编程 | 连载 23 - 常用函数与高阶函数
Python 编程 | 连载 23 - 常用函数与高阶函数
Python 编程 | 连载 23 - 常用函数与高阶函数
|
计算机视觉 Python
Python入门(六):调用自定义函数
python中内置了很多功能强的函数,能够满足很多时候的开发工作,但有些时候需要自己定义一些函数,来供在其他的地方调用,有些时候需要调用第三方库里面的很多函数,就需要先import第三方库,然后调用里面的方法即可。比如需要引用opencv以及里面的一些函数,可以通过以下方法:
432 0
Python入门(六):调用自定义函数
|
C++ Python
C++调用Python自定义函数
C++调用Python自定义函数
186 0
|
Java Python 分布式计算
python:函数的高级特性
很多语言中,都允许把函数本身做为参数,传递给其它参数:即所谓的高阶函数。python中也有类似特性: 一、map/reduce、filter、sorted hadoop里的map-reduce思想在python里已经变成内置函数了。
1250 0