PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/orc/lib/includes/prelude/list.inc

https://github.com/laurenyew/cOrcS
Pascal | 610 lines | 338 code | 29 blank | 243 comment | 20 complexity | 90c01a9ff6e13d2eb27942ba6e7fd0a2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. --
  2. -- list.inc -- Orc standard prelude include, lists section
  3. -- Project OrcScala
  4. --
  5. -- $Id: list.inc 2746 2011-04-14 05:33:29Z dkitchin $
  6. --
  7. -- Copyright (c) 2011 The University of Texas at Austin. All rights reserved.
  8. --
  9. -- Use and redistribution of this file is governed by the license terms in
  10. -- the LICENSE file found in the project's top-level directory and also found at
  11. -- URL: http://orc.csres.utexas.edu/license.shtml .
  12. --
  13. {--
  14. Operations on lists.
  15. Many of these functions are similar to those in the Haskell prelude, but
  16. operate on the elements of a <link linkend="ref.data.list">list</link> in parallel.
  17. --}
  18. {--
  19. @def each[A](List[A]) :: A
  20. <link linkend="ref.concepts.publish">Publish</link> every value in a <link linkend="ref.data.list">list</link>, simultaneously.
  21. @implementation
  22. --}
  23. def each[A](List[A]) :: A
  24. def each([]) = stop
  25. def each(h:t) = h | each(t)
  26. {--
  27. @def map[A,B](lambda (A) :: B, List[A]) :: List[B]
  28. Apply a function to every element of a <link linkend="ref.data.list">list</link> (in parallel),
  29. <link linkend="ref.concepts.publish">publishing</link> a list of the results.
  30. @implementation
  31. --}
  32. def map[A,B](lambda (A) :: B, List[A]) :: List[B]
  33. def map(f,[]) = []
  34. def map(f,h:t) = f(h):map(f,t)
  35. {--
  36. @def reverse[A](List[A]) :: List[A]
  37. <link linkend="ref.concepts.publish">Publish</link> the reverse of the given <link linkend="ref.data.list">list</link>.
  38. @implementation
  39. --}
  40. def reverse[A](List[A]) :: List[A]
  41. def reverse(l) =
  42. def tailrev(List[A], List[A]) :: List[A]
  43. def tailrev([],x) = x
  44. def tailrev(h:t,x) = tailrev(t,h:x)
  45. tailrev(l,[])
  46. {--
  47. @def filter[A](lambda (A) :: Boolean, List[A]) :: List[A]
  48. <link linkend="ref.concepts.publish">Publish</link> a <link linkend="ref.data.list">list</link> containing only those elements which satisfy the predicate.
  49. The filter is applied to all list elements in parallel.
  50. @implementation
  51. --}
  52. def filter[A](lambda (A) :: Boolean, List[A]) :: List[A]
  53. def filter(p,[]) = []
  54. def filter(p,x:xs) =
  55. val fxs = filter(p, xs)
  56. if p(x) then x:fxs else fxs
  57. {--
  58. @def head[A](List[A]) :: A
  59. <link linkend="ref.concepts.publish">Publish</link> the first element of a <link linkend="ref.data.list">list</link>.
  60. @implementation
  61. --}
  62. def head[A](List[A]) :: A
  63. def head(x:xs) = x
  64. {--
  65. @def tail[A](List[A]) :: List[A]
  66. <link linkend="ref.concepts.publish">Publish</link> all but the first element of a <link linkend="ref.data.list">list</link>.
  67. @implementation
  68. --}
  69. def tail[A](List[A]) :: List[A]
  70. def tail(x:xs) = xs
  71. {--
  72. @def init[A](List[A]) :: List[A]
  73. <link linkend="ref.concepts.publish">Publish</link> all but the last element of a <link linkend="ref.data.list">list</link>.
  74. @implementation
  75. --}
  76. def init[A](List[A]) :: List[A]
  77. def init([x]) = []
  78. def init(x:xs) = x:init(xs)
  79. {--
  80. @def last[A](List[A]) :: A
  81. <link linkend="ref.concepts.publish">Publish</link> the last element of a <link linkend="ref.data.list">list</link>.
  82. @implementation
  83. --}
  84. def last[A](List[A]) :: A
  85. def last([x]) = x
  86. def last(x:xs) = last(xs)
  87. {--
  88. @def empty[A](List[A]) :: Boolean
  89. Is the <link linkend="ref.data.list">list</link> empty?
  90. @implementation
  91. --}
  92. def empty[A](List[A]) :: Boolean
  93. def empty([]) = true
  94. def empty(_) = false
  95. {--
  96. @def index[A](List[A], Integer) :: A
  97. <link linkend="ref.concepts.publish">Publish</link> the nth element of a <link linkend="ref.data.list">list</link>, counting from 0.
  98. @implementation
  99. --}
  100. def index[A](List[A], Integer) :: A
  101. def index(h:t, 0) = h
  102. def index(h:t, n) = index(t, n-1)
  103. {--
  104. @def append[A](List[A], List[A]) :: List[A]
  105. <link linkend="ref.concepts.publish">Publish</link> the first <link linkend="ref.data.list">list</link> concatenated with the second.
  106. @implementation
  107. --}
  108. def append[A](List[A], List[A]) :: List[A]
  109. def append([],l) = l
  110. def append(h:t,l) = h:append(t,l)
  111. {--
  112. @def foldl[A,B](lambda (B, A) :: B, B, List[A]) :: B
  113. Reduce a <link linkend="ref.data.list">list</link> using the given left-associative binary operation and initial value.
  114. Given the list <code>[x1, x2, x3, ...]</code> and initial value <code>x0</code>,
  115. returns <code>f(... f(f(f(x0, x1), x2), x3) ...)</code>
  116. Example using <code>foldl</code> to reverse a list:
  117. <programlisting language="orc-demo"><![CDATA[
  118. -- Publishes: [3, 2, 1]
  119. foldl(flip((:)), [], [1,2,3])]]></programlisting>
  120. @implementation
  121. --}
  122. def foldl[A,B](lambda (B, A) :: B, B, List[A]) :: B
  123. def foldl(f,z,[]) = z
  124. def foldl(f,z,x:xs) = foldl(f,f(z,x),xs)
  125. {--
  126. @def foldl1[A](lambda (A, A) :: A, List[A]) :: A
  127. A special case of <code>foldl</code> which uses the first element of the <link linkend="ref.data.list">list</link> as the
  128. initial value. If called on an empty list, <link linkend="ref.concepts.states.halt">halt</link>.
  129. @implementation
  130. --}
  131. def foldl1[A](lambda (A, A) :: A, List[A]) :: A
  132. def foldl1(f,x:xs) = foldl(f,x,xs)
  133. {--
  134. @def foldr[A,B](lambda (A, B) :: B, B, List[A]) :: B
  135. Reduce a <link linkend="ref.data.list">list</link> using the given right-associative binary operation and initial value.
  136. Given the list <code>[..., x3, x2, x1]</code> and initial value <code>x0</code>,
  137. returns <code>f(... f(x3, f(x2, f(x1, x0))) ...)</code>
  138. Example summing the numbers in a list:
  139. <programlisting language="orc-demo"><![CDATA[
  140. -- Publishes: 6
  141. foldr((+), 0, [1,2,3])]]></programlisting>
  142. @implementation
  143. --}
  144. def foldr[A,B](lambda (A, B) :: B, B, List[A]) :: B
  145. def foldr(f,z,xs) = foldl(flip(f),z,reverse(xs))
  146. {--
  147. @def foldr1[A](lambda (A, A) :: A, List[A]) :: A
  148. A special case of <code>foldr</code> which uses the last element of the <link linkend="ref.data.list">list</link> as the
  149. initial value. If called on an empty list, <link linkend="ref.concepts.states.halt">halt</link>.
  150. @implementation
  151. --}
  152. def foldr1[A](lambda (A, A) :: A, List[A]) :: A
  153. def foldr1(f,xs) = foldl1(flip(f),reverse(xs))
  154. {--
  155. @def afold[A](lambda (A, A) :: A, List[A]) :: A
  156. Reduce a non-empty <link linkend="ref.data.list">list</link> using the given associative binary operation.
  157. This function reduces independent subexpressions in parallel; the
  158. calls exhibit a balanced tree structure, so the number of sequential
  159. reductions performed is O(log n). For expensive reductions, this
  160. is much more efficient than <code>foldl</code> or <code>foldr</code>.
  161. @implementation
  162. --}
  163. def afold[A](lambda (A, A) :: A, List[A]) :: A
  164. def afold(f, [x]) = x
  165. {- Here's the interesting part -}
  166. def afold(f, xs) =
  167. def afold'(List[A]) :: List[A]
  168. def afold'([]) = []
  169. def afold'([x]) = [x]
  170. def afold'(x:y:xs) = f(x,y):afold'(xs)
  171. afold(f, afold'(xs))
  172. {--
  173. @def cfold[A](lambda (A, A) :: A, List[A]) :: A
  174. Reduce a non-empty <link linkend="ref.data.list">list</link> using the given associative and commutative binary operation.
  175. This function opportunistically reduces independent subexpressions in parallel, so the number of
  176. sequential reductions performed is as small as possible. For expensive reductions, this
  177. is much more efficient than <code>foldl</code> or <code>foldr</code>. In cases
  178. where the reduction does not always take the same amount of time to complete, it
  179. is also more efficient than <code>afold</code>.
  180. @implementation
  181. --}
  182. def cfold[A](lambda (A, A) :: A, List[A]) :: A
  183. def cfold(f, []) = stop
  184. def cfold(f, [x]) = x
  185. def cfold(f, [x,y]) = f(x,y)
  186. def cfold(f, L) =
  187. val c = Channel[A]()
  188. def work(Number, List[A]) :: A
  189. def work(i, x:y:rest) =
  190. c.put(f(x,y)) >> stop | work(i+1, rest)
  191. def work(i, [x]) = c.put(x) >> stop | work(i+1, [])
  192. def work(i, []) =
  193. if (i <: 2) then c.get()
  194. else c.get() >x> c.get() >y>
  195. ( c.put(f(x,y)) >> stop | work(i-1,[]) )
  196. work(0, L)
  197. {--
  198. @def zip[A,B](List[A], List[B]) :: List[(A,B)]
  199. Combine a <link linkend="ref.data.tuple">pair</link> of <link linkend="ref.data.list">lists</link> into a list of pairs.
  200. The length of the shortest list determines the length of the result.
  201. @implementation
  202. --}
  203. def zip[A,B](List[A], List[B]) :: List[(A,B)]
  204. def zip([],_) = []
  205. def zip(_,[]) = []
  206. def zip(x:xs,y:ys) = (x,y):zip(xs,ys)
  207. {--
  208. @def unzip[A,B](List[(A,B)]) :: (List[A], List[B])
  209. Split a <link linkend="ref.data.list">list</link> of <link linkend="ref.data.tuple">pairs</link> into a pair of lists.
  210. @implementation
  211. --}
  212. def unzip[A,B](List[(A,B)]) :: (List[A], List[B])
  213. def unzip([]) = ([],[])
  214. def unzip((x,y):z) = (x:xs,y:ys) <(xs,ys)< unzip(z)
  215. {--
  216. @def concat[A](List[List[A]]) :: List[A]
  217. Concatenate a <link linkend="ref.data.list">list</link> of lists into a single list.
  218. @implementation
  219. --}
  220. def concat[A](List[List[A]]) :: List[A]
  221. def concat([]) = []
  222. def concat(h:t) = append(h,concat(t))
  223. {--
  224. @def length[A](List[A]) :: Integer
  225. <link linkend="ref.concepts.publish">Publish</link> the number of elements in a <link linkend="ref.data.list">list</link>.
  226. @implementation
  227. --}
  228. def length[A](List[A]) :: Integer
  229. def length([]) = 0
  230. def length(h:t) = 1 + length(t)
  231. {--
  232. @def take[A](Integer, List[A]) :: List[A]
  233. Given a number <code>n</code> and a <link linkend="ref.data.list">list</link> <code>l</code>,
  234. <link linkend="ref.concepts.publish">publish</link> a list of the first <code>n</code> elements of <code>l</code>.
  235. If <code>n</code> exceeds the length of <code>l</code>,
  236. or <code>n &lt; 0</code>, take <link linkend="ref.concepts.states.halt">halts</link> with an error.
  237. @implementation
  238. --}
  239. def take[A](Integer, List[A]) :: List[A]
  240. def take(0, _) = []
  241. def take(n, x:xs) =
  242. if n :> 0 then x:take(n-1, xs)
  243. else Error("Cannot take(" + n + ", _)")
  244. {--
  245. @def drop[A](Integer, List[A]) :: List[A]
  246. Given a number <code>n</code> and a <link linkend="ref.data.list">list</link> <code>l</code>,
  247. <link linkend="ref.concepts.publish">publish</link> a list of the elements of <code>l</code> after the first <code>n</code>.
  248. If <code>n</code> exceeds the length of <code>l</code>,
  249. or <code>n &lt; 0</code>, drop <link linkend="ref.concepts.states.halt">halts</link> with an error.
  250. @implementation
  251. --}
  252. def drop[A](Integer, List[A]) :: List[A]
  253. def drop(0, xs) = xs
  254. def drop(n, x:xs) =
  255. if n :> 0 then drop(n-1, xs)
  256. else Error("Cannot drop(" + n + ", _)")
  257. {--
  258. @def member[A](A, List[A]) :: Boolean
  259. <link linkend="ref.concepts.publish">Publish</link> true if the given item is a member of the given <link linkend="ref.data.list">list</link>, and false
  260. otherwise.
  261. @implementation
  262. --}
  263. def member[A](A, List[A]) :: Boolean
  264. def member(item, []) = false
  265. def member(item, h:t) =
  266. if item = h then true
  267. else member(item, t)
  268. {--
  269. @def merge[A](List[A], List[A]) :: List[A]
  270. Merge two sorted <link linkend="ref.data.list">lists</link>.
  271. Example:
  272. <programlisting language="orc-demo"><![CDATA[
  273. -- Publishes: [1, 2, 2, 3, 4, 5]
  274. merge([1,2,3], [2,4,5])]]></programlisting>
  275. @implementation
  276. --}
  277. def merge[A](List[A], List[A]) :: List[A]
  278. def merge(xs,ys) = mergeBy((<:), xs, ys)
  279. {--
  280. @def mergeBy[A](lambda (A,A) :: Boolean, List[A], List[A]) :: List[A]
  281. Merge two <link linkend="ref.data.list">lists</link> using the given less-than relation.
  282. @implementation
  283. --}
  284. def mergeBy[A](lambda (A,A) :: Boolean,
  285. List[A], List[A]) :: List[A]
  286. def mergeBy(lt, xs, []) = xs
  287. def mergeBy(lt, [], ys) = ys
  288. def mergeBy(lt, x:xs, y:ys) =
  289. if lt(y,x) then y:mergeBy(lt,x:xs,ys)
  290. else x:mergeBy(lt,xs,y:ys)
  291. {--
  292. @def sort[A](List[A]) :: List[A]
  293. Sort a <link linkend="ref.data.list">list</link>.
  294. Example:
  295. <programlisting language="orc-demo"><![CDATA[
  296. -- Publishes: [1, 2, 3]
  297. sort([1,3,2])]]></programlisting>
  298. @implementation
  299. --}
  300. def sort[A](List[A]) :: List[A]
  301. def sort(xs) = sortBy((<:), xs)
  302. {--
  303. @def sortBy[A](lambda (A,A) :: Boolean, List[A]) :: List[A]
  304. Sort a <link linkend="ref.data.list">list</link> using the given less-than relation.
  305. @implementation
  306. --}
  307. def sortBy[A](lambda (A,A) :: Boolean, List[A]) :: List[A]
  308. def sortBy(lt, []) = []
  309. def sortBy(lt, [x]) = [x]
  310. def sortBy(lt, xs) =
  311. val half = Floor(length(xs)/2)
  312. val front = take(half, xs)
  313. val back = drop(half, xs)
  314. mergeBy(lt, sortBy(lt, front), sortBy(lt, back))
  315. {--
  316. @def mergeUnique[A](List[A], List[A]) :: List[A]
  317. Merge two sorted <link linkend="ref.data.list">lists</link>, discarding duplicates.
  318. Example:
  319. <programlisting language="orc-demo"><![CDATA[
  320. -- Publishes: [1, 2, 3, 4, 5]
  321. mergeUnique([1,2,3], [2,4,5])]]></programlisting>
  322. @implementation
  323. --}
  324. def mergeUnique[A](List[A], List[A]) :: List[A]
  325. def mergeUnique(xs,ys) = mergeUniqueBy((=), (<:), xs, ys)
  326. {--
  327. @def mergeUniqueBy[A](lambda (A,A) :: Boolean, lambda (A,A) :: Boolean, List[A], List[A]) :: List[A]
  328. Merge two <link linkend="ref.data.list">lists</link>, discarding duplicates, using the given equality and less-than relations.
  329. @implementation
  330. --}
  331. def mergeUniqueBy[A](lambda (A,A) :: Boolean,
  332. lambda (A,A) :: Boolean,
  333. List[A], List[A])
  334. :: List[A]
  335. def mergeUniqueBy(eq, lt, xs, []) = xs
  336. def mergeUniqueBy(eq, lt, [], ys) = ys
  337. def mergeUniqueBy(eq, lt, x:xs, y:ys) =
  338. if eq(y,x) then mergeUniqueBy(eq, lt, xs, y:ys)
  339. else if lt(y,x) then y:mergeUniqueBy(eq,lt,x:xs,ys)
  340. else x:mergeUniqueBy(eq,lt,xs,y:ys)
  341. {--
  342. @def sortUnique[A](List[A]) :: List[A]
  343. Sort a <link linkend="ref.data.list">list</link>, discarding duplicates.
  344. Example:
  345. <programlisting language="orc-demo"><![CDATA[
  346. -- Publishes: [1, 2, 3]
  347. sortUnique([1,3,2,3])]]></programlisting>
  348. @implementation
  349. --}
  350. def sortUnique[A](List[A]) :: List[A]
  351. def sortUnique(xs) = sortUniqueBy((=), (<:), xs)
  352. {--
  353. @def sortUniqueBy[A](lambda (A,A) :: Boolean, lambda (A,A) :: Boolean, List[A]) :: List[A]
  354. Sort a <link linkend="ref.data.list">list</link>, discarding duplicates, using the given equality and less-than relations.
  355. @implementation
  356. --}
  357. def sortUniqueBy[A](lambda (A,A) :: Boolean,
  358. lambda (A,A) :: Boolean,
  359. List[A])
  360. :: List[A]
  361. def sortUniqueBy(eq, lt, []) = []
  362. def sortUniqueBy(eq, lt, [x]) = [x]
  363. def sortUniqueBy(eq, lt, xs) =
  364. val half = Floor(length(xs)/2)
  365. val front = take(half, xs)
  366. val back = drop(half, xs)
  367. mergeUniqueBy(eq, lt,
  368. sortUniqueBy(eq, lt, front),
  369. sortUniqueBy(eq, lt, back))
  370. {--
  371. @def group[A,B](List[(A,B)]) :: List[(A,List[B])]
  372. Given a <link linkend="ref.data.list">list</link> of <link linkend="ref.data.tuple">pairs</link>, group together the second
  373. elements of consecutive pairs with equal first elements.
  374. Example:
  375. <programlisting language="orc-demo"><![CDATA[
  376. -- Publishes: [(1, [1, 2]), (2, [3]), (3, [4]), (1, [3])]
  377. group([(1,1), (1,2), (2,3), (3,4), (1,3)])]]></programlisting>
  378. @implementation
  379. --}
  380. def group[A,B](List[(A,B)]) :: List[(A,List[B])]
  381. def group(xs) = groupBy((=), xs)
  382. {--
  383. @def groupBy[A,B](lambda (A,A) :: Boolean, List[(A,B)]) :: List[(A,List[B])]
  384. Given a <link linkend="ref.data.list">list</link> of <link linkend="ref.data.tuple">pairs</link>, group together the second
  385. elements of consecutive pairs with equal first elements,
  386. using the given equality relation.
  387. @implementation
  388. --}
  389. def groupBy[A,B](lambda (A,A) :: Boolean,
  390. List[(A,B)])
  391. :: List[(A,List[B])]
  392. def groupBy(eq, []) = []
  393. def groupBy(eq, (k,v):kvs) =
  394. def helper(A, List[B], List[(A,B)]) :: List[(A,List[B])]
  395. def helper(k,vs, []) = [(k,vs)]
  396. def helper(k,vs, (k2,v):kvs) =
  397. if eq(k2,k) then helper(k, v:vs, kvs)
  398. else (k,vs):helper(k2, [v], kvs)
  399. helper(k,[v], kvs)
  400. {--
  401. @def rangeBy(Number, Number, Number) :: List[Number]
  402. <code>rangeBy(low, high, skip)</code> returns a sorted <link linkend="ref.data.list">list</link> of
  403. numbers <code>n</code> which satisfy <code>n = low + skip*i</code> (for some
  404. integer <code>i</code>), <code>n &gt;= low</code>, and <code>n &lt; high</code>.
  405. @implementation
  406. --}
  407. def rangeBy(Number, Number, Number) :: List[Number]
  408. def rangeBy(low, high, skip) =
  409. if low <: high
  410. then low:rangeBy(low+skip, high, skip)
  411. else []
  412. {--
  413. @def range(Number, Number) :: List[Number]
  414. Generate a <link linkend="ref.data.list">list</link> of numbers in the given half-open range.
  415. @implementation
  416. --}
  417. def range(Number, Number) :: List[Number]
  418. def range(low, high) = rangeBy(low, high, 1)
  419. {--
  420. @def any[A](lambda (A) :: Boolean, List[A]) :: Boolean
  421. <link linkend="ref.concepts.publish">Publish</link> true if any of the elements of the <link linkend="ref.data.list">list</link> match the predicate, and false otherwise.
  422. The predicate is applied to all elements of the list in parallel; the result
  423. is returned as soon as it is known and any unnecessary execution of the predicate
  424. <link linkend="ref.concepts.states.kill">killed</link>.
  425. @implementation
  426. --}
  427. def any[A](lambda (A) :: Boolean, List[A]) :: Boolean
  428. def any(p, []) = false
  429. def any(p, x:xs) =
  430. Let(
  431. val b1 = p(x)
  432. val b2 = any(p, xs)
  433. Ift(b1) >> true | Ift(b2) >> true | (b1 || b2)
  434. )
  435. {--
  436. @def all[A](lambda (A) :: Boolean, List[A]) :: Boolean
  437. <link linkend="ref.concepts.publish">Publish</link> true if all of the elements of the <link linkend="ref.data.list">list</link> match the predicate, and false otherwise.
  438. The predicate is applied to all elements of the list in parallel; the result
  439. is returned as soon as it is known and any unnecessary execution of the predicate
  440. <link linkend="ref.concepts.states.kill">killed</link>.
  441. @implementation
  442. --}
  443. def all[A](lambda (A) :: Boolean, List[A]) :: Boolean
  444. def all(p, []) = true
  445. def all(p, x:xs) =
  446. Let(
  447. val b1 = p(x)
  448. val b2 = all(p, xs)
  449. Iff(b1) >> false | Iff(b2) >> false | (b1 && b2)
  450. )
  451. {--
  452. @def sum(List[Number]) :: Number
  453. <link linkend="ref.concepts.publish">Publish</link> the sum of all numbers in a <link linkend="ref.data.list">list</link>.
  454. The sum of an empty list is 0.
  455. @implementation
  456. --}
  457. def sum(List[Number]) :: Number
  458. def sum(xs) = foldl(
  459. (+) :: lambda (Number, Number) :: Number,
  460. 0, xs)
  461. {--
  462. @def product(List[Number]) :: Number
  463. <link linkend="ref.concepts.publish">Publish</link> the product of all numbers in a <link linkend="ref.data.list">list</link>.
  464. The product of an empty list is 1.
  465. @implementation
  466. --}
  467. def product(List[Number]) :: Number
  468. def product(xs) = foldl(
  469. (*) :: lambda (Number, Number) :: Number,
  470. 1, xs)
  471. {--
  472. @def and(List[Boolean]) :: Boolean
  473. <link linkend="ref.concepts.publish">Publish</link> the boolean conjunction of all boolean values in the <link linkend="ref.data.list">list</link>.
  474. The conjunction of an empty list is <code>true</code>.
  475. @implementation
  476. --}
  477. def and(List[Boolean]) :: Boolean
  478. def and([]) = true
  479. def and(false:xs) = false
  480. def and(true:xs) = and(xs)
  481. {--
  482. @def or(List[Boolean]) :: Boolean
  483. <link linkend="ref.concepts.publish">Publish</link> the boolean disjunction of all boolean values in the <link linkend="ref.data.list">list</link>.
  484. The disjunction of an empty list is <code>false</code>.
  485. @implementation
  486. --}
  487. def or(List[Boolean]) :: Boolean
  488. def or([]) = false
  489. def or(true:xs) = true
  490. def or(false:xs) = or(xs)
  491. {--
  492. @def minimum[A](List[A]) :: A
  493. <link linkend="ref.concepts.publish">Publish</link> the minimum element of a non-empty <link linkend="ref.data.list">list</link>.
  494. @implementation
  495. --}
  496. def minimum[A](List[A]) :: A
  497. def minimum(xs) =
  498. def minA(x :: A, y :: A) = min(x,y)
  499. foldl1(minA, xs)
  500. {--
  501. @def maximum[A](List[A]) :: A
  502. <link linkend="ref.concepts.publish">Publish</link> the maximum element of a non-empty <link linkend="ref.data.list">list</link>.
  503. @implementation
  504. --}
  505. def maximum[A](List[A]) :: A
  506. def maximum(xs) =
  507. def maxA(x :: A, y :: A) = max(x,y)
  508. foldl1(maxA, xs)