forked from exercism/clojure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add concept introductions * [CI] Format code * Update introduction.md Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
851965c
commit b9eda02
Showing
8 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
In Clojure, binding a value to a name is referred to as a _var_. Top-level (global) vars are similar to constants in other languages, but are commonly redefined to facilitate dynamic development. | ||
|
||
Top-level vars are defined using `def`: | ||
|
||
```clojure | ||
(def fingers 10) | ||
``` | ||
|
||
The `defn` macro can be used to define a function taking zero or more arguments. A function always returns the result of the last expression in its body. | ||
|
||
```clojure | ||
(defn add [x y] | ||
(+ x y)) | ||
``` | ||
|
||
Invoking a function is done by specifying its name and passing arguments for each of the function's parameters. | ||
|
||
```clojure | ||
(def five (add 2 3)) | ||
``` | ||
|
||
Functions and values in Clojure can only be used _after_ they have been defined. Using it before it has been defined results in a compile error. | ||
|
||
```clojure | ||
;; Compile error as `add` has not yet been defined | ||
(def seven (add 3 4)) | ||
|
||
(defn add [x y] | ||
(+ x y)) | ||
``` | ||
|
||
In Clojure, whitespace has no significance other than formatting. | ||
|
||
Clojure functions and vars are organized in namespaces. A namespace groups related functionality and is defined using the `ns` macro: | ||
|
||
```clojure | ||
(ns calculator) | ||
|
||
(def pi 3.14) | ||
|
||
(defn add [x y] | ||
(+ x y)) | ||
``` | ||
|
||
Clojure supports two types of comments. Single line comments are preceded by `;` and the `comment` form is used to prevent evaluation of everything between `(comment` and `)`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
Booleans in Clojure are represented by `true` or `false`. | ||
|
||
Predicate functions (functions which return a boolean) will typically end with a quesion mark (`?`), but this is by convention only. | ||
|
||
The core library includes functions for logical operators such as `not`, `and`, and `or`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
Numbers in Clojure include: | ||
|
||
- Integers: numbers with no digits behind the decimal separator (whole numbers). Examples are `-6`, `0`, `1`, `25`, `976` and `500000`. | ||
- Floating-point numbers: numbers with zero or more digits behind the decimal separator. Examples are `-2.4`, `0.1`, `3.14`, `16.984025` and `1024.0`. | ||
|
||
Two common numeric types are `int` and `float`. An `int` is a 32-bit integer and a `float` is a 64-bit floating-point number. | ||
|
||
Arithmetic is done using the standard arithmetic operators. Numbers can be compared using the standard numeric comparison operators and the equality (`=`) and inequality (`<>`) operators. | ||
|
||
In this exercise you must conditionally execute logic. A common way to do this in Clojure is by using `cond`: | ||
|
||
```clojure | ||
(cond (= x 5) "Expression to evaluate when x equals 5" | ||
(> x 7) "Expression to evaluate when x is greater than 7" | ||
:else "Expression to evaluate in all other cases") | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
A floating-point number is a number with zero or more digits behind the decimal separator. Examples are `-2.4`, `0.1`, `3.14`, `16.984025` and `1024.0`. | ||
|
||
Different floating-point types can store different numbers of digits after the digit separator - this is referred to as its precision. This means that trying to store PI in a `single` will only store the first 6 to 9 digits (with the last digit being rounded). | ||
|
||
Floating point numbers in Clojure are read as Doubles; with M suffix they are read as BigDecimals. | ||
|
||
- `Double`: 8 bytes (~15-17 digits precision). This is the most common type. Written as `2.45`. | ||
- `BigDecimal`: Arbitrary precision integer unscaled value and a 32-bit integer scale. Written as `2.45M`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
In Clojure, Lists are [collections][type-collection], just as like [lists in other languages][type-list]. Similar to other languages in the Lisp family, Clojure uses parentheses to express lists. | ||
|
||
Clojure lists can be created in one of two ways. The `list` function can create a list, or you can `quote` a literal list. | ||
|
||
Lists are special because Clojure will treat them as _calls_. It expects the call to start with an _operator_, which is usually a function. The remaining items of the list are considered _operands_, meaning they become the function's arguments. | ||
|
||
Clojure's special treatment of lists is why we cannot create a list literal directly. Quoting a list using `quote` or its shorthand `'` indicates that the list should not be evaluated. | ||
|
||
Unlike some modern languages, Clojure lists are _heterogenous_, meaning they can contain multiple types of item internally. E.g. `'(2 "a" "b" 3)` | ||
Unlike other other Lisps, an empty list in Clojure in truthy and is not equivalent to `nil` or `false`. | ||
|
||
[type-list]: ../../../../reference/types/list.md | ||
[type-collection]: ../../../../reference/types/collection.md |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
Numbers in Clojure include: | ||
|
||
- Integers: numbers with no digits behind the decimal separator (whole numbers). Examples are `-6`, `0`, `1`, `25`, `976` and `500000`. | ||
- Floating-point numbers: numbers with zero or more digits behind the decimal separator. Examples are `-2.4`, `0.1`, `3.14`, `16.984025` and `1024.0`. | ||
|
||
Two common numeric types are `int` and `float`. An `int` is a 32-bit integer and a `float` is a 64-bit floating-point number. | ||
|
||
Arithmetic is done using the standard arithmetic operators. Numbers can be compared using the standard numeric comparison operators and the equality (`=`) and inequality (`<>`) operators. | ||
|
||
In this exercise you must conditionally execute logic. A common way to do this in Clojure is by using `cond`: | ||
|
||
```clojure | ||
(cond (= x 5) "Expression to evaluate when x equals 5" | ||
(> x 7) "Expression to evaluate when x is greater than 7" | ||
:else "Expression to evaluate in all other cases") | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
A `string` in Clojure is a Java string, which is an object that represents immutable text as a sequence of Unicode characters (letters, digits, punctuation, etc.) and is defined as follows: | ||
|
||
```clojure | ||
(def fruit "Apple") | ||
``` | ||
|
||
The `clojure.string` library provides many standard string manipulation and processing functions. In addition, all the usual Java methods for operating on strings are available via interop. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction | ||
|
||
A `vector` in Clojure is a sequential, indexed, immutable collection of zero or more values. This means that once a vector has been created, it cannot be modified. Functions for operating on vectors will return a new vector, while the original vector remains unchanged. The values in a vector may be of heterogenous types. Vectors can be defined as follows: | ||
|
||
```clojure | ||
(def empty []) | ||
(def single-value 5) | ||
(def single-value-alternative (vector 5)) | ||
(def three-values [a b c] | ||
``` | ||
|
||
Elements can be retrieved from a vector using an index. Clojure vectors are zero-based, meaning that the first element's index is always zero: | ||
|
||
```clojure | ||
(def numbers [2 3 5]) | ||
;; Read value from vector | ||
(get numbers 2) | ||
;;=> 5 | ||
;; Update value in vector | ||
(assoc numbers 2 9) | ||
;;=> [2 3 9] | ||
``` | ||
|
||
The original vector is unchanged: | ||
|
||
```clojure | ||
numbers | ||
;;=> [2 3 5] | ||
``` | ||
|
||
To remember the updated value, we need to pass it along or capture it in a var: | ||
|
||
```clojure | ||
(def updated-numbers | ||
(assoc numbers 2 9)) | ||
(get updated-numbers 2) | ||
;;=> 9 | ||
``` |