Editing
Elixir Syntax
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Introduction == Elixir is a dynamic, functional programming language built on top of the Erlang virtual machine (BEAM). It provides a concise and expressive syntax that allows developers to write clean and maintainable code. This article aims to provide an overview of the Elixir syntax, covering its key features and conventions. == Data types == === Atoms === An atom is a constant whose value is its own name. It is used to represent static values, such as status codes or names. Atoms are declared by writing a lowercase word or a word enclosed in single quotes: Example: :ok :hello :'Hello, World!' === Numbers === Elixir supports both integers and floating-point numbers. Integers can be written in decimal, hexadecimal, octal, and binary representations. Floating-point numbers can be written using the decimal or the scientific notation: Example: 10 0xFF 0o777 0b1010 3.14 12.5e-3 === Strings === Strings in Elixir are delimited by double quotes. They can contain escape sequences and interpolations: Example: "Hello, World!" "Today is #{Date.utc_today()}." === Tuples === A tuple is an ordered collection of elements, enclosed in curly braces. Tuples can hold any combination of data types: Example: {:ok, 42} {"John", 25} === Lists === A list is a dynamic collection of elements, enclosed in square brackets. Lists can hold any combination of data types and are primarily used for pattern matching and recursion: Example: [1, 2, 3] [true, "Hello", :ok] === Maps === A map is a collection of key-value pairs. Keys can be any data type, and values can be any Erlang term. Maps are defined using a literal syntax or the %{} syntax: Example: %{name: "John", age: 25} %{:ok => "Success", :error => "Failure"} === Anonymous Functions === Anonymous functions, also known as lambda functions, allow you to create function literals. They are declared using the fn keyword followed by argument names and the function body: Example: fn x -> x * 2 end fn a, b -> a + b end == Control flow == === Pattern Matching === Pattern matching is a powerful feature in Elixir that allows for complex data manipulation and conditional branching. It is used extensively in function clauses and case statements: Example: {status, message} = {:ok, "Success"} case response do {:ok, result} -> IO.puts("Result: #{result}") {:error, reason} -> IO.puts("Error: #{reason}") end === Conditional Expressions === Elixir provides if, unless, and case expressions for control flow based on conditions: Example: if condition do # do something else # do something else end unless condition do # do something else # do something else end case value do pattern1 -> # do something pattern2 -> # do something else _ -> # default case end == Conclusion == This article provided an introduction to the Elixir syntax, covering the key data types, control flow constructs, and conventions. Understanding the Elixir syntax is essential for writing clean, efficient, and maintainable code in the Elixir programming language. For more in-depth information, refer to the articles on specific topics available on this wiki.
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information