পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Tuesday, January 31, 2017

Erlang - Funs

Funs are used to define anonymous functions in Erlang. The general syntax of an anonymous function is given below

Syntax

F = fun (Arg1, Arg2, ... ArgN) ->
      ...
   End
Where

  • F − This is the variable name assigned to the anonymous function.
  • Arg1, Arg2, ... ArgN − These are the arguments which are passed to the anonymous function.
The following example showcases how the anonymous function can be used.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   A = fun() -> io:fwrite("Hello") end, 
   A().
The following things need to be noted about the above program.
  • The anonymous function is assigned to the variable A.
  • The anonymous function via the variable A().
When we run the above program we will get the following result.
“Hello”
Another example of anonymous function is as follows, but this is with the use of parameters.
-module(helloworld). 
-export([start/0]). 

start() -> 
   A = fun(X) -> 
      io:fwrite("~p~n",[X]) 
      end, 
   A(5).
When we run the above program we will get the following result.

Output

5

Using Variables

The Anonymous function have the ability to access the variables which are outside of the scope of the anonymous function. Let’s look at an example of this −

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   B = 6, 
   A = fun(X) -> 
      io:fwrite("~p~n",[X]), 
      io:fwrite("~p~n",[B]) 
      end, 
   A(5).
The following things need to be noted about the above program.
  • The variable B is outside of the scope of the anonymous function.
  • The anonymous function can still access the variable defined in the global scope.
When we run the above program we will get the following result.

Output

5

6

Functions within Functions

One of the other most powerful aspects of higher order functions, is that you can define a function within a function. Let’s see an example of how we can achieve this.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   Adder = fun(X) -> fun(Y) -> io:fwrite("~p~n",[X + Y]) end end, 
   A = Adder(6), 
   A(10).
The following things need to be noted about the above program.
  • Adder is a higher order function defined as fun(X).
  • The Adder function fun(X) has a reference to another function fun(Y).
When we run the above program we will get the following result.

Output

16

No comments:

Post a Comment