1
1
mirror of https://github.com/tweag/nickel.git synced 2024-11-10 10:46:49 +03:00
nickel/examples/lists
2021-05-04 16:35:08 +02:00
..
lists.ncl Put each example in its own directory with a README 2021-04-28 18:37:11 +02:00
README.md Update README.md 2021-05-04 16:35:08 +02:00

Lists

This example shows how to write generic, statically typed functions in Nickel. The code is illustrative: in practice, you should use the list stdlib functions lists.map and lists.fold instead of redefining your own.

Run

$ nickel -f lists.ncl export

Playground

To see the effect of static typing, you can alter any of the map or fold function to see the program fails before it is even executed.

Another aspect is how Nickel guard typed function from untyped code by using contracts: at the end of the example, you can provide wrong arguments in the calls to myListLib.map or myListLib.fold to see contracts error appear:

-    let l = myListLib.map (fun x => x+1) [1, 2, 3, 4, 5, 6] in
+    let l = myListLib.map [1, 2, 3, 4, 5, 6] (fun x => x+1) in