N Best Programming Techniques
From Compsci.ca Wiki
(Difference between revisions)
m (→O'Caml) |
(→O'Caml) |
||
| Line 45: | Line 45: | ||
match n with | match n with | ||
1 -> 1 | 1 -> 1 | ||
| - | | o -> o * fact (o-1);;</pre> | + | | o -> o * fact (o - 1);;</pre> |
| + | |||
| + | <pre># let rec fact = | ||
| + | function | ||
| + | 1 -> 1 | ||
| + | | o -> o * fact (o - 1);;</pre> | ||
| + | |||
| + | <pre># let fact n = | ||
| + | let rec fact' n acc = | ||
| + | match n with | ||
| + | 1 -> acc | ||
| + | | o -> fact (o - 1) (n * acc);;</pre> | ||
==SML/NJ== | ==SML/NJ== | ||
Revision as of 12:10, 9 July 2006
Ten best programming techniques you're (probably) not using.
Contents |
Pattern matching
Common Lisp (destructuring-bind)
[1]> (defun foo (a-list)
(destructuring-bind (a &rest b) a-list
(list a b)))
FOO
[2]> (foo '(1 2 3 4))
(1 (2 3 4))
[3]>
Erlang
-module(example).
-export(hello/1]).
hello("wtd") ->
io:format("Hello, ~s!~n", [Name]);
hello(Name) ->
io:format("Get out!!~n", []).
Haskell
O'Caml
# let rec fact n =
match n with
1 -> 1
| o -> o * fact (o - 1);;
# let rec fact =
function
1 -> 1
| o -> o * fact (o - 1);;
# let fact n =
let rec fact' n acc =
match n with
1 -> acc
| o -> fact (o - 1) (n * acc);;