You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/examples/coconut-lang.md
+19-7Lines changed: 19 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,24 +11,34 @@ kernelspec:
11
11
name: coconut
12
12
---
13
13
14
-
# Notebooks in other languages
14
+
# Other Programming Languages
15
15
16
-
<http://coconut-lang.org/>
16
+
A Jupyter Notebook can utilise any program kernel that implements the [Jupyter messaging protocol](http://jupyter-client.readthedocs.io/en/latest/messaging.html) for executing code.
17
+
There are kernels available for [Python](http://ipython.org/notebook.html), [Julia](https://github.com/JuliaLang/IJulia.jl), [Ruby](https://github.com/minad/iruby), [Haskell](https://github.com/gibiansky/IHaskell) and [many other languages](https://github.com/jupyter/jupyter/wiki/Jupyter-kernels).
18
+
19
+
In this notebook we demonstrate executing code with the [Coconut Programming Language](http://coconut-lang.org), a variant of Python built for *simple, elegant, Pythonic functional programming*.
20
+
21
+
In the first example we will define a recursive `factorial` function, a fundamentally functional approach that doesn’t involve any state changes or loops:
17
22
18
23
```{code-cell} coconut
19
24
def factorial(n):
20
25
"""Compute n! where n is an integer >= 0."""
21
-
if n `isinstance` int and n >= 0:
22
-
acc = 1
23
-
for x in range(1, n+1):
24
-
acc *= x
25
-
return acc
26
+
case n:
27
+
match 0:
28
+
return 1
29
+
match x is int if x > 0:
30
+
return x * factorial(x-1)
26
31
else:
27
32
raise TypeError("the argument to factorial must be an integer >= 0")
28
33
29
34
3 |> factorial |> print
30
35
```
31
36
37
+
Although this example is very basic, pattern-matching is both one of Coconut’s most powerful and most complicated features.
38
+
39
+
In the second example, we implement the quick sort algorithm.
40
+
This quick_sort algorithm works using a bunch of new constructs:
41
+
32
42
```{code-cell} coconut
33
43
def quick_sort(l):
34
44
"""Sort the input iterator using the quick sort algorithm."""
@@ -42,6 +52,8 @@ def quick_sort(l):
42
52
[3,0,4,2,1] |> quick_sort |> list |> print
43
53
```
44
54
55
+
Finally, we see that exceptions are raised as one would expect:
0 commit comments