Order two numerical lists: Difference between revisions

Content added Content deleted
(Nimrod -> Nim)
(→‎{{header|Julia}}: A new entry for Julia)
Line 563: Line 563:
[1,2,3] < [1,2,4] # => true
[1,2,3] < [1,2,4] # => true
[1,2,3] < [1,2,3] # => false</lang>
[1,2,3] < [1,2,3] # => false</lang>

=={{header|Julia}}==
<tt>islexfirst</tt> is a somewhat permissive function in that it will accept many sorts of lists for comparison. It does check that all of the elements of both input lists are of some real number type, and if not will throw a <tt>DomainError</tt>.

'''Functions'''
<lang Julia>
function isallreal{T<:AbstractArray}(a::T)
all(map(x->isa(x, Real), a))
end

function islexfirst{T<:AbstractArray,U<:AbstractArray}(a::T, b::U)
isallreal(a) && isallreal(b) || throw(DomainError())
for i in 1:min(length(a), length(b))
x = a[i]
y = b[i]
x != y || continue
return x < y
end
return length(a) < length(b)
end
</lang>

'''Main'''
<lang Julia>
tests = {[1, 2, 3],
primes(10),
0:2:6,
[-Inf, 0.0, Inf],
[π, e, φ, catalan],
[2015, 5],
[-sqrt(50.0), 50.0^2],
}

println("Testing islexfirst:")
for (a, b) in combinations(tests, 2)
tres = islexfirst(a, b) ? " is " : " is not "
tres *= "lexically prior to\n "
println("\n ", a, tres, b)
end
</lang>

{{out}}
<pre>
Testing islexfirst:

[1,2,3] is lexically prior to
[2,3,5,7]

[1,2,3] is not lexically prior to
0:2:6

[1,2,3] is not lexically prior to
[-Inf,0.0,Inf]

[1,2,3] is lexically prior to
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219]

[1,2,3] is lexically prior to
[2015,5]

[1,2,3] is not lexically prior to
[-7.0710678118654755,2500.0]

[2,3,5,7] is not lexically prior to
0:2:6

[2,3,5,7] is not lexically prior to
[-Inf,0.0,Inf]

[2,3,5,7] is lexically prior to
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219]

[2,3,5,7] is lexically prior to
[2015,5]

[2,3,5,7] is not lexically prior to
[-7.0710678118654755,2500.0]

0:2:6 is not lexically prior to
[-Inf,0.0,Inf]

0:2:6 is lexically prior to
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219]

0:2:6 is lexically prior to
[2015,5]

0:2:6 is not lexically prior to
[-7.0710678118654755,2500.0]

[-Inf,0.0,Inf] is lexically prior to
[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219]

[-Inf,0.0,Inf] is lexically prior to
[2015,5]

[-Inf,0.0,Inf] is lexically prior to
[-7.0710678118654755,2500.0]

[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219] is lexically prior to
[2015,5]

[3.141592653589793,2.718281828459045,1.618033988749895,0.915965594177219] is not lexically prior to
[-7.0710678118654755,2500.0]

[2015,5] is not lexically prior to
[-7.0710678118654755,2500.0]
</pre>


=={{header|LabVIEW}}==
=={{header|LabVIEW}}==