Erlang, Day 1: Thoughts
The first Erlang chapter is just a gentle introduction to the language, so I haven't formed much of an impression of it yet. So far, it looks like a dynamically typed functional programming language with Prolog syntax and pattern matching. Of course, I mostly know of Erlang for its concurrency story, so I'm excited to experiment with that in later chapters.
Erlang, Day 1: Problems
Write a function that uses recursion to return the number of words in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(word_count). | |
-export([word_count/1]). | |
word_count([]) -> 0; | |
word_count(Sentence) -> count(Sentence, 1). | |
count([], Count) -> Count; | |
count([32|Tail], Count) -> count(Tail, Count + 1); | |
count([_|Tail], Count) -> count(Tail, Count). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(count_to_ten). | |
-export([count_to_ten/0]). | |
count_to_ten() -> do_count(0). | |
do_count(10) -> 10; | |
do_count(Value) -> do_count(Value + 1). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(error_or_success). | |
-export([print/1]). | |
print(success) -> "success"; | |
print({error, Message}) -> "error: " ++ Message. |
Check out Erlang, Day 2, for more functional programming goodness.