পৃষ্ঠাসমূহ

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 - Loops

Erlang is a functional programming language and what needs to be remembered about all functional programming languages is that they don’t offer any constructs for loops. Instead, functional programming depends on a concept called recursion.

while Statement Implementation

Since there is no direct while statement available in Erlang, one has to use the recursion techniques available in Erlang to carry out a while statement implementation.
We will try to follow the same implementation of the while loop as is followed in other programming languages. Following is the general flow which will be followed.
while Statement Implementation Let’s look at an example of how we can use recursion to implement the while loop in Erlang.

Example

-module(helloworld). 
-export([while/1,while/2, start/0]). 

while(L) -> while(L,0). 
while([], Acc) -> Acc;

while([_|T], Acc) ->
   io:fwrite("~w~n",[Acc]), 
   while(T,Acc+1). 
   
   start() -> 
   X = [1,2,3,4], 
   while(X).
The following key points need to be noted about the above program −
  • Define a recursive function called while which would simulate the implementation of our while loop.
  • Input a list of values defined in the variable X to our while function as an example.
  • The while function takes each list value and stores the intermediate value in the variable ‘Acc’.
  • The while loop is then called recursively for each value in the list.
The output of the above code will be −

Output

0
1
2
3

for Statement

Since there is no direct for statement available in Erlang, one has to use the recursion techniques available in Erlang to carry out a for statement implementation.
We will try to follow the same implementation of the for loop as is followed in other programming languages. Following is the general flow which should be adhered to.
for Statement Let’s look at an example of how we can use recursion to implement the for loop in Erlang.

Example

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

for(0,_) -> 
   []; 
   
   for(N,Term) when N > 0 -> 
   io:fwrite("Hello~n"), 
   [Term|for(N-1,Term)]. 
   
start() -> 
   for(5,1).
The following key points need to be noted about the above program −
  • We are defining a recursive function which would simulate the implementation of our for loop.
  • We are using a guard within the ‘for’ function to ensure that the value of N or the limit is a positive value.
  • We recursively call the for function, by reducing the value of N at each recursion.
The output of the above code will be −

Output

Hello
Hello
Hello
Hello
Hello

No comments:

Post a Comment