N Best Programming Techniques
From Compsci.ca Wiki
(Difference between revisions)
m |
m |
||
(15 intermediate revisions not shown) | |||
Line 1: | Line 1: | ||
Ten best programming techniques you're (probably) not using. | Ten best programming techniques you're (probably) not using. | ||
+ | |||
<!--# '''Pattern matching''' | <!--# '''Pattern matching''' | ||
Line 21: | Line 22: | ||
==Common Lisp (destructuring-bind)== | ==Common Lisp (destructuring-bind)== | ||
+ | |||
+ | <pre>[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]></pre> | ||
+ | |||
==Erlang== | ==Erlang== | ||
+ | <pre>-module(example). | ||
+ | -export(hello/1]). | ||
+ | |||
+ | hello("wtd") -> | ||
+ | io:format("Hello, ~s!~n", [Name]); | ||
+ | |||
+ | hello(Name) -> | ||
+ | io:format("Get out!!~n", []).</pre> | ||
+ | |||
+ | ==Scala== | ||
+ | <pre> | ||
+ | def fact(n : int) : int = n match { | ||
+ | case 0 => 1 | ||
+ | case _ => n * fact(n - 1) | ||
+ | } | ||
+ | </pre> | ||
+ | |||
==Haskell== | ==Haskell== | ||
+ | <pre> | ||
+ | fact 0 = 1 | ||
+ | fact n = n * (fact $ n - 1) | ||
+ | </pre> | ||
+ | |||
+ | <pre> | ||
+ | fact n = case n of | ||
+ | 0 -> 1 | ||
+ | _ -> n * (fact $ n - 1) | ||
+ | </pre> | ||
+ | |||
+ | <pre> | ||
+ | fact n | ||
+ | | n == 0 = 1 | ||
+ | | otherwise = n * (fact $ n - 1) | ||
+ | </pre> | ||
+ | |||
==O'Caml== | ==O'Caml== | ||
+ | <pre># let rec fact n = | ||
+ | match n with | ||
+ | 1 -> 1 | ||
+ | | 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) | ||
+ | in | ||
+ | fact' n 1;;</pre> | ||
+ | |||
==SML/NJ== | ==SML/NJ== | ||
+ | |||
=Templates/Generics= | =Templates/Generics= | ||
==C++/D== | ==C++/D== | ||
Line 31: | Line 94: | ||
=Regular expressions= | =Regular expressions= | ||
=Anonymous functions/closures= | =Anonymous functions/closures= | ||
+ | =Dynamic Programming= |
Latest revision as of 14:35, 11 June 2007
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", []).
Scala
def fact(n : int) : int = n match { case 0 => 1 case _ => n * fact(n - 1) }
Haskell
fact 0 = 1 fact n = n * (fact $ n - 1)
fact n = case n of 0 -> 1 _ -> n * (fact $ n - 1)
fact n | n == 0 = 1 | otherwise = n * (fact $ n - 1)
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) in fact' n 1;;