P. 1
PicoLisp by Example

# PicoLisp by Example

|Views: 4,232|Likes:
Published by tj64
More than 600 PicoLisp solutions for Rosetta Code programming tasks from a wide range of topics, written by Alexander Burger (the creator of PicoLisp) himself - plus the complete PicoLisp function reference. Learn a fascinating programming language that maximises expressive power while minimizing complexity 'by Example'.
More than 600 PicoLisp solutions for Rosetta Code programming tasks from a wide range of topics, written by Alexander Burger (the creator of PicoLisp) himself - plus the complete PicoLisp function reference. Learn a fascinating programming language that maximises expressive power while minimizing complexity 'by Example'.

### More info:

Published by: tj64 on Aug 23, 2012
Copyright:Attribution Non-commercial

### Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

08/09/2014

pdf

text

original

Write a program to ﬁnd the median value of a vector of ﬂoating-point numbers. The
program need not handle the case where the vector is empty, but must handle the
case where there are an even number of elements.

There are several approaches to this. One is to sort the elements, and then pick the
one in the middle. Sorting would take at least O(nlogn). Another would be to build
a priority queue from the elements, and then extract half of the elements to get to
the middle one(s). This would also take O(nlogn). The best solution is to use the
selection algorithm to ﬁnd the median in O(n) time.

See also: Mean, Mode

(de median (Lst)
(let N (length Lst)
(if (bit? 1 N)
(get (sort Lst) (/ (inc N) 2))
(setq Lst (nth (sort Lst) (/ N 2)))
(/ (+ (car Lst) (cadr Lst)) 2) ) ) )

(scl 2)
(prinl (round (median (1.0 2.0 3.0))))
(prinl (round (median (1.0 2.0 3.0 4.0))))
(prinl (round (median (5.1 2.6 6.2 8.8 4.6 4.1))))
(prinl (round (median (5.1 2.6 8.8 4.6 4.1))))

Output:

2.00
2.50
4.85
4.60

102

3 Rosetta Code Tasks starting with A

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->