有什么不同?为什么我只获得列表中的第一个元素?
-module(hello). -export(quicksort/1, lc_quicksort/1]). quicksort([]) -> []; quicksort([Pivot|Rest]) -> quicksort([x || x <- Rest, x =< Pivot]) ++[Pivot] ++ quicksort([y || y <- Rest, y > Pivot]). lc_quicksort([]) -> []; lc_quicksort([Pivot|Rest]) -> lc_quicksort([Smaller || Smaller <- Rest, Smaller =< Pivot]) ++ [Pivot] ++ lc_quicksort([Larger || Larger <- Rest, Larger > Pivot]). 1> c("hello.erl"). {ok,hello} 2> hello:quicksort([3, 1, 4, 2, 5, 9]). [3] 3> hello:lc_quicksort([3, 1, 4, 2, 5, 9]). [1,2,3,4,5,9]
我错过了什么?发布这个以了解我在学习时错过的erlang功能.
谢谢.
变量的第一个字母必须是大写的Rest
或者是Smaller
.x
并且y
在你的函数是atom
秒.
quicksort([]) -> []; quicksort([Pivot|Rest]) -> quicksort([X || X <- Rest, X =< Pivot]) ++[Pivot] ++ quicksort([Y || Y <- Rest, Y > Pivot]).