
Memory Upgrades
Erlang is a functional programming language. Among other things this means that funs can be used as the arguments to functions and that functions (or funs) can return funs. Functions that return funs, or functions that can accept funs as their arguments, are called higher-order functions. We’ll see a few examples of these in the next sections.
Now all of this might not sound very exciting since we haven’t seen what we can do with funs. So far, the code in a fun looks just like
regular function code in a module, but nothing could be further from the truth. Higher-order functions are the very essence of functional programming languages—they breathe fire into the belly of the code.
Once you’ve learned to use them, you’ll love them. We’ll see a lot more of them in the future.
Functions That Return Funs
Not only can funs be used as arguments to functions (such as map and
filter), but functions can also return funs.
Here’s an example—suppose I have a list of something, say fruit:
1> Fruit = [apple,pear,orange].
[apple,pear,orange]
Now I can define a function MakeTest(L) that turns a list of things (L) into
a test function that checks whether its argument is in the list L:
2> MakeTest = fun(L) -> (fun(X) -> lists:member(X, L) end) end.
#Fun<erl_eval.6.56006484>
3> IsFruit = MakeTest(Fruit).
#Fun<erl_eval.6.56006484>
lists:member(X, L) returns true if X is a member of the list L; otherwise, it
returns false. Now that we have built a test function, we can try it:
4> IsFruit(pear).
true
5> IsFruit(apple).
true
6> IsFruit(dog).
false
We can also use it as an argument to lists:filter/2:
7> lists:filter(IsFruit, [dog,orange,cat,apple,bear]).
[orange,apple]
The notation for funs that return funs takes a little getting used to,
so let’s dissect the notation to make what’s going on a little clearer. A
function that returns a “normal” value looks like this:
1> Double = fun(X) -> ( 2 * X ) end.
#Fun<erl_eval.6.56006484>
2> Double(5).
10
Erlang is a functional programming language. Among other things this means that funs can be used as the arguments to functions and that functions (or funs) can return funs. Functions that return funs, or functions that can accept funs as their arguments, are called higher-order functions. We’ll see a few examples of these in the next sections.
Now all of this might not sound very exciting since we haven’t seen what we can do with funs. So far, the code in a fun looks just like
regular function code in a module, but nothing could be further from the truth. Higher-order functions are the very essence of functional programming languages—they breathe fire into the belly of the code.
Once you’ve learned to use them, you’ll love them. We’ll see a lot more of them in the future.
Now all of this might not sound very exciting since we haven’t seen what we can do with funs. So far, the code in a fun looks just like
regular function code in a module, but nothing could be further from the truth. Higher-order functions are the very essence of functional programming languages—they breathe fire into the belly of the code.
Once you’ve learned to use them, you’ll love them. We’ll see a lot more of them in the future.
Functions That Return Funs
Not only can funs be used as arguments to functions (such as map and
filter), but functions can also return funs.
Here’s an example—suppose I have a list of something, say fruit:
1> Fruit = [apple,pear,orange].
[apple,pear,orange]
Now I can define a function MakeTest(L) that turns a list of things (L) into
a test function that checks whether its argument is in the list L:
2> MakeTest = fun(L) -> (fun(X) -> lists:member(X, L) end) end.
#Fun<erl_eval.6.56006484>
3> IsFruit = MakeTest(Fruit).
#Fun<erl_eval.6.56006484>
lists:member(X, L) returns true if X is a member of the list L; otherwise, it
returns false. Now that we have built a test function, we can try it:
4> IsFruit(pear).
true
5> IsFruit(apple).
true
6> IsFruit(dog).
false
We can also use it as an argument to lists:filter/2:
7> lists:filter(IsFruit, [dog,orange,cat,apple,bear]).
[orange,apple]
The notation for funs that return funs takes a little getting used to,
so let’s dissect the notation to make what’s going on a little clearer. A
function that returns a “normal” value looks like this:
1> Double = fun(X) -> ( 2 * X ) end.
#Fun<erl_eval.6.56006484>
2> Double(5).
10
Not only can funs be used as arguments to functions (such as map and
filter), but functions can also return funs.
Here’s an example—suppose I have a list of something, say fruit:
1> Fruit = [apple,pear,orange].
[apple,pear,orange]
Now I can define a function MakeTest(L) that turns a list of things (L) into
a test function that checks whether its argument is in the list L:
2> MakeTest = fun(L) -> (fun(X) -> lists:member(X, L) end) end.
#Fun<erl_eval.6.56006484>
3> IsFruit = MakeTest(Fruit).
#Fun<erl_eval.6.56006484>
lists:member(X, L) returns true if X is a member of the list L; otherwise, it
returns false. Now that we have built a test function, we can try it:
4> IsFruit(pear).
true
5> IsFruit(apple).
true
6> IsFruit(dog).
false
We can also use it as an argument to lists:filter/2:
7> lists:filter(IsFruit, [dog,orange,cat,apple,bear]).
[orange,apple]
The notation for funs that return funs takes a little getting used to,
so let’s dissect the notation to make what’s going on a little clearer. A
function that returns a “normal” value looks like this:
1> Double = fun(X) -> ( 2 * X ) end.
#Fun<erl_eval.6.56006484>
2> Double(5).
10
Our Services
- Network LAN
- Hubs And Switches
- PABX and Installation
- Simple Network Management Protocol(SNMP)
- Router Configuration
- Virtual Private Networking(VPN)
- Firewalls
- Technical Support
- Hardware Upgrades
- Motherboard Replacement
- Memory Upgrades
- CCTV Installation
- Software Installation
- Software Upgrading
- Troubleshooting
- Voice Over Internet Protocol
- Website Development
- Photocopying Printing
- Data Recovery