From c7564c958c8b630b84bfd81ce01dd44d5fc48541 Mon Sep 17 00:00:00 2001 From: Boris Verkhovskiy Date: Sun, 12 May 2024 02:42:27 -0600 Subject: Lowercase URLs --- ATS.html.markdown | 607 ----------------------------------------- CHICKEN.html.markdown | 517 ----------------------------------- LOLCODE.html.markdown | 185 ------------- ats.html.markdown | 607 +++++++++++++++++++++++++++++++++++++++++ chicken.html.markdown | 517 +++++++++++++++++++++++++++++++++++ de-de/LOLCODE-de.html.markdown | 188 ------------- de-de/lolcode-de.html.markdown | 188 +++++++++++++ fr-fr/HTML-fr.html.markdown | 117 -------- fr-fr/html-fr.html.markdown | 117 ++++++++ lolcode.html.markdown | 185 +++++++++++++ no-nb/JSON-no.html.markdown | 60 ---- no-nb/json-no.html.markdown | 60 ++++ 12 files changed, 1674 insertions(+), 1674 deletions(-) delete mode 100644 ATS.html.markdown delete mode 100644 CHICKEN.html.markdown delete mode 100644 LOLCODE.html.markdown create mode 100644 ats.html.markdown create mode 100644 chicken.html.markdown delete mode 100644 de-de/LOLCODE-de.html.markdown create mode 100644 de-de/lolcode-de.html.markdown delete mode 100644 fr-fr/HTML-fr.html.markdown create mode 100644 fr-fr/html-fr.html.markdown create mode 100644 lolcode.html.markdown delete mode 100644 no-nb/JSON-no.html.markdown create mode 100644 no-nb/json-no.html.markdown diff --git a/ATS.html.markdown b/ATS.html.markdown deleted file mode 100644 index f290ac0f..00000000 --- a/ATS.html.markdown +++ /dev/null @@ -1,607 +0,0 @@ ---- -language: ATS -contributors: - - ["Mark Barbone", "https://github.com/mb64"] -filename: learnats.dats ---- - -ATS is a low-level functional programming language. It has a powerful type -system which lets you write programs with the same level of control and -efficiency as C, but in a memory safe and type safe way. - -The ATS type system supports: - -* Full type erasure: ATS compiles to efficient C -* Dependent types, including [LF](http://twelf.org/wiki/LF) and proving - metatheorems -* Refinement types -* Linearity for resource tracking -* An effect system that tracks exceptions, mutation, termination, and other - side effects - -This tutorial is not an introduction to functional programming, dependent types, -or linear types, but rather to how they all fit together in ATS. That said, ATS -is a very complex language, and this tutorial doesn't cover it all. Not only -does ATS's type system boast a wide array of confusing features, its -idiosyncratic syntax can make even "simple" examples hard to understand. In the -interest of keeping it a reasonable length, this document is meant to give a -taste of ATS, giving a high-level overview of what's possible and how, rather -than attempting to fully explain how everything works. - -You can [try ATS in your browser](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php), -or install it from [http://www.ats-lang.org/](http://www.ats-lang.org/). - - -```ocaml -// Include the standard library -#include "share/atspre_define.hats" -#include "share/atspre_staload.hats" - -// To compile, either use -// $ patscc -DATS_MEMALLOC_LIBC program.dats -o program -// or install the ats-acc wrapper https://github.com/sparverius/ats-acc and use -// $ acc pc program.dats - -// C-style line comments -/* and C-style block comments */ -(* as well as ML-style block comments *) - -/*************** Part 1: the ML fragment ****************/ - -val () = print "Hello, World!\n" - -// No currying -fn add (x: int, y: int) = x + y - -// fn vs fun is like the difference between let and let rec in OCaml/F# -fun fact (n: int): int = if n = 0 then 1 else n * fact (n-1) - -// Multi-argument functions need parentheses when you call them; single-argument -// functions can omit parentheses -val forty_three = add (fact 4, 19) - -// let is like let in SML -fn sum_and_prod (x: int, y: int): (int, int) = - let - val sum = x + y - val prod = x * y - in (sum, prod) end - -// 'type' is the type of all heap-allocated, non-linear types -// Polymorphic parameters go in {} after the function name -fn id {a:type} (x: a) = x - -// ints aren't heap-allocated, so we can't pass them to 'id' -// val y: int = id 7 // doesn't compile - -// 't@ype' is the type of all non-linear potentially unboxed types. It is a -// supertype of 'type'. -// Templated parameters go in {} before the function name -fn {a:t@ype} id2 (x: a) = x - -val y: int = id2 7 // works - -// can't have polymorphic t@ype parameters -// fn id3 {a:t@ype} (x: a) = x // doesn't compile - -// explicity specifying type parameters: -fn id4 {a:type} (x: a) = id {a} x // {} for non-template parameters -fn id5 {a:type} (x: a) = id2 x // <> for template parameters -fn id6 {a:type} (x: a) = id {..} x // {..} to explicitly infer it - -// Heap allocated shareable datatypes -// using datatypes leaks memory -datatype These (a:t@ype, b:t@ype) = This of a - | That of b - | These of (a, b) - -// Pattern matching using 'case' -fn {a,b: t@ype} from_these (x: a, y: b, these: These(a,b)): (a, b) = - case these of - | This(x) => (x, y) // Shadowing of variable names is fine; here, x shadows - // the parameter x - | That(y) => (x, y) - | These(x, y) => (x, y) - -// Partial pattern match using 'case-' -// Will throw an exception if passed This -fn {a,b:t@ype} unwrap_that (these: These(a,b)): b = - case- these of - | That(y) => y - | These(_, y) => y - - -/*************** Part 2: refinements ****************/ - -// Parameterize functions by what values they take and return -fn cool_add {n:int} {m:int} (x: int n, y: int m): int (n+m) = x + y - -// list(a, n) is a list of n a's -fun square_all {n:int} (xs: list(int, n)): list(int, n) = - case xs of - | list_nil() => list_nil() - | list_cons(x, rest) => list_cons(x * x, square_all rest) - -fn {a:t@ype} get_first {n:int | n >= 1} (xs: list(a, n)): a = - case+ xs of // '+' asks ATS to prove it's total - | list_cons(x, _) => x - -// Can't run get_first on lists of length 0 -// val x: int = get_first (list_nil()) // doesn't compile - -// in the stdlib: -// sortdef nat = {n:int | n >= 0} -// sortdef pos = {n:int | n >= 1} - -fn {a:t@ype} also_get_first {n:pos} (xs: list(a, n)): a = - let - val+ list_cons(x, _) = xs // val+ also works - in x end - -// tail-recursive reverse -fun {a:t@ype} reverse {n:int} (xs: list(a, n)): list(a, n) = - let - // local functions can use type variables from their enclosing scope - // this one uses both 'a' and 'n' - fun rev_helper {i:nat} (xs: list(a, n-i), acc: list(a, i)): list(a, n) = - case xs of - | list_nil() => acc - | list_cons(x, rest) => rev_helper(rest, list_cons(x, acc)) - in rev_helper(xs, list_nil) end - -// ATS has three context-dependent namespaces -// the two 'int's mean different things in this example, as do the two 'n's -fn namespace_example {n:int} (n: int n): int n = n -// ^^^ sort namespace -// ^ ^^^ ^ ^^^ ^ statics namespace -// ^^^^^^^^^^^^^^^^^ ^ ^ value namespace - -// a termination metric can go in .< >. -// it must decrease on each recursive call -// then ATS will prove it doesn't infinitely recurse -fun terminating_factorial {n:nat} .. (n: int n): int = - if n = 0 then 1 else n * terminating_factorial (n-1) - - -/*************** Part 3: the LF fragment ****************/ - -// ATS supports proving theorems in LF (http://twelf.org/wiki/LF) - -// Relations are represented by inductive types - -// Proofs that the nth fibonacci number is f -dataprop Fib(n:int, m:int) = - | FibZero(0, 0) - | FibOne(1, 1) - | {n, f1, f2: int} FibInd(n, f1 + f2) of (Fib(n-1,f1), Fib(n-2,f2)) - -// Proved-correct fibonacci implementation -// [A] B is an existential type: "there exists A such that B" -// (proof | value) -fun fib {n:nat} .. (n: int n): [f:int] (Fib(n,f) | int f) = - if n = 0 then (FibZero | 0) else - if n = 1 then (FibOne | 1) else - let - val (proof1 | val1) = fib (n-1) - val (proof2 | val2) = fib (n-2) - // the existential type is inferred - in (FibInd(proof1, proof2) | val1 + val2) end - -// Faster proved-correct fibonacci implementation -fn fib_tail {n:nat} (n: int n): [f:int] (Fib(n,f) | int f) = - let - fun loop {i:int | i < n} {f1, f2: int} .. - (p1: Fib(i,f1), p2: Fib(i+1,f2) - | i: int i, f1: int f1, f2: int f2, n: int n - ): [f:int] (Fib(n,f) | int f) = - if i = n - 1 - then (p2 | f2) - else loop (p2, FibInd(p2,p1) | i+1, f2, f1+f2, n) - in if n = 0 then (FibZero | 0) else loop (FibZero, FibOne | 0, 0, 1, n) end - -// Proof-level lists of ints, of type 'sort' -datasort IntList = ILNil of () - | ILCons of (int, IntList) - -// ILAppend(x,y,z) iff x ++ y = z -dataprop ILAppend(IntList, IntList, IntList) = - | {y:IntList} AppendNil(ILNil, y, y) - | {a:int} {x,y,z: IntList} - AppendCons(ILCons(a,x), y, ILCons(a,z)) of ILAppend(x,y,z) - -// prfuns/prfns are compile-time functions acting on proofs - -// metatheorem: append is total -prfun append_total {x,y: IntList} .. (): [z:IntList] ILAppend(x,y,z) - = scase x of // scase lets you inspect static arguments (only in prfuns) - | ILNil() => AppendNil - | ILCons(a,rest) => AppendCons(append_total()) - - -/*************** Part 4: views ****************/ - -// views are like props, but linear; ie they must be consumed exactly once -// prop is a subtype of view - -// 'type @ address' is the most basic view - -fn {a:t@ype} read_ptr {l:addr} (pf: a@l | p: ptr l): (a@l | a) = - let - // !p searches for usable proofs that say something is at that address - val x = !p - in (pf | x) end - -// oops, tried to dereference a potentially invalid pointer -// fn {a:t@ype} bad {l:addr} (p: ptr l): a = !p // doesn't compile - -// oops, dropped the proof (leaked the memory) -// fn {a:t@ype} bad {l:addr} (pf: a@l | p: ptr l): a = !p // doesn't compile - -fn inc_at_ptr {l:addr} (pf: int@l | p: ptr l): (int@l | void) = - let - // !p := value writes value to the location at p - // like !p, it implicitly searches for usable proofs that are in scope - val () = !p := !p + 1 - in (pf | ()) end - -// threading proofs through gets annoying -fn inc_three_times {l:addr} (pf: int@l | p: ptr l): (int@l | void) = - let - val (pf2 | ()) = inc_at_ptr (pf | p) - val (pf3 | ()) = inc_at_ptr (pf2 | p) - val (pf4 | ()) = inc_at_ptr (pf3 | p) - in (pf4 | ()) end - -// so there's special syntactic sugar for when you don't consume a proof -fn dec_at_ptr {l:addr} (pf: !int@l | p: ptr l): void = - !p := !p - 1 // ^ note the exclamation point - -fn dec_three_times {l:addr} (pf: !int@l | p: ptr l): void = - let - val () = dec_at_ptr (pf | p) - val () = dec_at_ptr (pf | p) - val () = dec_at_ptr (pf | p) - in () end - -// dataview is like dataprop, but linear -// A proof that either the address is null, or there is a value there -dataview MaybeNull(a:t@ype, addr) = - | NullPtr(a, null) - | {l:addr | l > null} NonNullPtr(a, l) of (a @ l) - -fn maybe_inc {l:addr} (pf: !MaybeNull(int, l) | p: ptr l): void = - if ptr1_is_null p - then () - else let - // Deconstruct the proof to access the proof of a @ l - prval NonNullPtr(value_exists) = pf - val () = !p := !p + 1 - // Reconstruct it again for the caller - prval () = pf := NonNullPtr(value_exists) - in () end - -// array_v(a,l,n) represents and array of n a's at location l -// this gets compiled into an efficient for loop, since all proofs are erased -fn sum_array {l:addr}{n:nat} (pf: !array_v(int,l,n) | p: ptr l, n: int n): int = - let - fun loop {l:addr}{n:nat} .. ( - pf: !array_v(int,l,n) - | ptr: ptr l, - length: int n, - acc: int - ): int = if length = 0 - then acc - else let - prval (head, rest) = array_v_uncons(pf) - val result = loop(rest | ptr_add(ptr, 1), length - 1, acc + !ptr) - prval () = pf := array_v_cons(head, rest) - in result end - in loop (pf | p, n, 0) end - -// 'var' is used to create stack-allocated (lvalue) variables -val seven: int = let - var res: int = 3 - // they can be modified - val () = res := res + 1 - // addr@ res is a pointer to it, and view@ res is the associated proof - val (pf | ()) = inc_three_times(view@ res | addr@ res) - // need to give back the view before the variable goes out of scope - prval () = view@ res := pf - in res end - -// References let you pass lvalues, like in C++ -fn square (x: &int): void = - x := x * x // they can be modified - -val sixteen: int = let - var res: int = 4 - val () = square res - in res end - -fn inc_at_ref (x: &int): void = - let - // like vars, references have views and addresses - val (pf | ()) = inc_at_ptr(view@ x | addr@ x) - prval () = view@ x := pf - in () end - -// Like ! for views, & references are only legal as argument types -// fn bad (x: &int): &int = x // doesn't compile - -// this takes a proof int n @ l, but returns a proof int (n+1) @ l -// since they're different types, we can't use !int @ l like before -fn refined_inc_at_ptr {n:int}{l:addr} ( - pf: int n @ l | p: ptr l -): (int (n+1) @ l | void) = - let - val () = !p := !p + 1 - in (pf | ()) end - -// special syntactic sugar for returning a proof at a different type -fn refined_dec_at_ptr {n:int}{l:addr} ( - pf: !int n @ l >> int (n-1) @ l | p: ptr l -): void = - !p := !p - 1 - -// legal but very bad code -prfn swap_proofs {v1,v2:view} (a: !v1 >> v2, b: !v2 >> v1): void = - let - prval tmp = a - prval () = a := b - prval () = b := tmp - in () end - -// also works with references -fn refined_square {n:int} (x: &int n >> int (n*n)): void = - x := x * x - -fn replace {a,b:vtype} (dest: &a >> b, src: b): a = - let - val old = dest - val () = dest := src - in old end - -// values can be uninitialized -fn {a:vt@ype} write (place: &a? >> a, value: a): void = - place := value - -val forty: int = let - var res: int - val () = write (res, 40) - in res end - -// viewtype: a view and a type -viewtypedef MaybeNullPtr(a:t@ype) = [l:addr] (MaybeNull(a, l) | ptr l) -// MaybeNullPtr has type 'viewtype' (aka 'vtype') -// type is a subtype of vtype and t@ype is a subtype of vt@ype - -// The most general identity function: -fn {a:vt@ype} id7 (x: a) = x - -// since they contain views, viewtypes must be used linearly -// fn {a:vt@ype} duplicate (x: a) = (x, x) // doesn't compile -// fn {a:vt@ype} ignore (x: a) = () // doesn't compile - -// arrayptr(a,l,n) is a convenient built-in viewtype -fn easier_sum_array {l:addr}{n:nat} (p: !arrayptr(int,l,n), n: int n): int = - let - fun loop {i:nat | i <= n} ( - p: !arrayptr(int,l,n), n: int n, i: int i, acc: int - ): int = - if i = n - then acc - else loop(p, n, i+1, acc + p[i]) - in loop(p, n, 0, 0) end - - -/*************** Part 5: dataviewtypes ****************/ - -// a dataviewtype is a heap-allocated non-shared inductive type - -// in the stdlib: -// dataviewtype list_vt(a:vt@ype, int) = -// | list_vt_nil(a, 0) -// | {n:nat} list_vt_cons(a, n+1) of (a, list_vt(a, n)) - -fn {a:vt@ype} length {n:int} (l: !list_vt(a, n)): int n = - let // ^ since we're not consuming it - fun loop {acc:int} (l: !list_vt(a, n-acc), acc: int acc): int n = - case l of - | list_vt_nil() => acc - | list_vt_cons(head, tail) => loop(tail, acc + 1) - in loop (l, 0) end - -// vvvvv not vt@ype, because you can't easily get rid of a vt@ype -fun {a:t@ype} destroy_list {n:nat} (l: list_vt(a,n)): void = - case l of - // ~ pattern match consumes and frees that node - | ~list_vt_nil() => () - | ~list_vt_cons(_, tail) => destroy_list tail - -// unlike a datatype, a dataviewtype can be modified: -fun {a:vt@ype} push_back {n:nat} ( - x: a, - l: &list_vt(a,n) >> list_vt(a,n+1) -): void = - case l of - | ~list_vt_nil() => l := list_vt_cons(x, list_vt_nil) - // @ pattern match disassembles/"unfolds" the datavtype's view, so you can - // modify its components - | @list_vt_cons(head, tail) => let - val () = push_back (x, tail) - // reassemble it with fold@ - prval () = fold@ l - in () end - -fun {a:vt@ype} pop_last {n:pos} (l: &list_vt(a,n) >> list_vt(a,n-1)): a = - let - val+ @list_vt_cons(head, tail) = l - in case tail of - | list_vt_cons _ => let - val res = pop_last tail - prval () = fold@ l - in res end - | ~list_vt_nil() => let - val head = head - // Deallocate empty datavtype nodes with free@ - val () = free@{..}{0} l - val () = l := list_vt_nil() - in head end - /** Equivalently: - * | ~list_vt_nil() => let - * prval () = fold@ l - * val+ ~list_vt_cons(head, ~list_vt_nil()) = l - * val () = l := list_vt_nil() - * in head end - */ - end - -// "holes" (ie uninitialized memory) can be created with _ on the RHS -// This function uses destination-passing-style to copy the list in a single -// tail-recursive pass. -fn {a:t@ype} copy_list {n:nat} (l: !list_vt(a, n)): list_vt(a, n) = - let - var res: ptr - fun loop {k:nat} (l: !list_vt(a, k), hole: &ptr? >> list_vt(a, k)): void = - case l of - | list_vt_nil() => hole := list_vt_nil - | list_vt_cons(first, rest) => let - val () = hole := list_vt_cons{..}{k-1}(first, _) - // ^ on RHS: a hole - val+list_vt_cons(_, new_hole) = hole - // ^ on LHS: wildcard pattern (not a hole) - val () = loop (rest, new_hole) - prval () = fold@ hole - in () end - val () = loop (l, res) - in res end - -// Reverse a linked-list *in place* -- no allocations or frees -fn {a:vt@ype} in_place_reverse {n:nat} (l: list_vt(a, n)): list_vt(a, n) = - let - fun loop {k:nat} (l: list_vt(a, n-k), acc: list_vt(a, k)): list_vt(a, n) = - case l of - | ~list_vt_nil() => acc - | @list_vt_cons(x, tail) => let - val rest = replace(tail, acc) - // the node 'l' is now part of acc instead of the original list - prval () = fold@ l - in loop (rest, l) end - in loop (l, list_vt_nil) end - - -/*************** Part 6: miscellaneous extras ****************/ - -// a record -// Point has type 't@ype' -typedef Point = @{ x= int, y= int } -val origin: Point = @{ x= 0, y= 0 } - -// tuples and records are normally unboxed, but there are boxed variants -// BoxedPoint has type 'type' -typedef BoxedPoint = '{ x= int, y= int } -val boxed_pair: '(int,int) = '(5, 3) - -// When passing a pair as the single argument to a function, it needs to be -// written @(a,b) to avoid ambiguity with multi-argument functions -val six_plus_seven = let - fun sum_of_pair (pair: (int,int)) = pair.0 + pair.1 - in sum_of_pair @(6, 7) end - -// When a constructor has no associated data, such as None(), the parentheses -// are optional in expressions. However, they are mandatory in patterns -fn inc_option (opt: Option int) = - case opt of - | Some(x) => Some(x+1) - | None() => None - -// ATS has a simple FFI, since it compiles to C and (mostly) uses the C ABI -%{ -// Inline C code -int scanf_wrapper(void *format, void *value) { - return scanf((char *) format, (int *) value); -} -%} -// If you wanted to, you could define a custom dataviewtype more accurately -// describing the result of scanf -extern fn scanf (format: string, value: &int): int = "scanf_wrapper" - -fn get_input_number (): Option int = - let - var x: int = 0 - in - if scanf("%d\n", x) = 1 - then Some(x) - else None - end - -// extern is also used for separate declarations and definitions -extern fn say_hi (): void -// later on, or in another file: -implement say_hi () = print "hi\n" - -// if you implement main0, it will run as the main function -// implmnt is an alias for implement -implmnt main0 () = () - -// as well as for axioms: -extern praxi view_id {a:view} (x: a): a -// you don't need to actually implement the axioms, but you could -primplmnt view_id x = x -// primplmnt is an alias for primplement - -// Some standard aliases are: -// List0(a) = [n:nat] list(a,n) and List0_vt(a) = [n:nat] list_vt(a,n) -// t0p = t@ype and vt0p = vt@ype -fun {a:t0p} append (xs: List0 a, ys: List0 a): List0 a = - case xs of - | list_nil() => ys - | list_cons(x, xs) => list_cons(x, append(xs, ys)) - -// there are many ways of doing things after one another -val let_in_example = let - val () = print "thing one\n" - val () = print "thing two\n" - in () end - -val parens_example = (print "thing one\n"; print "thing two\n") - -val begin_end_example = begin - print "thing one\n"; - print "thing two\n"; // optional trailing semicolon - end - -// and many ways to use local variables -fun times_four_let x = - let - fun times_two (x: int) = x * 2 - in times_two (times_two x) end - -local - fun times_two (x: int) = x * 2 -in - fun times_four_local x = times_two (times_two x) -end - -fun times_four_where x = times_two (times_two x) - where { - fun times_two (x: int) = x * 2 - } - -//// The last kind of comment in ATS is an end-of-file comment. - -Anything between the four slashes and the end of the file is ignored. - -Have fun with ATS! -``` - -## Learn more - -This isn't all there is to ATS -- notably, some core features like closures and -the effect system are left out, as well as other less type-y stuff like modules -and the build system. If you'd like to write these sections yourself, -contributions would be welcome! - -To learn more about ATS, visit the [ATS website](http://www.ats-lang.org/), in -particular the [documentation page](http://www.ats-lang.org/Documents.html). - diff --git a/CHICKEN.html.markdown b/CHICKEN.html.markdown deleted file mode 100644 index 37f6c859..00000000 --- a/CHICKEN.html.markdown +++ /dev/null @@ -1,517 +0,0 @@ ---- -language: "CHICKEN" -filename: CHICKEN.scm -contributors: - - ["Diwakar Wagle", "https://github.com/deewakar"] ---- - - -CHICKEN is an implementation of Scheme programming language that can -compile Scheme programs to C code as well as interpret them. CHICKEN -supports R5RS and R7RS (work in progress) standards and many extensions. - - -```scheme -;; #!/usr/bin/env csi -s - -;; Run the CHICKEN REPL in the commandline as follows : -;; $ csi - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 0. Syntax -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; Single line comments start with a semicolon - -#| Block comments - can span multiple lines and... - #| can be nested - |# -|# - -;; S-expression comments are used to comment out expressions -#; (display "nothing") ; discard this expression - -;; CHICKEN has two fundamental pieces of syntax: Atoms and S-expressions -;; an atom is something that evaluates to itself -;; all builtin data types viz. numbers, chars, booleans, strings etc. are atoms -;; Furthermore an atom can be a symbol, an identifier, a keyword, a procedure -;; or the empty list (also called null) -'athing ;; => athing -'+ ;; => + -+ ;; => - -;; S-expressions (short for symbolic expressions) consists of one or more atoms -(quote +) ;; => + ; another way of writing '+ -(+ 1 2 3) ;; => 6 ; this S-expression evaluates to a function call -'(+ 1 2 3) ;; => (+ 1 2 3) ; evaluates to a list - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 1. Primitive Datatypes and Operators -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; Numbers -99999999999999999999 ;; integers -#b1010 ;; binary ; => 10 -#o10 ;; octal ; => 8 -#x8ded ;; hexadecimal ; => 36333 -3.14 ;; real -6.02e+23 -3/4 ;; rational - -;;Characters and Strings -#\A ;; A char -"Hello, World!" ;; strings are fixed-length arrays of characters - -;; Booleans -#t ;; true -#f ;; false - -;; Function call is written as (f x y z ...) -;; where f is a function and x,y,z, ... are arguments -(print "Hello, World!") ;; => Hello, World! -;; formatted output -(printf "Hello, ~a.\n" "World") ;; => Hello, World. - -;; print commandline arguments -(map print (command-line-arguments)) - -(list 'foo 'bar 'baz) ;; => (foo bar baz) -(string-append "pine" "apple") ;; => "pineapple" -(string-ref "tapioca" 3) ;; => #\i;; character 'i' is at index 3 -(string->list "CHICKEN") ;; => (#\C #\H #\I #\C #\K #\E #\N) -(string-intersperse '("1" "2") ":") ;; => "1:2" -(string-split "1:2:3" ":") ;; => ("1" "2" "3") - - -;; Predicates are special functions that return boolean values -(atom? #t) ;; => #t - -(symbol? #t) ;; => #f - -(symbol? '+) ;; => #t - -(procedure? +) ;; => #t - -(pair? '(1 2)) ;; => #t - -(pair? '(1 2 . 3)) ;; => #t - -(pair? '()) ;; => #f - -(list? '()) ;; => #t - - -;; Some arithmetic operations - -(+ 1 1) ;; => 2 -(- 8 1) ;; => 7 -(* 10 2) ;; => 20 -(expt 2 3) ;; => 8 -(remainder 5 2) ;; => 1 -(/ 35 5) ;; => 7 -(/ 1 3) ;; => 0.333333333333333 - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 2. Variables -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; You can create variables with define -;; A variable name can use any character except: ()[]{}",'`;#\ -(define myvar 5) -myvar ;; => 5 - -;; Alias to a procedure -(define ** expt) -(** 2 3) ;; => 8 - -;; Accessing an undefined variable raises an exception -s ;; => Error: unbound variable: s - -;; Local binding -(let ((me "Bob")) - (print me)) ;; => Bob - -(print me) ;; => Error: unbound variable: me - -;; Assign a new value to previously defined variable -(set! myvar 10) -myvar ;; => 10 - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 3. Collections -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; Pairs -;; 'cons' constructs pairs, -;; 'car' extracts the first element, 'cdr' extracts the rest of the elements -(cons 'subject 'verb) ;; => '(subject . verb) -(car (cons 'subject 'verb)) ;; => subject -(cdr (cons 'subject 'verb)) ;; => verb - -;; Lists -;; cons creates a new list if the second item is a list -(cons 0 '()) ;; => (0) -(cons 1 (cons 2 (cons 3 '()))) ;; => (1 2 3) -;; 'list' is a convenience variadic constructor for lists -(list 1 2 3) ;; => (1 2 3) - - -;; Use 'append' to append lists together -(append '(1 2) '(3 4)) ;; => (1 2 3 4) - -;; Some basic operations on lists -(map add1 '(1 2 3)) ;; => (2 3 4) -(reverse '(1 3 4 7)) ;; => (7 4 3 1) -(sort '(11 22 33 44) >) ;; => (44 33 22 11) - -(define days '(SUN MON FRI)) -(list-ref days 1) ;; => MON -(set! (list-ref days 1) 'TUE) -days ;; => (SUN TUE FRI) - -;; Vectors -;; Vectors are heterogeneous structures whose elements are indexed by integers -;; A Vector typically occupies less space than a list of the same length -;; Random access of an element in a vector is faster than in a list -#(1 2 3) ;; => #(1 2 3) ;; literal syntax -(vector 'a 'b 'c) ;; => #(a b c) -(vector? #(1 2 3)) ;; => #t -(vector-length #(1 (2) "a")) ;; => 3 -(vector-ref #(1 (2) (3 3)) 2);; => (3 3) - -(define vec #(1 2 3)) -(vector-set! vec 2 4) -vec ;; => #(1 2 4) - -;; Vectors can be created from lists and vice-verca -(vector->list #(1 2 4)) ;; => '(1 2 4) -(list->vector '(a b c)) ;; => #(a b c) - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 4. Functions -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; Use 'lambda' to create functions. -;; A function always returns the value of its last expression -(lambda () "Hello World") ;; => # - -;; Use extra parens around function definition to execute -((lambda () "Hello World")) ;; => Hello World ;; argument list is empty - -;; A function with an argument -((lambda (x) (* x x)) 3) ;; => 9 -;; A function with two arguments -((lambda (x y) (* x y)) 2 3) ;; => 6 - -;; assign a function to a variable -(define sqr (lambda (x) (* x x))) -sqr ;; => # -(sqr 3) ;; => 9 - -;; We can shorten this using the function definition syntactic sugar -(define (sqr x) (* x x)) -(sqr 3) ;; => 9 - -;; We can redefine existing procedures -(foldl cons '() '(1 2 3 4 5)) ;; => (((((() . 1) . 2) . 3) . 4) . 5) -(define (foldl func accu alist) - (if (null? alist) - accu - (foldl func (func (car alist) accu) (cdr alist)))) - -(foldl cons '() '(1 2 3 4 5)) ;; => (5 4 3 2 1) - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 5. Equality -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; For numbers use '=' -(= 3 3.0) ;; => #t -(= 2 1) ;; => #f - -;; 'eq?' returns #t if two arguments refer to the same object in memory -;; In other words, it's a simple pointer comparison. -(eq? '() '()) ;; => #t ;; there's only one empty list in memory -(eq? (list 3) (list 3)) ;; => #f ;; not the same object -(eq? 'yes 'yes) ;; => #t -(eq? 3 3) ;; => #t ;; don't do this even if it works in this case -(eq? 3 3.0) ;; => #f ;; it's better to use '=' for number comparisons -(eq? "Hello" "Hello") ;; => #f - -;; 'eqv?' is same as 'eq?' all datatypes except numbers and characters -(eqv? 3 3.0) ;; => #f -(eqv? (expt 2 3) (expt 2 3)) ;; => #t -(eqv? 'yes 'yes) ;; => #t - -;; 'equal?' recursively compares the contents of pairs, vectors, and strings, -;; applying eqv? on other objects such as numbers and symbols. -;; A rule of thumb is that objects are generally equal? if they print the same. - -(equal? '(1 2 3) '(1 2 3)) ;; => #t -(equal? #(a b c) #(a b c)) ;; => #t -(equal? 'a 'a) ;; => #t -(equal? "abc" "abc") ;; => #t - -;; In Summary: -;; eq? tests if objects are identical -;; eqv? tests if objects are operationally equivalent -;; equal? tests if objects have same structure and contents - -;; Comparing strings for equality -(string=? "Hello" "Hello") ;; => #t - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 6. Control Flow -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; Conditionals -(if #t ;; test expression - "True" ;; then expression - "False") ;; else expression - ;; => "True" - -(if (> 3 2) - "yes" - "no") ;; => "yes" - -;; In conditionals, all values that are not '#f' are treated as true. -;; 0, '(), #() "" , are all true values -(if 0 - "0 is not false" - "0 is false") ;; => "0 is not false" - -;; 'cond' chains a series of tests and returns as soon as it encounters a true condition -;; 'cond' can be used to simulate 'if/elseif/else' statements -(cond ((> 2 2) "not true so don't return this") - ((< 2 5) "true, so return this") - (else "returning default")) ;; => "true, so return this" - - -;; A case expression is evaluated as follows: -;; The key is evaluated and compared with each datum in sense of 'eqv?', -;; The corresponding clause in the matching datum is evaluated and returned as result -(case (* 2 3) ;; the key is 6 - ((2 3 5 7) 'prime) ;; datum 1 - ((1 4 6 8) 'composite)) ;; datum 2; matched! - ;; => composite - -;; case with else clause -(case (car '(c d)) - ((a e i o u) 'vowel) - ((w y) 'semivowel) - (else 'consonant)) ;; => consonant - -;; Boolean expressions -;; 'and' returns the first expression that evaluates to #f -;; otherwise, it returns the result of the last expression -(and #t #f (= 2 2.0)) ;; => #f -(and (< 2 5) (> 2 0) "0 < 2 < 5") ;; => "0 < 2 < 5" - -;; 'or' returns the first expression that evaluates to #t -;; otherwise the result of the last expression is returned -(or #f #t #f) ;; => #t -(or #f #f #f) ;; => #f - -;; 'when' is like 'if' without the else expression -(when (positive? 5) "I'm positive") ;; => "I'm positive" - -;; 'unless' is equivalent to (when (not ) ) -(unless (null? '(1 2 3)) "not null") ;; => "not null" - - -;; Loops -;; loops can be created with the help of tail-recursions -(define (loop count) - (unless (= count 0) - (print "hello") - (loop (sub1 count)))) -(loop 4) ;; => hello, hello ... - -;; Or with a named let -(let loop ((i 0) (limit 5)) - (when (< i limit) - (printf "i = ~a\n" i) - (loop (add1 i) limit))) ;; => i = 0, i = 1.... - -;; 'do' is another iteration construct -;; It initializes a set of variables and updates them in each iteration -;; A final expression is evaluated after the exit condition is met -(do ((x 0 (add1 x ))) ;; initialize x = 0 and add 1 in each iteration - ((= x 10) (print "done")) ;; exit condition and final expression - (print x)) ;; command to execute in each step - ;; => 0,1,2,3....9,done - -;; Iteration over lists -(for-each (lambda (a) (print (* a a))) - '(3 5 7)) ;; => 9, 25, 49 - -;; 'map' is like for-each but returns a list -(map add1 '(11 22 33)) ;; => (12 23 34) - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 7. Extensions -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; The CHICKEN core is very minimal, but additional features are provided by library extensions known as Eggs. -;; You can install Eggs with 'chicken-install ' command. - -;; complex numbers -3+4i ;; => 3+2i -;; Supports fractions without falling back to inexact flonums -1/3 ;; => 1/3 -;; provides support for large integers through bignums -(expt 9 20) ;; => 12157665459056928801 -;; And other 'extended' functions -(log 10 (exp 1)) ;; => 2.30258509299405 -(numerator 2/3) ;; => 2 - -;; 'utf8' provides unicode support -(import utf8) -"\u03BBx:(\u03BC\u0251.\u0251\u2192\u0251).xx" ;; => "λx:(μɑ.ɑ→ɑ).xx" - -;; 'posix' provides file I/O and lots of other services for unix-like operating systems -;; Some of the functions are not available in Windows system, -;; See http://wiki.call-cc.org/man/5/Module%20(chicken%20file%20posix) for more details - -;; Open a file to append, open "write only" and create file if it does not exist -(define outfn (file-open "chicken-hen.txt" (+ open/append open/wronly open/creat))) -;; write some text to the file -(file-write outfn "Did chicken came before hen?") -;; close the file -(file-close outfn) -;; Open the file "read only" -(define infn (file-open "chicken-hen.txt" open/rdonly)) -;; read some text from the file -(file-read infn 30) ;; => ("Did chicken came before hen? ", 28) -(file-close infn) - -;; CHICKEN also supports SRFI (Scheme Requests For Implementation) extensions -;; See 'http://srfi.schemers.org/srfi-implementers.html" to see srfi's supported by CHICKEN -(import srfi-1) ;; list library -(filter odd? '(1 2 3 4 5 6 7)) ;; => (1 3 5 7) -(count even? '(1 2 3 4 5)) ;; => 2 -(take '(12 24 36 48 60) 3) ;; => (12 24 36) -(drop '(12 24 36 48 60) 2) ;; => (36 48 60) -(circular-list 'z 'q) ;; => z q z q ... - -(import srfi-13) ;; string library -(string-reverse "pan") ;; => "nap" -(string-index "Turkey" #\k) ;; => 3 -(string-every char-upper-case? "CHICKEN") ;; => #t -(string-join '("foo" "bar" "baz") ":") ;; => "foo:bar:baz" - - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 8. Macros -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; A 'for .. in ..' iteration like python, for lists -(define-syntax for - (syntax-rules (in) - ((for elem in alist body ...) - (for-each (lambda (elem) body ...) alist)))) - -(for x in '(2 4 8 16) - (print x)) ;; => 2, 4, 8, 16 - -(for chr in (string->list "PENCHANT") - (print chr)) ;; => P, E, N, C, H, A, N, T - -;; While loop -(define-syntax while - (syntax-rules () - ((while cond body ...) - (let loop () - (when cond - body ... - (loop)))))) - -(let ((str "PENCHANT") (i 0)) - (while (< i (string-length str)) ;; while (condition) - (print (string-ref str i)) ;; body - (set! i (add1 i)))) - ;; => P, E, N, C, H, A, N, T - -;; Advanced Syntax-Rules Primer -> http://petrofsky.org/src/primer.txt -;; Macro system in chicken -> http://lists.gnu.org/archive/html/chicken-users/2008-04/msg00013.html - -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -; 9. Modules -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; - -;; Also See http://wiki.call-cc.org/man/5/Modules - -;; The 'test' module exports a value named 'hello' and a macro named 'greet' -(module test (hello greet) - (import scheme) - - (define-syntax greet - (syntax-rules () - ((_ whom) - (begin - (display "Hello, ") - (display whom) - (display " !\n") ) ) ) ) - - (define (hello) - (greet "world") ) ) - -;; we can define our modules in a separate file (say test.scm) and load them to the interpreter with -;; (load "test.scm") - -;; import the module -(import test) -(hello) ;; => Hello, world ! -(greet "schemers") ;; => Hello, schemers ! - -;; We can compile the module files in to shared libraries by using following command, -;; csc -s test.scm -;; (load "test.so") - -;; Functors -;; Functors are high level modules that can be parameterized by other modules -;; Following functor requires another module named 'M' that provides a function called 'multiply' -;; The functor itself exports a generic function 'square' -(functor (squaring-functor (M (multiply))) (square) - (import scheme M) - (define (square x) (multiply x x))) - -;; Module 'nums' can be passed as a parameter to 'squaring-functor' -(module nums (multiply) - (import scheme) ;; predefined modules - (define (multiply x y) (* x y))) -;; the final module can be imported and used in our program -(module number-squarer = (squaring-functor nums)) - -(import number-squarer) -(square 3) ;; => 9 - -;; We can instantiate the functor for other inputs -;; Here's another example module that can be passed to squaring-functor -(module stars (multiply) - (import chicken scheme) ;; chicken module for the 'use' keyword - (use srfi-1) ;; we can use external libraries in our module - (define (multiply x y) - (list-tabulate x (lambda _ (list-tabulate y (lambda _ '*)))))) -(module star-squarer = (squaring-functor stars)) - -(import star-squarer) -(square 3) ;; => ((* * *)(* * *)(* * *)) -``` - -## Further Reading -* [CHICKEN User's Manual](https://wiki.call-cc.org/manual). -* [R5RS standards](http://www.schemers.org/Documents/Standards/R5RS) - - -## Extra Info - -* [For programmers of other languages](https://wiki.call-cc.org/chicken-for-programmers-of-other-languages) -* [Compare CHICKEN syntax with other languages](http://plr.sourceforge.net/cgi-bin/plr/launch.py) diff --git a/LOLCODE.html.markdown b/LOLCODE.html.markdown deleted file mode 100644 index 217b7b7d..00000000 --- a/LOLCODE.html.markdown +++ /dev/null @@ -1,185 +0,0 @@ ---- -language: LOLCODE -filename: learnLOLCODE.lol -contributors: - - ["abactel", "https://github.com/abactel"] ---- - -LOLCODE is an esoteric programming language designed to resemble the speech of [lolcats](https://upload.wikimedia.org/wikipedia/commons/a/ab/Lolcat_in_folder.jpg?1493656347257). - -``` -BTW This is an inline comment -BTW All code must begin with `HAI ` and end with `KTHXBYE` - -HAI 1.3 -CAN HAS STDIO? BTW Importing standard headers - -OBTW - ========================================================================== - ================================= BASICS ================================= - ========================================================================== -TLDR - -BTW Displaying text: -VISIBLE "HELLO WORLD" - -BTW Declaring variables: -I HAS A MESSAGE ITZ "CATZ ARE GOOD" -VISIBLE MESSAGE - -OBTW - (This is a codeblock.) Variables are dynamically typed so you don't need to - declare their type. A variable's type matches its content. These are the - types: -TLDR - -I HAS A STRING ITZ "DOGZ ARE GOOOD" BTW type is YARN -I HAS A INTEGER ITZ 42 BTW type is NUMBR -I HAS A FLOAT ITZ 3.1415 BTW type is NUMBAR -I HAS A BOOLEAN ITZ WIN BTW type is TROOF -I HAS A UNTYPED BTW type is NOOB - -BTW Accepting user input: -I HAS A AGE -GIMMEH AGE -BTW The variable is stored as a YARN. To convert it into NUMBR: -AGE IS NOW A NUMBR - -OBTW - ========================================================================== - ================================== MATH ================================== - ========================================================================== -TLDR - -BTW LOLCODE uses polish notation style math. - -BTW Basic mathematical notation: - -SUM OF 21 AN 33 BTW 21 + 33 -DIFF OF 90 AN 10 BTW 90 - 10 -PRODUKT OF 12 AN 13 BTW 12 * 13 -QUOSHUNT OF 32 AN 43 BTW 32 / 43 -MOD OF 43 AN 64 BTW 43 modulo 64 -BIGGR OF 23 AN 53 BTW max(23, 53) -SMALLR OF 53 AN 45 BTW min(53, 45) - -BTW Binary notation: - -BOTH OF WIN AN WIN BTW and: WIN if x=WIN, y=WIN -EITHER OF FAIL AN WIN BTW or: FAIL if x=FAIL, y=FAIL -WON OF WIN AN FAIL BTW xor: FAIL if x=y -NOT FAIL BTW unary negation: WIN if x=FAIL -ALL OF WIN AN WIN MKAY BTW infinite arity AND -ANY OF WIN AN FAIL MKAY BTW infinite arity OR - -BTW Comparison: - -BOTH SAEM "CAT" AN "DOG" BTW WIN if x == y -DIFFRINT 732 AN 184 BTW WIN if x != y -BOTH SAEM 12 AN BIGGR OF 12 AN 4 BTW x >= y -BOTH SAEM 43 AN SMALLR OF 43 AN 56 BTW x <= y -DIFFRINT 64 AN SMALLR OF 64 AN 2 BTW x > y -DIFFRINT 75 AN BIGGR OF 75 AN 643 BTW x < y - -OBTW - ========================================================================== - ============================== FLOW CONTROL ============================== - ========================================================================== -TLDR - -BTW If/then statement: -I HAS A ANIMAL -GIMMEH ANIMAL -BOTH SAEM ANIMAL AN "CAT", O RLY? - YA RLY - VISIBLE "YOU HAV A CAT" - MEBBE BOTH SAEM ANIMAL AN "MAUS" - VISIBLE "NOM NOM NOM. I EATED IT." - NO WAI - VISIBLE "AHHH IS A WOOF WOOF" -OIC - -BTW Case statement: -I HAS A COLOR -GIMMEH COLOR -COLOR, WTF? - OMG "R" - VISIBLE "RED FISH" - GTFO - OMG "Y" - VISIBLE "YELLOW FISH" - BTW Since there is no `GTFO` the next statements will also be tested - OMG "G" - OMG "B" - VISIBLE "FISH HAS A FLAVOR" - GTFO - OMGWTF - VISIBLE "FISH IS TRANSPARENT OHNO WAT" -OIC - -BTW For loop: -I HAS A TEMPERATURE -GIMMEH TEMPERATURE -TEMPERATURE IS NOW A NUMBR -IM IN YR LOOP UPPIN YR ITERATOR TIL BOTH SAEM ITERATOR AN TEMPERATURE - VISIBLE ITERATOR -IM OUTTA YR LOOP - -BTW While loop: -IM IN YR LOOP NERFIN YR ITERATOR WILE DIFFRINT ITERATOR AN -10 - VISIBLE ITERATOR -IM OUTTA YR LOOP - -OBTW - ========================================================================= - ================================ Strings ================================ - ========================================================================= -TLDR - -BTW Linebreaks: -VISIBLE "FIRST LINE :) SECOND LINE" - -BTW Tabs: -VISIBLE ":>SPACES ARE SUPERIOR" - -BTW Bell (goes beep): -VISIBLE "NXT CUSTOMER PLS :o" - -BTW Literal double quote: -VISIBLE "HE SAID :"I LIKE CAKE:"" - -BTW Literal colon: -VISIBLE "WHERE I LIVE:: CYBERSPACE" - -OBTW - ========================================================================= - =============================== FUNCTIONS =============================== - ========================================================================= -TLDR - -BTW Declaring a new function: -HOW IZ I SELECTMOVE YR MOVE BTW `MOVE` is an argument - BOTH SAEM MOVE AN "ROCK", O RLY? - YA RLY - VISIBLE "YOU HAV A ROCK" - NO WAI - VISIBLE "OH NO IS A SNIP-SNIP" - OIC - GTFO BTW This returns NOOB -IF U SAY SO - -BTW Declaring a function and returning a value: -HOW IZ I IZYELLOW - FOUND YR "YELLOW" -IF U SAY SO - -BTW Calling a function: -I IZ IZYELLOW MKAY - -KTHXBYE -``` - -## Further reading: - -- [LCI compiler](https://github.com/justinmeza/lci) -- [Official spec](https://github.com/justinmeza/lolcode-spec/blob/master/v1.2/lolcode-spec-v1.2.md) diff --git a/ats.html.markdown b/ats.html.markdown new file mode 100644 index 00000000..f290ac0f --- /dev/null +++ b/ats.html.markdown @@ -0,0 +1,607 @@ +--- +language: ATS +contributors: + - ["Mark Barbone", "https://github.com/mb64"] +filename: learnats.dats +--- + +ATS is a low-level functional programming language. It has a powerful type +system which lets you write programs with the same level of control and +efficiency as C, but in a memory safe and type safe way. + +The ATS type system supports: + +* Full type erasure: ATS compiles to efficient C +* Dependent types, including [LF](http://twelf.org/wiki/LF) and proving + metatheorems +* Refinement types +* Linearity for resource tracking +* An effect system that tracks exceptions, mutation, termination, and other + side effects + +This tutorial is not an introduction to functional programming, dependent types, +or linear types, but rather to how they all fit together in ATS. That said, ATS +is a very complex language, and this tutorial doesn't cover it all. Not only +does ATS's type system boast a wide array of confusing features, its +idiosyncratic syntax can make even "simple" examples hard to understand. In the +interest of keeping it a reasonable length, this document is meant to give a +taste of ATS, giving a high-level overview of what's possible and how, rather +than attempting to fully explain how everything works. + +You can [try ATS in your browser](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php), +or install it from [http://www.ats-lang.org/](http://www.ats-lang.org/). + + +```ocaml +// Include the standard library +#include "share/atspre_define.hats" +#include "share/atspre_staload.hats" + +// To compile, either use +// $ patscc -DATS_MEMALLOC_LIBC program.dats -o program +// or install the ats-acc wrapper https://github.com/sparverius/ats-acc and use +// $ acc pc program.dats + +// C-style line comments +/* and C-style block comments */ +(* as well as ML-style block comments *) + +/*************** Part 1: the ML fragment ****************/ + +val () = print "Hello, World!\n" + +// No currying +fn add (x: int, y: int) = x + y + +// fn vs fun is like the difference between let and let rec in OCaml/F# +fun fact (n: int): int = if n = 0 then 1 else n * fact (n-1) + +// Multi-argument functions need parentheses when you call them; single-argument +// functions can omit parentheses +val forty_three = add (fact 4, 19) + +// let is like let in SML +fn sum_and_prod (x: int, y: int): (int, int) = + let + val sum = x + y + val prod = x * y + in (sum, prod) end + +// 'type' is the type of all heap-allocated, non-linear types +// Polymorphic parameters go in {} after the function name +fn id {a:type} (x: a) = x + +// ints aren't heap-allocated, so we can't pass them to 'id' +// val y: int = id 7 // doesn't compile + +// 't@ype' is the type of all non-linear potentially unboxed types. It is a +// supertype of 'type'. +// Templated parameters go in {} before the function name +fn {a:t@ype} id2 (x: a) = x + +val y: int = id2 7 // works + +// can't have polymorphic t@ype parameters +// fn id3 {a:t@ype} (x: a) = x // doesn't compile + +// explicity specifying type parameters: +fn id4 {a:type} (x: a) = id {a} x // {} for non-template parameters +fn id5 {a:type} (x: a) = id2 x // <> for template parameters +fn id6 {a:type} (x: a) = id {..} x // {..} to explicitly infer it + +// Heap allocated shareable datatypes +// using datatypes leaks memory +datatype These (a:t@ype, b:t@ype) = This of a + | That of b + | These of (a, b) + +// Pattern matching using 'case' +fn {a,b: t@ype} from_these (x: a, y: b, these: These(a,b)): (a, b) = + case these of + | This(x) => (x, y) // Shadowing of variable names is fine; here, x shadows + // the parameter x + | That(y) => (x, y) + | These(x, y) => (x, y) + +// Partial pattern match using 'case-' +// Will throw an exception if passed This +fn {a,b:t@ype} unwrap_that (these: These(a,b)): b = + case- these of + | That(y) => y + | These(_, y) => y + + +/*************** Part 2: refinements ****************/ + +// Parameterize functions by what values they take and return +fn cool_add {n:int} {m:int} (x: int n, y: int m): int (n+m) = x + y + +// list(a, n) is a list of n a's +fun square_all {n:int} (xs: list(int, n)): list(int, n) = + case xs of + | list_nil() => list_nil() + | list_cons(x, rest) => list_cons(x * x, square_all rest) + +fn {a:t@ype} get_first {n:int | n >= 1} (xs: list(a, n)): a = + case+ xs of // '+' asks ATS to prove it's total + | list_cons(x, _) => x + +// Can't run get_first on lists of length 0 +// val x: int = get_first (list_nil()) // doesn't compile + +// in the stdlib: +// sortdef nat = {n:int | n >= 0} +// sortdef pos = {n:int | n >= 1} + +fn {a:t@ype} also_get_first {n:pos} (xs: list(a, n)): a = + let + val+ list_cons(x, _) = xs // val+ also works + in x end + +// tail-recursive reverse +fun {a:t@ype} reverse {n:int} (xs: list(a, n)): list(a, n) = + let + // local functions can use type variables from their enclosing scope + // this one uses both 'a' and 'n' + fun rev_helper {i:nat} (xs: list(a, n-i), acc: list(a, i)): list(a, n) = + case xs of + | list_nil() => acc + | list_cons(x, rest) => rev_helper(rest, list_cons(x, acc)) + in rev_helper(xs, list_nil) end + +// ATS has three context-dependent namespaces +// the two 'int's mean different things in this example, as do the two 'n's +fn namespace_example {n:int} (n: int n): int n = n +// ^^^ sort namespace +// ^ ^^^ ^ ^^^ ^ statics namespace +// ^^^^^^^^^^^^^^^^^ ^ ^ value namespace + +// a termination metric can go in .< >. +// it must decrease on each recursive call +// then ATS will prove it doesn't infinitely recurse +fun terminating_factorial {n:nat} .. (n: int n): int = + if n = 0 then 1 else n * terminating_factorial (n-1) + + +/*************** Part 3: the LF fragment ****************/ + +// ATS supports proving theorems in LF (http://twelf.org/wiki/LF) + +// Relations are represented by inductive types + +// Proofs that the nth fibonacci number is f +dataprop Fib(n:int, m:int) = + | FibZero(0, 0) + | FibOne(1, 1) + | {n, f1, f2: int} FibInd(n, f1 + f2) of (Fib(n-1,f1), Fib(n-2,f2)) + +// Proved-correct fibonacci implementation +// [A] B is an existential type: "there exists A such that B" +// (proof | value) +fun fib {n:nat} .. (n: int n): [f:int] (Fib(n,f) | int f) = + if n = 0 then (FibZero | 0) else + if n = 1 then (FibOne | 1) else + let + val (proof1 | val1) = fib (n-1) + val (proof2 | val2) = fib (n-2) + // the existential type is inferred + in (FibInd(proof1, proof2) | val1 + val2) end + +// Faster proved-correct fibonacci implementation +fn fib_tail {n:nat} (n: int n): [f:int] (Fib(n,f) | int f) = + let + fun loop {i:int | i < n} {f1, f2: int} .. + (p1: Fib(i,f1), p2: Fib(i+1,f2) + | i: int i, f1: int f1, f2: int f2, n: int n + ): [f:int] (Fib(n,f) | int f) = + if i = n - 1 + then (p2 | f2) + else loop (p2, FibInd(p2,p1) | i+1, f2, f1+f2, n) + in if n = 0 then (FibZero | 0) else loop (FibZero, FibOne | 0, 0, 1, n) end + +// Proof-level lists of ints, of type 'sort' +datasort IntList = ILNil of () + | ILCons of (int, IntList) + +// ILAppend(x,y,z) iff x ++ y = z +dataprop ILAppend(IntList, IntList, IntList) = + | {y:IntList} AppendNil(ILNil, y, y) + | {a:int} {x,y,z: IntList} + AppendCons(ILCons(a,x), y, ILCons(a,z)) of ILAppend(x,y,z) + +// prfuns/prfns are compile-time functions acting on proofs + +// metatheorem: append is total +prfun append_total {x,y: IntList} .. (): [z:IntList] ILAppend(x,y,z) + = scase x of // scase lets you inspect static arguments (only in prfuns) + | ILNil() => AppendNil + | ILCons(a,rest) => AppendCons(append_total()) + + +/*************** Part 4: views ****************/ + +// views are like props, but linear; ie they must be consumed exactly once +// prop is a subtype of view + +// 'type @ address' is the most basic view + +fn {a:t@ype} read_ptr {l:addr} (pf: a@l | p: ptr l): (a@l | a) = + let + // !p searches for usable proofs that say something is at that address + val x = !p + in (pf | x) end + +// oops, tried to dereference a potentially invalid pointer +// fn {a:t@ype} bad {l:addr} (p: ptr l): a = !p // doesn't compile + +// oops, dropped the proof (leaked the memory) +// fn {a:t@ype} bad {l:addr} (pf: a@l | p: ptr l): a = !p // doesn't compile + +fn inc_at_ptr {l:addr} (pf: int@l | p: ptr l): (int@l | void) = + let + // !p := value writes value to the location at p + // like !p, it implicitly searches for usable proofs that are in scope + val () = !p := !p + 1 + in (pf | ()) end + +// threading proofs through gets annoying +fn inc_three_times {l:addr} (pf: int@l | p: ptr l): (int@l | void) = + let + val (pf2 | ()) = inc_at_ptr (pf | p) + val (pf3 | ()) = inc_at_ptr (pf2 | p) + val (pf4 | ()) = inc_at_ptr (pf3 | p) + in (pf4 | ()) end + +// so there's special syntactic sugar for when you don't consume a proof +fn dec_at_ptr {l:addr} (pf: !int@l | p: ptr l): void = + !p := !p - 1 // ^ note the exclamation point + +fn dec_three_times {l:addr} (pf: !int@l | p: ptr l): void = + let + val () = dec_at_ptr (pf | p) + val () = dec_at_ptr (pf | p) + val () = dec_at_ptr (pf | p) + in () end + +// dataview is like dataprop, but linear +// A proof that either the address is null, or there is a value there +dataview MaybeNull(a:t@ype, addr) = + | NullPtr(a, null) + | {l:addr | l > null} NonNullPtr(a, l) of (a @ l) + +fn maybe_inc {l:addr} (pf: !MaybeNull(int, l) | p: ptr l): void = + if ptr1_is_null p + then () + else let + // Deconstruct the proof to access the proof of a @ l + prval NonNullPtr(value_exists) = pf + val () = !p := !p + 1 + // Reconstruct it again for the caller + prval () = pf := NonNullPtr(value_exists) + in () end + +// array_v(a,l,n) represents and array of n a's at location l +// this gets compiled into an efficient for loop, since all proofs are erased +fn sum_array {l:addr}{n:nat} (pf: !array_v(int,l,n) | p: ptr l, n: int n): int = + let + fun loop {l:addr}{n:nat} .. ( + pf: !array_v(int,l,n) + | ptr: ptr l, + length: int n, + acc: int + ): int = if length = 0 + then acc + else let + prval (head, rest) = array_v_uncons(pf) + val result = loop(rest | ptr_add(ptr, 1), length - 1, acc + !ptr) + prval () = pf := array_v_cons(head, rest) + in result end + in loop (pf | p, n, 0) end + +// 'var' is used to create stack-allocated (lvalue) variables +val seven: int = let + var res: int = 3 + // they can be modified + val () = res := res + 1 + // addr@ res is a pointer to it, and view@ res is the associated proof + val (pf | ()) = inc_three_times(view@ res | addr@ res) + // need to give back the view before the variable goes out of scope + prval () = view@ res := pf + in res end + +// References let you pass lvalues, like in C++ +fn square (x: &int): void = + x := x * x // they can be modified + +val sixteen: int = let + var res: int = 4 + val () = square res + in res end + +fn inc_at_ref (x: &int): void = + let + // like vars, references have views and addresses + val (pf | ()) = inc_at_ptr(view@ x | addr@ x) + prval () = view@ x := pf + in () end + +// Like ! for views, & references are only legal as argument types +// fn bad (x: &int): &int = x // doesn't compile + +// this takes a proof int n @ l, but returns a proof int (n+1) @ l +// since they're different types, we can't use !int @ l like before +fn refined_inc_at_ptr {n:int}{l:addr} ( + pf: int n @ l | p: ptr l +): (int (n+1) @ l | void) = + let + val () = !p := !p + 1 + in (pf | ()) end + +// special syntactic sugar for returning a proof at a different type +fn refined_dec_at_ptr {n:int}{l:addr} ( + pf: !int n @ l >> int (n-1) @ l | p: ptr l +): void = + !p := !p - 1 + +// legal but very bad code +prfn swap_proofs {v1,v2:view} (a: !v1 >> v2, b: !v2 >> v1): void = + let + prval tmp = a + prval () = a := b + prval () = b := tmp + in () end + +// also works with references +fn refined_square {n:int} (x: &int n >> int (n*n)): void = + x := x * x + +fn replace {a,b:vtype} (dest: &a >> b, src: b): a = + let + val old = dest + val () = dest := src + in old end + +// values can be uninitialized +fn {a:vt@ype} write (place: &a? >> a, value: a): void = + place := value + +val forty: int = let + var res: int + val () = write (res, 40) + in res end + +// viewtype: a view and a type +viewtypedef MaybeNullPtr(a:t@ype) = [l:addr] (MaybeNull(a, l) | ptr l) +// MaybeNullPtr has type 'viewtype' (aka 'vtype') +// type is a subtype of vtype and t@ype is a subtype of vt@ype + +// The most general identity function: +fn {a:vt@ype} id7 (x: a) = x + +// since they contain views, viewtypes must be used linearly +// fn {a:vt@ype} duplicate (x: a) = (x, x) // doesn't compile +// fn {a:vt@ype} ignore (x: a) = () // doesn't compile + +// arrayptr(a,l,n) is a convenient built-in viewtype +fn easier_sum_array {l:addr}{n:nat} (p: !arrayptr(int,l,n), n: int n): int = + let + fun loop {i:nat | i <= n} ( + p: !arrayptr(int,l,n), n: int n, i: int i, acc: int + ): int = + if i = n + then acc + else loop(p, n, i+1, acc + p[i]) + in loop(p, n, 0, 0) end + + +/*************** Part 5: dataviewtypes ****************/ + +// a dataviewtype is a heap-allocated non-shared inductive type + +// in the stdlib: +// dataviewtype list_vt(a:vt@ype, int) = +// | list_vt_nil(a, 0) +// | {n:nat} list_vt_cons(a, n+1) of (a, list_vt(a, n)) + +fn {a:vt@ype} length {n:int} (l: !list_vt(a, n)): int n = + let // ^ since we're not consuming it + fun loop {acc:int} (l: !list_vt(a, n-acc), acc: int acc): int n = + case l of + | list_vt_nil() => acc + | list_vt_cons(head, tail) => loop(tail, acc + 1) + in loop (l, 0) end + +// vvvvv not vt@ype, because you can't easily get rid of a vt@ype +fun {a:t@ype} destroy_list {n:nat} (l: list_vt(a,n)): void = + case l of + // ~ pattern match consumes and frees that node + | ~list_vt_nil() => () + | ~list_vt_cons(_, tail) => destroy_list tail + +// unlike a datatype, a dataviewtype can be modified: +fun {a:vt@ype} push_back {n:nat} ( + x: a, + l: &list_vt(a,n) >> list_vt(a,n+1) +): void = + case l of + | ~list_vt_nil() => l := list_vt_cons(x, list_vt_nil) + // @ pattern match disassembles/"unfolds" the datavtype's view, so you can + // modify its components + | @list_vt_cons(head, tail) => let + val () = push_back (x, tail) + // reassemble it with fold@ + prval () = fold@ l + in () end + +fun {a:vt@ype} pop_last {n:pos} (l: &list_vt(a,n) >> list_vt(a,n-1)): a = + let + val+ @list_vt_cons(head, tail) = l + in case tail of + | list_vt_cons _ => let + val res = pop_last tail + prval () = fold@ l + in res end + | ~list_vt_nil() => let + val head = head + // Deallocate empty datavtype nodes with free@ + val () = free@{..}{0} l + val () = l := list_vt_nil() + in head end + /** Equivalently: + * | ~list_vt_nil() => let + * prval () = fold@ l + * val+ ~list_vt_cons(head, ~list_vt_nil()) = l + * val () = l := list_vt_nil() + * in head end + */ + end + +// "holes" (ie uninitialized memory) can be created with _ on the RHS +// This function uses destination-passing-style to copy the list in a single +// tail-recursive pass. +fn {a:t@ype} copy_list {n:nat} (l: !list_vt(a, n)): list_vt(a, n) = + let + var res: ptr + fun loop {k:nat} (l: !list_vt(a, k), hole: &ptr? >> list_vt(a, k)): void = + case l of + | list_vt_nil() => hole := list_vt_nil + | list_vt_cons(first, rest) => let + val () = hole := list_vt_cons{..}{k-1}(first, _) + // ^ on RHS: a hole + val+list_vt_cons(_, new_hole) = hole + // ^ on LHS: wildcard pattern (not a hole) + val () = loop (rest, new_hole) + prval () = fold@ hole + in () end + val () = loop (l, res) + in res end + +// Reverse a linked-list *in place* -- no allocations or frees +fn {a:vt@ype} in_place_reverse {n:nat} (l: list_vt(a, n)): list_vt(a, n) = + let + fun loop {k:nat} (l: list_vt(a, n-k), acc: list_vt(a, k)): list_vt(a, n) = + case l of + | ~list_vt_nil() => acc + | @list_vt_cons(x, tail) => let + val rest = replace(tail, acc) + // the node 'l' is now part of acc instead of the original list + prval () = fold@ l + in loop (rest, l) end + in loop (l, list_vt_nil) end + + +/*************** Part 6: miscellaneous extras ****************/ + +// a record +// Point has type 't@ype' +typedef Point = @{ x= int, y= int } +val origin: Point = @{ x= 0, y= 0 } + +// tuples and records are normally unboxed, but there are boxed variants +// BoxedPoint has type 'type' +typedef BoxedPoint = '{ x= int, y= int } +val boxed_pair: '(int,int) = '(5, 3) + +// When passing a pair as the single argument to a function, it needs to be +// written @(a,b) to avoid ambiguity with multi-argument functions +val six_plus_seven = let + fun sum_of_pair (pair: (int,int)) = pair.0 + pair.1 + in sum_of_pair @(6, 7) end + +// When a constructor has no associated data, such as None(), the parentheses +// are optional in expressions. However, they are mandatory in patterns +fn inc_option (opt: Option int) = + case opt of + | Some(x) => Some(x+1) + | None() => None + +// ATS has a simple FFI, since it compiles to C and (mostly) uses the C ABI +%{ +// Inline C code +int scanf_wrapper(void *format, void *value) { + return scanf((char *) format, (int *) value); +} +%} +// If you wanted to, you could define a custom dataviewtype more accurately +// describing the result of scanf +extern fn scanf (format: string, value: &int): int = "scanf_wrapper" + +fn get_input_number (): Option int = + let + var x: int = 0 + in + if scanf("%d\n", x) = 1 + then Some(x) + else None + end + +// extern is also used for separate declarations and definitions +extern fn say_hi (): void +// later on, or in another file: +implement say_hi () = print "hi\n" + +// if you implement main0, it will run as the main function +// implmnt is an alias for implement +implmnt main0 () = () + +// as well as for axioms: +extern praxi view_id {a:view} (x: a): a +// you don't need to actually implement the axioms, but you could +primplmnt view_id x = x +// primplmnt is an alias for primplement + +// Some standard aliases are: +// List0(a) = [n:nat] list(a,n) and List0_vt(a) = [n:nat] list_vt(a,n) +// t0p = t@ype and vt0p = vt@ype +fun {a:t0p} append (xs: List0 a, ys: List0 a): List0 a = + case xs of + | list_nil() => ys + | list_cons(x, xs) => list_cons(x, append(xs, ys)) + +// there are many ways of doing things after one another +val let_in_example = let + val () = print "thing one\n" + val () = print "thing two\n" + in () end + +val parens_example = (print "thing one\n"; print "thing two\n") + +val begin_end_example = begin + print "thing one\n"; + print "thing two\n"; // optional trailing semicolon + end + +// and many ways to use local variables +fun times_four_let x = + let + fun times_two (x: int) = x * 2 + in times_two (times_two x) end + +local + fun times_two (x: int) = x * 2 +in + fun times_four_local x = times_two (times_two x) +end + +fun times_four_where x = times_two (times_two x) + where { + fun times_two (x: int) = x * 2 + } + +//// The last kind of comment in ATS is an end-of-file comment. + +Anything between the four slashes and the end of the file is ignored. + +Have fun with ATS! +``` + +## Learn more + +This isn't all there is to ATS -- notably, some core features like closures and +the effect system are left out, as well as other less type-y stuff like modules +and the build system. If you'd like to write these sections yourself, +contributions would be welcome! + +To learn more about ATS, visit the [ATS website](http://www.ats-lang.org/), in +particular the [documentation page](http://www.ats-lang.org/Documents.html). + diff --git a/chicken.html.markdown b/chicken.html.markdown new file mode 100644 index 00000000..37f6c859 --- /dev/null +++ b/chicken.html.markdown @@ -0,0 +1,517 @@ +--- +language: "CHICKEN" +filename: CHICKEN.scm +contributors: + - ["Diwakar Wagle", "https://github.com/deewakar"] +--- + + +CHICKEN is an implementation of Scheme programming language that can +compile Scheme programs to C code as well as interpret them. CHICKEN +supports R5RS and R7RS (work in progress) standards and many extensions. + + +```scheme +;; #!/usr/bin/env csi -s + +;; Run the CHICKEN REPL in the commandline as follows : +;; $ csi + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 0. Syntax +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Single line comments start with a semicolon + +#| Block comments + can span multiple lines and... + #| can be nested + |# +|# + +;; S-expression comments are used to comment out expressions +#; (display "nothing") ; discard this expression + +;; CHICKEN has two fundamental pieces of syntax: Atoms and S-expressions +;; an atom is something that evaluates to itself +;; all builtin data types viz. numbers, chars, booleans, strings etc. are atoms +;; Furthermore an atom can be a symbol, an identifier, a keyword, a procedure +;; or the empty list (also called null) +'athing ;; => athing +'+ ;; => + ++ ;; => + +;; S-expressions (short for symbolic expressions) consists of one or more atoms +(quote +) ;; => + ; another way of writing '+ +(+ 1 2 3) ;; => 6 ; this S-expression evaluates to a function call +'(+ 1 2 3) ;; => (+ 1 2 3) ; evaluates to a list + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 1. Primitive Datatypes and Operators +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Numbers +99999999999999999999 ;; integers +#b1010 ;; binary ; => 10 +#o10 ;; octal ; => 8 +#x8ded ;; hexadecimal ; => 36333 +3.14 ;; real +6.02e+23 +3/4 ;; rational + +;;Characters and Strings +#\A ;; A char +"Hello, World!" ;; strings are fixed-length arrays of characters + +;; Booleans +#t ;; true +#f ;; false + +;; Function call is written as (f x y z ...) +;; where f is a function and x,y,z, ... are arguments +(print "Hello, World!") ;; => Hello, World! +;; formatted output +(printf "Hello, ~a.\n" "World") ;; => Hello, World. + +;; print commandline arguments +(map print (command-line-arguments)) + +(list 'foo 'bar 'baz) ;; => (foo bar baz) +(string-append "pine" "apple") ;; => "pineapple" +(string-ref "tapioca" 3) ;; => #\i;; character 'i' is at index 3 +(string->list "CHICKEN") ;; => (#\C #\H #\I #\C #\K #\E #\N) +(string-intersperse '("1" "2") ":") ;; => "1:2" +(string-split "1:2:3" ":") ;; => ("1" "2" "3") + + +;; Predicates are special functions that return boolean values +(atom? #t) ;; => #t + +(symbol? #t) ;; => #f + +(symbol? '+) ;; => #t + +(procedure? +) ;; => #t + +(pair? '(1 2)) ;; => #t + +(pair? '(1 2 . 3)) ;; => #t + +(pair? '()) ;; => #f + +(list? '()) ;; => #t + + +;; Some arithmetic operations + +(+ 1 1) ;; => 2 +(- 8 1) ;; => 7 +(* 10 2) ;; => 20 +(expt 2 3) ;; => 8 +(remainder 5 2) ;; => 1 +(/ 35 5) ;; => 7 +(/ 1 3) ;; => 0.333333333333333 + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 2. Variables +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; You can create variables with define +;; A variable name can use any character except: ()[]{}",'`;#\ +(define myvar 5) +myvar ;; => 5 + +;; Alias to a procedure +(define ** expt) +(** 2 3) ;; => 8 + +;; Accessing an undefined variable raises an exception +s ;; => Error: unbound variable: s + +;; Local binding +(let ((me "Bob")) + (print me)) ;; => Bob + +(print me) ;; => Error: unbound variable: me + +;; Assign a new value to previously defined variable +(set! myvar 10) +myvar ;; => 10 + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 3. Collections +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Pairs +;; 'cons' constructs pairs, +;; 'car' extracts the first element, 'cdr' extracts the rest of the elements +(cons 'subject 'verb) ;; => '(subject . verb) +(car (cons 'subject 'verb)) ;; => subject +(cdr (cons 'subject 'verb)) ;; => verb + +;; Lists +;; cons creates a new list if the second item is a list +(cons 0 '()) ;; => (0) +(cons 1 (cons 2 (cons 3 '()))) ;; => (1 2 3) +;; 'list' is a convenience variadic constructor for lists +(list 1 2 3) ;; => (1 2 3) + + +;; Use 'append' to append lists together +(append '(1 2) '(3 4)) ;; => (1 2 3 4) + +;; Some basic operations on lists +(map add1 '(1 2 3)) ;; => (2 3 4) +(reverse '(1 3 4 7)) ;; => (7 4 3 1) +(sort '(11 22 33 44) >) ;; => (44 33 22 11) + +(define days '(SUN MON FRI)) +(list-ref days 1) ;; => MON +(set! (list-ref days 1) 'TUE) +days ;; => (SUN TUE FRI) + +;; Vectors +;; Vectors are heterogeneous structures whose elements are indexed by integers +;; A Vector typically occupies less space than a list of the same length +;; Random access of an element in a vector is faster than in a list +#(1 2 3) ;; => #(1 2 3) ;; literal syntax +(vector 'a 'b 'c) ;; => #(a b c) +(vector? #(1 2 3)) ;; => #t +(vector-length #(1 (2) "a")) ;; => 3 +(vector-ref #(1 (2) (3 3)) 2);; => (3 3) + +(define vec #(1 2 3)) +(vector-set! vec 2 4) +vec ;; => #(1 2 4) + +;; Vectors can be created from lists and vice-verca +(vector->list #(1 2 4)) ;; => '(1 2 4) +(list->vector '(a b c)) ;; => #(a b c) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 4. Functions +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Use 'lambda' to create functions. +;; A function always returns the value of its last expression +(lambda () "Hello World") ;; => # + +;; Use extra parens around function definition to execute +((lambda () "Hello World")) ;; => Hello World ;; argument list is empty + +;; A function with an argument +((lambda (x) (* x x)) 3) ;; => 9 +;; A function with two arguments +((lambda (x y) (* x y)) 2 3) ;; => 6 + +;; assign a function to a variable +(define sqr (lambda (x) (* x x))) +sqr ;; => # +(sqr 3) ;; => 9 + +;; We can shorten this using the function definition syntactic sugar +(define (sqr x) (* x x)) +(sqr 3) ;; => 9 + +;; We can redefine existing procedures +(foldl cons '() '(1 2 3 4 5)) ;; => (((((() . 1) . 2) . 3) . 4) . 5) +(define (foldl func accu alist) + (if (null? alist) + accu + (foldl func (func (car alist) accu) (cdr alist)))) + +(foldl cons '() '(1 2 3 4 5)) ;; => (5 4 3 2 1) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 5. Equality +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; For numbers use '=' +(= 3 3.0) ;; => #t +(= 2 1) ;; => #f + +;; 'eq?' returns #t if two arguments refer to the same object in memory +;; In other words, it's a simple pointer comparison. +(eq? '() '()) ;; => #t ;; there's only one empty list in memory +(eq? (list 3) (list 3)) ;; => #f ;; not the same object +(eq? 'yes 'yes) ;; => #t +(eq? 3 3) ;; => #t ;; don't do this even if it works in this case +(eq? 3 3.0) ;; => #f ;; it's better to use '=' for number comparisons +(eq? "Hello" "Hello") ;; => #f + +;; 'eqv?' is same as 'eq?' all datatypes except numbers and characters +(eqv? 3 3.0) ;; => #f +(eqv? (expt 2 3) (expt 2 3)) ;; => #t +(eqv? 'yes 'yes) ;; => #t + +;; 'equal?' recursively compares the contents of pairs, vectors, and strings, +;; applying eqv? on other objects such as numbers and symbols. +;; A rule of thumb is that objects are generally equal? if they print the same. + +(equal? '(1 2 3) '(1 2 3)) ;; => #t +(equal? #(a b c) #(a b c)) ;; => #t +(equal? 'a 'a) ;; => #t +(equal? "abc" "abc") ;; => #t + +;; In Summary: +;; eq? tests if objects are identical +;; eqv? tests if objects are operationally equivalent +;; equal? tests if objects have same structure and contents + +;; Comparing strings for equality +(string=? "Hello" "Hello") ;; => #t + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 6. Control Flow +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Conditionals +(if #t ;; test expression + "True" ;; then expression + "False") ;; else expression + ;; => "True" + +(if (> 3 2) + "yes" + "no") ;; => "yes" + +;; In conditionals, all values that are not '#f' are treated as true. +;; 0, '(), #() "" , are all true values +(if 0 + "0 is not false" + "0 is false") ;; => "0 is not false" + +;; 'cond' chains a series of tests and returns as soon as it encounters a true condition +;; 'cond' can be used to simulate 'if/elseif/else' statements +(cond ((> 2 2) "not true so don't return this") + ((< 2 5) "true, so return this") + (else "returning default")) ;; => "true, so return this" + + +;; A case expression is evaluated as follows: +;; The key is evaluated and compared with each datum in sense of 'eqv?', +;; The corresponding clause in the matching datum is evaluated and returned as result +(case (* 2 3) ;; the key is 6 + ((2 3 5 7) 'prime) ;; datum 1 + ((1 4 6 8) 'composite)) ;; datum 2; matched! + ;; => composite + +;; case with else clause +(case (car '(c d)) + ((a e i o u) 'vowel) + ((w y) 'semivowel) + (else 'consonant)) ;; => consonant + +;; Boolean expressions +;; 'and' returns the first expression that evaluates to #f +;; otherwise, it returns the result of the last expression +(and #t #f (= 2 2.0)) ;; => #f +(and (< 2 5) (> 2 0) "0 < 2 < 5") ;; => "0 < 2 < 5" + +;; 'or' returns the first expression that evaluates to #t +;; otherwise the result of the last expression is returned +(or #f #t #f) ;; => #t +(or #f #f #f) ;; => #f + +;; 'when' is like 'if' without the else expression +(when (positive? 5) "I'm positive") ;; => "I'm positive" + +;; 'unless' is equivalent to (when (not ) ) +(unless (null? '(1 2 3)) "not null") ;; => "not null" + + +;; Loops +;; loops can be created with the help of tail-recursions +(define (loop count) + (unless (= count 0) + (print "hello") + (loop (sub1 count)))) +(loop 4) ;; => hello, hello ... + +;; Or with a named let +(let loop ((i 0) (limit 5)) + (when (< i limit) + (printf "i = ~a\n" i) + (loop (add1 i) limit))) ;; => i = 0, i = 1.... + +;; 'do' is another iteration construct +;; It initializes a set of variables and updates them in each iteration +;; A final expression is evaluated after the exit condition is met +(do ((x 0 (add1 x ))) ;; initialize x = 0 and add 1 in each iteration + ((= x 10) (print "done")) ;; exit condition and final expression + (print x)) ;; command to execute in each step + ;; => 0,1,2,3....9,done + +;; Iteration over lists +(for-each (lambda (a) (print (* a a))) + '(3 5 7)) ;; => 9, 25, 49 + +;; 'map' is like for-each but returns a list +(map add1 '(11 22 33)) ;; => (12 23 34) + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 7. Extensions +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; The CHICKEN core is very minimal, but additional features are provided by library extensions known as Eggs. +;; You can install Eggs with 'chicken-install ' command. + +;; complex numbers +3+4i ;; => 3+2i +;; Supports fractions without falling back to inexact flonums +1/3 ;; => 1/3 +;; provides support for large integers through bignums +(expt 9 20) ;; => 12157665459056928801 +;; And other 'extended' functions +(log 10 (exp 1)) ;; => 2.30258509299405 +(numerator 2/3) ;; => 2 + +;; 'utf8' provides unicode support +(import utf8) +"\u03BBx:(\u03BC\u0251.\u0251\u2192\u0251).xx" ;; => "λx:(μɑ.ɑ→ɑ).xx" + +;; 'posix' provides file I/O and lots of other services for unix-like operating systems +;; Some of the functions are not available in Windows system, +;; See http://wiki.call-cc.org/man/5/Module%20(chicken%20file%20posix) for more details + +;; Open a file to append, open "write only" and create file if it does not exist +(define outfn (file-open "chicken-hen.txt" (+ open/append open/wronly open/creat))) +;; write some text to the file +(file-write outfn "Did chicken came before hen?") +;; close the file +(file-close outfn) +;; Open the file "read only" +(define infn (file-open "chicken-hen.txt" open/rdonly)) +;; read some text from the file +(file-read infn 30) ;; => ("Did chicken came before hen? ", 28) +(file-close infn) + +;; CHICKEN also supports SRFI (Scheme Requests For Implementation) extensions +;; See 'http://srfi.schemers.org/srfi-implementers.html" to see srfi's supported by CHICKEN +(import srfi-1) ;; list library +(filter odd? '(1 2 3 4 5 6 7)) ;; => (1 3 5 7) +(count even? '(1 2 3 4 5)) ;; => 2 +(take '(12 24 36 48 60) 3) ;; => (12 24 36) +(drop '(12 24 36 48 60) 2) ;; => (36 48 60) +(circular-list 'z 'q) ;; => z q z q ... + +(import srfi-13) ;; string library +(string-reverse "pan") ;; => "nap" +(string-index "Turkey" #\k) ;; => 3 +(string-every char-upper-case? "CHICKEN") ;; => #t +(string-join '("foo" "bar" "baz") ":") ;; => "foo:bar:baz" + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 8. Macros +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; A 'for .. in ..' iteration like python, for lists +(define-syntax for + (syntax-rules (in) + ((for elem in alist body ...) + (for-each (lambda (elem) body ...) alist)))) + +(for x in '(2 4 8 16) + (print x)) ;; => 2, 4, 8, 16 + +(for chr in (string->list "PENCHANT") + (print chr)) ;; => P, E, N, C, H, A, N, T + +;; While loop +(define-syntax while + (syntax-rules () + ((while cond body ...) + (let loop () + (when cond + body ... + (loop)))))) + +(let ((str "PENCHANT") (i 0)) + (while (< i (string-length str)) ;; while (condition) + (print (string-ref str i)) ;; body + (set! i (add1 i)))) + ;; => P, E, N, C, H, A, N, T + +;; Advanced Syntax-Rules Primer -> http://petrofsky.org/src/primer.txt +;; Macro system in chicken -> http://lists.gnu.org/archive/html/chicken-users/2008-04/msg00013.html + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +; 9. Modules +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +;; Also See http://wiki.call-cc.org/man/5/Modules + +;; The 'test' module exports a value named 'hello' and a macro named 'greet' +(module test (hello greet) + (import scheme) + + (define-syntax greet + (syntax-rules () + ((_ whom) + (begin + (display "Hello, ") + (display whom) + (display " !\n") ) ) ) ) + + (define (hello) + (greet "world") ) ) + +;; we can define our modules in a separate file (say test.scm) and load them to the interpreter with +;; (load "test.scm") + +;; import the module +(import test) +(hello) ;; => Hello, world ! +(greet "schemers") ;; => Hello, schemers ! + +;; We can compile the module files in to shared libraries by using following command, +;; csc -s test.scm +;; (load "test.so") + +;; Functors +;; Functors are high level modules that can be parameterized by other modules +;; Following functor requires another module named 'M' that provides a function called 'multiply' +;; The functor itself exports a generic function 'square' +(functor (squaring-functor (M (multiply))) (square) + (import scheme M) + (define (square x) (multiply x x))) + +;; Module 'nums' can be passed as a parameter to 'squaring-functor' +(module nums (multiply) + (import scheme) ;; predefined modules + (define (multiply x y) (* x y))) +;; the final module can be imported and used in our program +(module number-squarer = (squaring-functor nums)) + +(import number-squarer) +(square 3) ;; => 9 + +;; We can instantiate the functor for other inputs +;; Here's another example module that can be passed to squaring-functor +(module stars (multiply) + (import chicken scheme) ;; chicken module for the 'use' keyword + (use srfi-1) ;; we can use external libraries in our module + (define (multiply x y) + (list-tabulate x (lambda _ (list-tabulate y (lambda _ '*)))))) +(module star-squarer = (squaring-functor stars)) + +(import star-squarer) +(square 3) ;; => ((* * *)(* * *)(* * *)) +``` + +## Further Reading +* [CHICKEN User's Manual](https://wiki.call-cc.org/manual). +* [R5RS standards](http://www.schemers.org/Documents/Standards/R5RS) + + +## Extra Info + +* [For programmers of other languages](https://wiki.call-cc.org/chicken-for-programmers-of-other-languages) +* [Compare CHICKEN syntax with other languages](http://plr.sourceforge.net/cgi-bin/plr/launch.py) diff --git a/de-de/LOLCODE-de.html.markdown b/de-de/LOLCODE-de.html.markdown deleted file mode 100644 index 57eb0ff8..00000000 --- a/de-de/LOLCODE-de.html.markdown +++ /dev/null @@ -1,188 +0,0 @@ ---- -language: LOLCODE -filename: learnLOLCODE-de.lol -contributors: - - ["abactel", "https://github.com/abactel"] -translators: - - ["Henrik Jürges", "http://github.com/santifa"] -lang: de-de ---- - -LOLCODE ist eine esoterische Programmiersprache die die Sprache der [lolcats](https://upload.wikimedia.org/wikipedia/commons/a/ab/Lolcat_in_folder.jpg?1493656347257) nachahmt. - -``` -BTW Das ist ein Kommentar -BTW Das Programm muss mit `HAI ` beginnen und mit `KTHXBYE` enden. - -HAI 1.3 -CAN HAS STDIO? BTW Standard Header importieren - -OBTW - ========================================================================== - ============================== Grundlegendes ============================= - ========================================================================== -TLDR - -BTW Texte anzeigen: -VISIBLE "HELLO WORLD" - -BTW Variablen deklarieren: -I HAS A MESSAGE ITZ "CATZ ARE GOOD" -VISIBLE MESSAGE - -OBTW - Variablen sind dynamisch typisiert und der Typ muss nicht explizit - angegeben werden. Die möglichen Typen sind: -TLDR - -I HAS A STRING ITZ "DOGZ ARE GOOOD" BTW Typ ist YARN -I HAS A INTEGER ITZ 42 BTW Typ ist NUMBR -I HAS A FLOAT ITZ 3.1415 BTW Typ ist NUMBAR -I HAS A BOOLEAN ITZ WIN BTW Typ ist TROOF -I HAS A UNTYPED BTW Typ ist NOOB - -BTW Eingaben von Nutzern: -I HAS A AGE -GIMMEH AGE -BTW Die Variable wird als YARN gespeichert und kann in eine -BTW NUMBR konvertiert werden: -AGE IS NOW A NUMBR - -OBTW - ========================================================================== - ================================== MATHE ================================= - ========================================================================== -TLDR - -BTW LOLCODE benutzt polnische Notation für Mathe. - -BTW grundlegende mathematische Notationen: - -SUM OF 21 AN 33 BTW 21 + 33 -DIFF OF 90 AN 10 BTW 90 - 10 -PRODUKT OF 12 AN 13 BTW 12 * 13 -QUOSHUNT OF 32 AN 43 BTW 32 / 43 -MOD OF 43 AN 64 BTW 43 modulo 64 -BIGGR OF 23 AN 53 BTW max(23, 53) -SMALLR OF 53 AN 45 BTW min(53, 45) - -BTW binäre Notation: - -BOTH OF WIN AN WIN BTW und: WIN if x=WIN, y=WIN -EITHER OF FAIL AN WIN BTW oder: FAIL if x=FAIL, y=FAIL -WON OF WIN AN FAIL BTW exklusives oder: FAIL if x=y -NOT FAIL BTW unäre Negation: WIN if x=FAIL -ALL OF WIN AN WIN MKAY BTW beliebige Stelligkeit bei AND -ANY OF WIN AN FAIL MKAY BTW beliebige Stelligkeit bei OR - -BTW Vergleiche: - -BOTH SAEM "CAT" AN "DOG" BTW WIN wenn x == y -DIFFRINT 732 AN 184 BTW WIN wenn x != y -BOTH SAEM 12 AN BIGGR OF 12 AN 4 BTW x >= y -BOTH SAEM 43 AN SMALLR OF 43 AN 56 BTW x <= y -DIFFRINT 64 AN SMALLR OF 64 AN 2 BTW x > y -DIFFRINT 75 AN BIGGR OF 75 AN 643 BTW x < y - -OBTW - ========================================================================== - ============================= Flusskontrolle ============================= - ========================================================================== -TLDR - -BTW If/then Statement: -I HAS A ANIMAL -GIMMEH ANIMAL -BOTH SAEM ANIMAL AN "CAT", O RLY? - YA RLY - VISIBLE "YOU HAV A CAT" - MEBBE BOTH SAEM ANIMAL AN "MAUS" - VISIBLE "NOM NOM NOM. I EATED IT." - NO WAI - VISIBLE "AHHH IS A WOOF WOOF" -OIC - -BTW Case Statement: -I HAS A COLOR -GIMMEH COLOR -COLOR, WTF? - OMG "R" - VISIBLE "RED FISH" - GTFO - OMG "Y" - VISIBLE "YELLOW FISH" - BTW Weil hier kein `GTFO` ist wird auch das nächste Statement überprüft - OMG "G" - OMG "B" - VISIBLE "FISH HAS A FLAVOR" - GTFO - OMGWTF - VISIBLE "FISH IS TRANSPARENT OHNO WAT" -OIC - -BTW For Schleife: -I HAS A TEMPERATURE -GIMMEH TEMPERATURE -TEMPERATURE IS NOW A NUMBR -IM IN YR LOOP UPPIN YR ITERATOR TIL BOTH SAEM ITERATOR AN TEMPERATURE - VISIBLE ITERATOR -IM OUTTA YR LOOP - -BTW While Schleife: -IM IN YR LOOP NERFIN YR ITERATOR WILE DIFFRINT ITERATOR AN -10 - VISIBLE ITERATOR -IM OUTTA YR LOOP - -OBTW - ========================================================================= - ================================ Strings ================================ - ========================================================================= -TLDR - -BTW Zeilenumbrüche: -VISIBLE "FIRST LINE :) SECOND LINE" - -BTW Tabulatoren: -VISIBLE ":>SPACES ARE SUPERIOR" - -BTW Bell (macht beep): -VISIBLE "NXT CUSTOMER PLS :o" - -BTW Anführungszeichen in Strings: -VISIBLE "HE SAID :"I LIKE CAKE:"" - -BTW Doppelpunkte in Strings : -VISIBLE "WHERE I LIVE:: CYBERSPACE" - -OBTW - ========================================================================= - =============================== Funktionen ============================== - ========================================================================= -TLDR - -BTW Definieren einer neuen Funktion: -HOW IZ I SELECTMOVE YR MOVE BTW `MOVE` ist ein Argument - BOTH SAEM MOVE AN "ROCK", O RLY? - YA RLY - VISIBLE "YOU HAV A ROCK" - NO WAI - VISIBLE "OH NO IS A SNIP-SNIP" - OIC - GTFO BTW Gibt NOOB zurück -IF U SAY SO - -BTW Eine Funktion deklarieren und einen Wert zurückgeben: -HOW IZ I IZYELLOW - FOUND YR "YELLOW" -IF U SAY SO - -BTW Eine Funktion aufrufen: -I IZ IZYELLOW MKAY - -KTHXBYE -``` - -## Weiterführende Informationen: - -- [LCI compiler](https://github.com/justinmeza/lci) -- [Official spec](https://github.com/justinmeza/lolcode-spec/blob/master/v1.2/lolcode-spec-v1.2.md) diff --git a/de-de/lolcode-de.html.markdown b/de-de/lolcode-de.html.markdown new file mode 100644 index 00000000..57eb0ff8 --- /dev/null +++ b/de-de/lolcode-de.html.markdown @@ -0,0 +1,188 @@ +--- +language: LOLCODE +filename: learnLOLCODE-de.lol +contributors: + - ["abactel", "https://github.com/abactel"] +translators: + - ["Henrik Jürges", "http://github.com/santifa"] +lang: de-de +--- + +LOLCODE ist eine esoterische Programmiersprache die die Sprache der [lolcats](https://upload.wikimedia.org/wikipedia/commons/a/ab/Lolcat_in_folder.jpg?1493656347257) nachahmt. + +``` +BTW Das ist ein Kommentar +BTW Das Programm muss mit `HAI ` beginnen und mit `KTHXBYE` enden. + +HAI 1.3 +CAN HAS STDIO? BTW Standard Header importieren + +OBTW + ========================================================================== + ============================== Grundlegendes ============================= + ========================================================================== +TLDR + +BTW Texte anzeigen: +VISIBLE "HELLO WORLD" + +BTW Variablen deklarieren: +I HAS A MESSAGE ITZ "CATZ ARE GOOD" +VISIBLE MESSAGE + +OBTW + Variablen sind dynamisch typisiert und der Typ muss nicht explizit + angegeben werden. Die möglichen Typen sind: +TLDR + +I HAS A STRING ITZ "DOGZ ARE GOOOD" BTW Typ ist YARN +I HAS A INTEGER ITZ 42 BTW Typ ist NUMBR +I HAS A FLOAT ITZ 3.1415 BTW Typ ist NUMBAR +I HAS A BOOLEAN ITZ WIN BTW Typ ist TROOF +I HAS A UNTYPED BTW Typ ist NOOB + +BTW Eingaben von Nutzern: +I HAS A AGE +GIMMEH AGE +BTW Die Variable wird als YARN gespeichert und kann in eine +BTW NUMBR konvertiert werden: +AGE IS NOW A NUMBR + +OBTW + ========================================================================== + ================================== MATHE ================================= + ========================================================================== +TLDR + +BTW LOLCODE benutzt polnische Notation für Mathe. + +BTW grundlegende mathematische Notationen: + +SUM OF 21 AN 33 BTW 21 + 33 +DIFF OF 90 AN 10 BTW 90 - 10 +PRODUKT OF 12 AN 13 BTW 12 * 13 +QUOSHUNT OF 32 AN 43 BTW 32 / 43 +MOD OF 43 AN 64 BTW 43 modulo 64 +BIGGR OF 23 AN 53 BTW max(23, 53) +SMALLR OF 53 AN 45 BTW min(53, 45) + +BTW binäre Notation: + +BOTH OF WIN AN WIN BTW und: WIN if x=WIN, y=WIN +EITHER OF FAIL AN WIN BTW oder: FAIL if x=FAIL, y=FAIL +WON OF WIN AN FAIL BTW exklusives oder: FAIL if x=y +NOT FAIL BTW unäre Negation: WIN if x=FAIL +ALL OF WIN AN WIN MKAY BTW beliebige Stelligkeit bei AND +ANY OF WIN AN FAIL MKAY BTW beliebige Stelligkeit bei OR + +BTW Vergleiche: + +BOTH SAEM "CAT" AN "DOG" BTW WIN wenn x == y +DIFFRINT 732 AN 184 BTW WIN wenn x != y +BOTH SAEM 12 AN BIGGR OF 12 AN 4 BTW x >= y +BOTH SAEM 43 AN SMALLR OF 43 AN 56 BTW x <= y +DIFFRINT 64 AN SMALLR OF 64 AN 2 BTW x > y +DIFFRINT 75 AN BIGGR OF 75 AN 643 BTW x < y + +OBTW + ========================================================================== + ============================= Flusskontrolle ============================= + ========================================================================== +TLDR + +BTW If/then Statement: +I HAS A ANIMAL +GIMMEH ANIMAL +BOTH SAEM ANIMAL AN "CAT", O RLY? + YA RLY + VISIBLE "YOU HAV A CAT" + MEBBE BOTH SAEM ANIMAL AN "MAUS" + VISIBLE "NOM NOM NOM. I EATED IT." + NO WAI + VISIBLE "AHHH IS A WOOF WOOF" +OIC + +BTW Case Statement: +I HAS A COLOR +GIMMEH COLOR +COLOR, WTF? + OMG "R" + VISIBLE "RED FISH" + GTFO + OMG "Y" + VISIBLE "YELLOW FISH" + BTW Weil hier kein `GTFO` ist wird auch das nächste Statement überprüft + OMG "G" + OMG "B" + VISIBLE "FISH HAS A FLAVOR" + GTFO + OMGWTF + VISIBLE "FISH IS TRANSPARENT OHNO WAT" +OIC + +BTW For Schleife: +I HAS A TEMPERATURE +GIMMEH TEMPERATURE +TEMPERATURE IS NOW A NUMBR +IM IN YR LOOP UPPIN YR ITERATOR TIL BOTH SAEM ITERATOR AN TEMPERATURE + VISIBLE ITERATOR +IM OUTTA YR LOOP + +BTW While Schleife: +IM IN YR LOOP NERFIN YR ITERATOR WILE DIFFRINT ITERATOR AN -10 + VISIBLE ITERATOR +IM OUTTA YR LOOP + +OBTW + ========================================================================= + ================================ Strings ================================ + ========================================================================= +TLDR + +BTW Zeilenumbrüche: +VISIBLE "FIRST LINE :) SECOND LINE" + +BTW Tabulatoren: +VISIBLE ":>SPACES ARE SUPERIOR" + +BTW Bell (macht beep): +VISIBLE "NXT CUSTOMER PLS :o" + +BTW Anführungszeichen in Strings: +VISIBLE "HE SAID :"I LIKE CAKE:"" + +BTW Doppelpunkte in Strings : +VISIBLE "WHERE I LIVE:: CYBERSPACE" + +OBTW + ========================================================================= + =============================== Funktionen ============================== + ========================================================================= +TLDR + +BTW Definieren einer neuen Funktion: +HOW IZ I SELECTMOVE YR MOVE BTW `MOVE` ist ein Argument + BOTH SAEM MOVE AN "ROCK", O RLY? + YA RLY + VISIBLE "YOU HAV A ROCK" + NO WAI + VISIBLE "OH NO IS A SNIP-SNIP" + OIC + GTFO BTW Gibt NOOB zurück +IF U SAY SO + +BTW Eine Funktion deklarieren und einen Wert zurückgeben: +HOW IZ I IZYELLOW + FOUND YR "YELLOW" +IF U SAY SO + +BTW Eine Funktion aufrufen: +I IZ IZYELLOW MKAY + +KTHXBYE +``` + +## Weiterführende Informationen: + +- [LCI compiler](https://github.com/justinmeza/lci) +- [Official spec](https://github.com/justinmeza/lolcode-spec/blob/master/v1.2/lolcode-spec-v1.2.md) diff --git a/fr-fr/HTML-fr.html.markdown b/fr-fr/HTML-fr.html.markdown deleted file mode 100644 index 9ec04420..00000000 --- a/fr-fr/HTML-fr.html.markdown +++ /dev/null @@ -1,117 +0,0 @@ ---- -language: html -filename: learnhtml-fr.html -contributors: - - ["Christophe THOMAS", "https://github.com/WinChris"] -lang: fr-fr ---- - -HTML signifie HyperText Markup Language. -C'est un langage (format de fichiers) qui permet d'écrire des pages internet. -C’est un langage de balisage, il nous permet d'écrire des pages HTML au moyen de balises (Markup, en anglais). -Les fichiers HTML sont en réalité de simple fichier texte. -Qu'est-ce que le balisage ? C'est une façon de hiérarchiser ses données en les entourant par une balise ouvrante et une balise fermante. -Ce balisage sert à donner une signification au texte ainsi entouré. -Comme tous les autres langages, HTML a plusieurs versions. Ici, nous allons parlons de HTML5. - -**NOTE :** Vous pouvez tester les différentes balises que nous allons voir au fur et à mesure du tutoriel sur des sites comme [codepen](http://codepen.io/pen/) afin de voir les résultats, comprendre, et vous familiariser avec le langage. -Cet article porte principalement sur la syntaxe et quelques astuces. - - -```html - - - - - - - - - - - Mon Site - - -

Hello, world!

-
Venez voir ce que ça donne -

Ceci est un paragraphe

-

Ceci est un autre paragraphe

- - - - - - - - - - - - - - - - - - - - - Mon Site - - - - - - - -

Hello, world!

- - Venez voir ce que ça donne -

Ceci est un paragraphe

-

Ceci est un autre paragraphe

- - - - - - - - - - - - - - - - - - - - - - - - - - -
First Header Second Header
Première ligne, première cellule Première ligne, deuxième cellule
Deuxième ligne, première celluleDeuxième ligne, deuxième cellule
-``` - -## Utilisation - -Le HTML s'écrit dans des fichiers `.html`. - -## En savoir plus - -* [Tutoriel HTML](http://slaout.linux62.org/html_css/html.html) -* [W3School](http://www.w3schools.com/html/html_intro.asp) diff --git a/fr-fr/html-fr.html.markdown b/fr-fr/html-fr.html.markdown new file mode 100644 index 00000000..9ec04420 --- /dev/null +++ b/fr-fr/html-fr.html.markdown @@ -0,0 +1,117 @@ +--- +language: html +filename: learnhtml-fr.html +contributors: + - ["Christophe THOMAS", "https://github.com/WinChris"] +lang: fr-fr +--- + +HTML signifie HyperText Markup Language. +C'est un langage (format de fichiers) qui permet d'écrire des pages internet. +C’est un langage de balisage, il nous permet d'écrire des pages HTML au moyen de balises (Markup, en anglais). +Les fichiers HTML sont en réalité de simple fichier texte. +Qu'est-ce que le balisage ? C'est une façon de hiérarchiser ses données en les entourant par une balise ouvrante et une balise fermante. +Ce balisage sert à donner une signification au texte ainsi entouré. +Comme tous les autres langages, HTML a plusieurs versions. Ici, nous allons parlons de HTML5. + +**NOTE :** Vous pouvez tester les différentes balises que nous allons voir au fur et à mesure du tutoriel sur des sites comme [codepen](http://codepen.io/pen/) afin de voir les résultats, comprendre, et vous familiariser avec le langage. +Cet article porte principalement sur la syntaxe et quelques astuces. + + +```html + + + + + + + + + + + Mon Site + + +

Hello, world!

+ Venez voir ce que ça donne +

Ceci est un paragraphe

+

Ceci est un autre paragraphe

+ + + + + + + + + + + + + + + + + + + + + Mon Site + + + + + + + +

Hello, world!

+ + Venez voir ce que ça donne +

Ceci est un paragraphe

+

Ceci est un autre paragraphe

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
First Header Second Header
Première ligne, première cellule Première ligne, deuxième cellule
Deuxième ligne, première celluleDeuxième ligne, deuxième cellule
+``` + +## Utilisation + +Le HTML s'écrit dans des fichiers `.html`. + +## En savoir plus + +* [Tutoriel HTML](http://slaout.linux62.org/html_css/html.html) +* [W3School](http://www.w3schools.com/html/html_intro.asp) diff --git a/lolcode.html.markdown b/lolcode.html.markdown new file mode 100644 index 00000000..217b7b7d --- /dev/null +++ b/lolcode.html.markdown @@ -0,0 +1,185 @@ +--- +language: LOLCODE +filename: learnLOLCODE.lol +contributors: + - ["abactel", "https://github.com/abactel"] +--- + +LOLCODE is an esoteric programming language designed to resemble the speech of [lolcats](https://upload.wikimedia.org/wikipedia/commons/a/ab/Lolcat_in_folder.jpg?1493656347257). + +``` +BTW This is an inline comment +BTW All code must begin with `HAI ` and end with `KTHXBYE` + +HAI 1.3 +CAN HAS STDIO? BTW Importing standard headers + +OBTW + ========================================================================== + ================================= BASICS ================================= + ========================================================================== +TLDR + +BTW Displaying text: +VISIBLE "HELLO WORLD" + +BTW Declaring variables: +I HAS A MESSAGE ITZ "CATZ ARE GOOD" +VISIBLE MESSAGE + +OBTW + (This is a codeblock.) Variables are dynamically typed so you don't need to + declare their type. A variable's type matches its content. These are the + types: +TLDR + +I HAS A STRING ITZ "DOGZ ARE GOOOD" BTW type is YARN +I HAS A INTEGER ITZ 42 BTW type is NUMBR +I HAS A FLOAT ITZ 3.1415 BTW type is NUMBAR +I HAS A BOOLEAN ITZ WIN BTW type is TROOF +I HAS A UNTYPED BTW type is NOOB + +BTW Accepting user input: +I HAS A AGE +GIMMEH AGE +BTW The variable is stored as a YARN. To convert it into NUMBR: +AGE IS NOW A NUMBR + +OBTW + ========================================================================== + ================================== MATH ================================== + ========================================================================== +TLDR + +BTW LOLCODE uses polish notation style math. + +BTW Basic mathematical notation: + +SUM OF 21 AN 33 BTW 21 + 33 +DIFF OF 90 AN 10 BTW 90 - 10 +PRODUKT OF 12 AN 13 BTW 12 * 13 +QUOSHUNT OF 32 AN 43 BTW 32 / 43 +MOD OF 43 AN 64 BTW 43 modulo 64 +BIGGR OF 23 AN 53 BTW max(23, 53) +SMALLR OF 53 AN 45 BTW min(53, 45) + +BTW Binary notation: + +BOTH OF WIN AN WIN BTW and: WIN if x=WIN, y=WIN +EITHER OF FAIL AN WIN BTW or: FAIL if x=FAIL, y=FAIL +WON OF WIN AN FAIL BTW xor: FAIL if x=y +NOT FAIL BTW unary negation: WIN if x=FAIL +ALL OF WIN AN WIN MKAY BTW infinite arity AND +ANY OF WIN AN FAIL MKAY BTW infinite arity OR + +BTW Comparison: + +BOTH SAEM "CAT" AN "DOG" BTW WIN if x == y +DIFFRINT 732 AN 184 BTW WIN if x != y +BOTH SAEM 12 AN BIGGR OF 12 AN 4 BTW x >= y +BOTH SAEM 43 AN SMALLR OF 43 AN 56 BTW x <= y +DIFFRINT 64 AN SMALLR OF 64 AN 2 BTW x > y +DIFFRINT 75 AN BIGGR OF 75 AN 643 BTW x < y + +OBTW + ========================================================================== + ============================== FLOW CONTROL ============================== + ========================================================================== +TLDR + +BTW If/then statement: +I HAS A ANIMAL +GIMMEH ANIMAL +BOTH SAEM ANIMAL AN "CAT", O RLY? + YA RLY + VISIBLE "YOU HAV A CAT" + MEBBE BOTH SAEM ANIMAL AN "MAUS" + VISIBLE "NOM NOM NOM. I EATED IT." + NO WAI + VISIBLE "AHHH IS A WOOF WOOF" +OIC + +BTW Case statement: +I HAS A COLOR +GIMMEH COLOR +COLOR, WTF? + OMG "R" + VISIBLE "RED FISH" + GTFO + OMG "Y" + VISIBLE "YELLOW FISH" + BTW Since there is no `GTFO` the next statements will also be tested + OMG "G" + OMG "B" + VISIBLE "FISH HAS A FLAVOR" + GTFO + OMGWTF + VISIBLE "FISH IS TRANSPARENT OHNO WAT" +OIC + +BTW For loop: +I HAS A TEMPERATURE +GIMMEH TEMPERATURE +TEMPERATURE IS NOW A NUMBR +IM IN YR LOOP UPPIN YR ITERATOR TIL BOTH SAEM ITERATOR AN TEMPERATURE + VISIBLE ITERATOR +IM OUTTA YR LOOP + +BTW While loop: +IM IN YR LOOP NERFIN YR ITERATOR WILE DIFFRINT ITERATOR AN -10 + VISIBLE ITERATOR +IM OUTTA YR LOOP + +OBTW + ========================================================================= + ================================ Strings ================================ + ========================================================================= +TLDR + +BTW Linebreaks: +VISIBLE "FIRST LINE :) SECOND LINE" + +BTW Tabs: +VISIBLE ":>SPACES ARE SUPERIOR" + +BTW Bell (goes beep): +VISIBLE "NXT CUSTOMER PLS :o" + +BTW Literal double quote: +VISIBLE "HE SAID :"I LIKE CAKE:"" + +BTW Literal colon: +VISIBLE "WHERE I LIVE:: CYBERSPACE" + +OBTW + ========================================================================= + =============================== FUNCTIONS =============================== + ========================================================================= +TLDR + +BTW Declaring a new function: +HOW IZ I SELECTMOVE YR MOVE BTW `MOVE` is an argument + BOTH SAEM MOVE AN "ROCK", O RLY? + YA RLY + VISIBLE "YOU HAV A ROCK" + NO WAI + VISIBLE "OH NO IS A SNIP-SNIP" + OIC + GTFO BTW This returns NOOB +IF U SAY SO + +BTW Declaring a function and returning a value: +HOW IZ I IZYELLOW + FOUND YR "YELLOW" +IF U SAY SO + +BTW Calling a function: +I IZ IZYELLOW MKAY + +KTHXBYE +``` + +## Further reading: + +- [LCI compiler](https://github.com/justinmeza/lci) +- [Official spec](https://github.com/justinmeza/lolcode-spec/blob/master/v1.2/lolcode-spec-v1.2.md) diff --git a/no-nb/JSON-no.html.markdown b/no-nb/JSON-no.html.markdown deleted file mode 100644 index 6c8c3c79..00000000 --- a/no-nb/JSON-no.html.markdown +++ /dev/null @@ -1,60 +0,0 @@ ---- -language: json -filename: learnjson-no.json -lang: no-nb -contributors: - - ["Ole Mathias Heggem", "https://github.com/msbone"] - - ["Anna Harren", "https://github.com/iirelu"] - - ["Marco Scannadinari", "https://github.com/marcoms"] ---- - -JSON er en enkel tekstbasert standard for datautveksling. -Den er opprinnelig avledet fra JavaScript for å representere enkle datastrukturer. -Standarden er imidlertid uavhengig av JavaScript eller andre programmeringsspråk. - -JSON i sin reneste form har ingen faktiske kommentarer, men de fleste parsere vil akseptere -C-stil (`//`, `/* */`) kommentarer. - -```json -{ - "nøkkel": "verdi", - - "nøkler": "må alltid være i doble anførselstegn", - "tall": 0, - "strings": "Hellø, wørld. Alt unicode er godkjent, også \"escaping\".", - "har bools?": true, - "ingenting": null, - - "stort tall": 1.2e+100, - - "objekt": { - "kommentar": "Meste av strukturen kommer ifra objekt.", - - "array": [0, 1, 2, 3, "Arrays kan inneholde alt.", 5], - - "nytt object": { - "comment": "Ny kommentar" - } - }, - - "tull": [ - { - "Kilde til Kalium": ["bananer"] - }, - [ - [1, 0, 0, 0], - [0, 1, 0, 0], - [0, 0, 1, "neo"], - [0, 0, 0, 1] - ] - ], - - "Alternativ": { - "Kommentar": "Sjekk ut ditta!" - , "plassering av komma": "Sålenge den er før verdien er det gyldig" - , "Enda en kommentar": "TØFT!" - }, - - "Ferdig": "Da er den korte innledninga til JSON ferdig" -} -``` diff --git a/no-nb/json-no.html.markdown b/no-nb/json-no.html.markdown new file mode 100644 index 00000000..6c8c3c79 --- /dev/null +++ b/no-nb/json-no.html.markdown @@ -0,0 +1,60 @@ +--- +language: json +filename: learnjson-no.json +lang: no-nb +contributors: + - ["Ole Mathias Heggem", "https://github.com/msbone"] + - ["Anna Harren", "https://github.com/iirelu"] + - ["Marco Scannadinari", "https://github.com/marcoms"] +--- + +JSON er en enkel tekstbasert standard for datautveksling. +Den er opprinnelig avledet fra JavaScript for å representere enkle datastrukturer. +Standarden er imidlertid uavhengig av JavaScript eller andre programmeringsspråk. + +JSON i sin reneste form har ingen faktiske kommentarer, men de fleste parsere vil akseptere +C-stil (`//`, `/* */`) kommentarer. + +```json +{ + "nøkkel": "verdi", + + "nøkler": "må alltid være i doble anførselstegn", + "tall": 0, + "strings": "Hellø, wørld. Alt unicode er godkjent, også \"escaping\".", + "har bools?": true, + "ingenting": null, + + "stort tall": 1.2e+100, + + "objekt": { + "kommentar": "Meste av strukturen kommer ifra objekt.", + + "array": [0, 1, 2, 3, "Arrays kan inneholde alt.", 5], + + "nytt object": { + "comment": "Ny kommentar" + } + }, + + "tull": [ + { + "Kilde til Kalium": ["bananer"] + }, + [ + [1, 0, 0, 0], + [0, 1, 0, 0], + [0, 0, 1, "neo"], + [0, 0, 0, 1] + ] + ], + + "Alternativ": { + "Kommentar": "Sjekk ut ditta!" + , "plassering av komma": "Sålenge den er før verdien er det gyldig" + , "Enda en kommentar": "TØFT!" + }, + + "Ferdig": "Da er den korte innledninga til JSON ferdig" +} +``` -- cgit v1.2.3