- CommonLisp:
- hash-table
- Scheme:
- hash-table (SRFI-69), hashtable (R6RS Scheme)
- Haskell:
- Map
OCaml:
| Require Import Arith Div2 Omega Recdef. | |
| Function bsearch (p : nat -> bool) n { wf lt n } := | |
| match n with | |
| | O => 0 | |
| | _ => | |
| let m := div2 n in | |
| if p m then bsearch p m | |
| else S m + bsearch (fun x => p (S m + x)) (n - S m) | |
| end. |
| :- use_module(library(socket)). | |
| write_atom(Stream,Atom) :- | |
| atom_codes(Atom,Codes), | |
| write_codes(Stream,Codes). | |
| write_codes(_,[]) :- !. | |
| write_codes(Stream,[Code|Tail]) :- | |
| put_code(Stream,Code), | |
| write_codes(Stream,Tail). |
| (* | |
| OCaml translation of the ideas explained in http://fumieval.hatenablog.com/entry/2014/09/22/144401 | |
| To emulate the higher kinded polymorphism, the technique used explained in https://ocamllabs.github.io/higher/lightweight-higher-kinded-polymorphism.pdf | |
| *) | |
| module StateMonad = struct | |
| type ('s, 'a) m = 's -> 's * 'a |
This post also appears on lisper.in.
Reader macros are perhaps not as famous as ordinary macros. While macros are a great way to create your own DSL, reader macros provide even greater flexibility by allowing you to create entirely new syntax on top of Lisp.
Paul Graham explains them very well in [On Lisp][] (Chapter 17, Read-Macros):
The three big moments in a Lisp expression's life are read-time, compile-time, and runtime. Functions are in control at runtime. Macros give us a chance to perform transformations on programs at compile-time. ...read-macros... do their work at read-time.
| var util = require('util'); | |
| function Nothing () {} | |
| Object.Nothing = Nothing; | |
| Array.prototype.maybeMap = function (callback, thisArg) { | |
| var T, A, k; | |
| if (this == null) { | |
| throw new TypeError(" this is null or not defined"); |
| // based on http://labs.cybozu.co.jp/blog/kazuho/archives/2007/05/coopthread.php | |
| function get_all(urls) { | |
| runnable(function (next) { | |
| for (var i = 0; i < urls.length; i++) { | |
| // build request | |
| var xhr = new XMLHttpRequest(); | |
| xhr.open("get", urls[i], true); | |
| xhr.onreadystatechange = function () { | |
| if (xhr.readyState == 4) | |
| next(xhr.responseText); |