erldemo.erl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. % Autogenerated with DRAKON Editor 1.26
  2. -module(erldemo).
  3. -export([common_fate/1, fibonacci/1, foreach/2, main/0, quicksort/2]).
  4. back_cmp(X, Y) ->
  5. % item 168
  6. case X < Y of true ->
  7. % item 169
  8. 1
  9. ; false ->
  10. % item 173
  11. case X > Y of true ->
  12. % item 174
  13. -1
  14. ; false ->
  15. % item 175
  16. 0
  17. end
  18. end
  19. .
  20. common_fate(X) ->
  21. % item 279
  22. case X > 0 of true ->
  23. % item 282
  24. Y = 500
  25. ; false ->
  26. % item 283
  27. Y = 200
  28. end,
  29. % item 284
  30. Y * 7
  31. .
  32. fibonacci(N) ->
  33. case N of
  34. 0 ->
  35. % item 218
  36. [0]
  37. ;
  38. 1 ->
  39. % item 219
  40. [1, 0]
  41. ;
  42. _ ->
  43. % item 220
  44. [First, Second | Rest] = fibonacci(N - 1),
  45. % item 221
  46. Fib = First + Second,
  47. % item 222
  48. [Fib, First, Second | Rest]
  49. end
  50. .
  51. foreach(List, Visitor) ->
  52. % item 55
  53. case List == [] of true ->
  54. % item 56
  55. void
  56. ; false ->
  57. % item 59
  58. [ Head | Rest ] = List,
  59. Visitor(Head),
  60. % item 60
  61. foreach(Rest, Visitor)
  62. end
  63. .
  64. foreach_demo() ->
  65. % item 49
  66. io:format("iteration demo~n"),
  67. % item 42
  68. FibReversed = fibonacci(20),
  69. Fibonacci = lists:reverse(FibReversed),
  70. % item 224
  71. Visitor = fun(X) ->
  72. print(X)
  73. end,
  74. % item 61
  75. foreach(Fibonacci, Visitor),
  76. io:format("~n", [])
  77. .
  78. is_sorted(List) ->
  79. % item 182
  80. Length = length(List),
  81. case Length of
  82. 0 ->
  83. []
  84. ;
  85. 1 ->
  86. []
  87. ;
  88. _ ->
  89. % item 191
  90. [First, Second | Tail] = List,
  91. % item 192
  92. Cmp = back_cmp(First, Second),
  93. % item 193
  94. case Cmp == 1 of true ->
  95. % item 195
  96. print_list(List),
  97. throw("List not sorted")
  98. ; false ->
  99. % item 196
  100. is_sorted(Tail)
  101. end
  102. end
  103. .
  104. main() ->
  105. % item 34
  106. foreach_demo(),
  107. % item 66
  108. quicksort_demo()
  109. .
  110. print(Item) ->
  111. % item 48
  112. io:format("~w ", [Item])
  113. .
  114. print_list(List) ->
  115. % item 226
  116. Visitor = fun(X) ->
  117. io:format("~s ", [X])
  118. end,
  119. % item 225
  120. foreach(List, Visitor),
  121. % item 75
  122. io:format("~n", [])
  123. .
  124. quicksort(List, Comparer) ->
  125. % item 121
  126. Length = length(List),
  127. case Length of
  128. 0 ->
  129. % item 157
  130. Result = List,
  131. % item 162
  132. Result
  133. ;
  134. 1 ->
  135. % item 157
  136. Result = List,
  137. % item 162
  138. Result
  139. ;
  140. 2 ->
  141. % item 158
  142. [ First, Second | _ ] = List,
  143. Cmp = Comparer(First, Second),
  144. % item 159
  145. case Cmp == -1 of true ->
  146. % item 160
  147. Result = List
  148. ; false ->
  149. % item 138
  150. Result = [Second, First]
  151. end,
  152. % item 162
  153. Result
  154. ;
  155. _ ->
  156. % item 141
  157. [ Pivot | Others ] = List,
  158. % item 223
  159. LessThanPivot = fun(X) ->
  160. Comparer(X, Pivot) == -1
  161. end,
  162. % item 198
  163. {Left, Right} = lists:partition(
  164. LessThanPivot,
  165. Others
  166. ),
  167. % item 155
  168. LeftSorted = quicksort(Left, Comparer),
  169. RightSorted = quicksort(Right, Comparer),
  170. % item 161
  171. Result = LeftSorted ++ [Pivot] ++ RightSorted,
  172. % item 162
  173. Result
  174. end
  175. .
  176. quicksort_demo() ->
  177. % item 65
  178. io:format("quick sort demo~n", []),
  179. % item 67
  180. Unsorted = [ "the", "sooner", "we", "start", "this", "the", "better" ],
  181. Sorted = [ "aa", "bb", "cc", "dd", "ee", "ff" ],
  182. Reverse = [ "ff", "ee", "dd", "cc", "bb", "aa" ],
  183. Empty = [],
  184. Flat = [ "flat", "flat", "flat", "flat", "flat" ],
  185. % item 91
  186. Comparer = fun(X, Y) -> back_cmp(X, Y) end,
  187. Unsorted2 = quicksort(Unsorted, Comparer),
  188. Sorted2 = quicksort(Sorted, Comparer),
  189. Reverse2 = quicksort(Reverse, Comparer),
  190. Empty2 = quicksort(Empty, Comparer),
  191. Flat2 = quicksort(Flat, Comparer),
  192. % item 68
  193. print_list(Unsorted2),
  194. print_list(Sorted2),
  195. print_list(Reverse2),
  196. print_list(Empty2),
  197. print_list(Flat2),
  198. % item 197
  199. is_sorted(Unsorted2),
  200. is_sorted(Sorted2),
  201. is_sorted(Reverse2),
  202. is_sorted(Empty2),
  203. is_sorted(Flat2)
  204. .