﻿ haskell – 你发现你仍然需要变量,你可以改变,如果是为什么？ - 代码日志

``````values.sum
``````

``````function collection.sum --> inject(zero, function (v,t) --> t+v )
``````

``````x = if a > b then a else b
``````

``````n = case s
/^\d*\$/ : s.to_int
''      : 0
'*'     : a.length
'?'     : a.length.random
else    fail "I don't know how many you want"
``````

> Paul Johnson的Fisher-Yates algorithm答案，当你包括大O约束时，这是相当强大。但是，如果catulahoops指出，大O问题不是绑定到SSA问题，而是有可变的数据类型，并且留出来的算法可以在SSA中写得相当清楚：

`````` shuffle(Lst) ->
array:to_list(shuffle(array:from_list(Lst), erlang:length(Lst) - 1)).
shuffle(Array, 0) -> Array;
shuffle(Array, N) ->
K = random:uniform(N) - 1,
Ek = array:get(K, Array),
En = array:get(N, Array),
shuffle(array:set(K, En, array:set(N, Ek, Array)), N-1).
``````

> jpalecek的area of a polygon示例：

``````def area(figure : List[Point]) : Float = {
if(figure.empty) return 0
val last = figure(0)
var first= figure(0)
val ret = 0
for (pt <- figure) {
ret+=crossprod(last - first, pt - first)
last = pt
}
ret
}
``````

``````def area(figure : List[Point]) : Float = {
if figure.length < 3
0
else
var a = figure(0)
var b = figure(1)
var c = figure(2)
if figure.length == 3
magnitude(crossproduct(b-a,c-a))
else
foldLeft((0,a,b))(figure.rest)) {
((t,a,b),c) => (t+area([a,b,c]),a,c)
}
``````

``````def area([])    = 0.0   # An empty figure has no area
def area([_])   = 0.0   # ...nor does a point
def area([_,_]) = 0.0   # ...or a line segment
def area([a,b,c]) =     # The area of a triangle can be found directly
magnitude(crossproduct(b-a,c-a))
def area(figure) =      # For larger figures, reduce to triangles and sum
as_triangles(figure).collect(area).sum

def as_triangles([])      = []  # No triangles without at least three points
def as_triangles([_])     = []
def as_triangles([_,_])   = []
def as_triangles([a,b,c | rest) = [[a,b,c] | as_triangles([a,c | rest])]
``````

>关于使用不可变结构实现O(1)队列的困难的公主点很有趣(并且可能提供了一个引人注目的例子的基础)，但是如所述的，它基本上涉及数据结构的可变性，而不是直接关于多重赋值问题。
>我对Eratosthenes筛选的答案感兴趣，但不相信。正确的大O，拉出许多素数，你想要的发电机在他引用的文章看起来不容易实现正确与否与SSA。

http://stackoverflow.com/questions/610956/do-you-find-you-still-need-variables-you-can-change-and-if-so-why