Groups | Search | Server Info | Login | Register


Groups > comp.lang.forth > #23637

Re: I am still kinda depressed about learning forth

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Re: I am still kinda depressed about learning forth
Date 2013-06-15 01:21 +0000
Organization A noiseless patient Spider
Message-ID <kpgfjj$4f1$1@dont-email.me> (permalink)
References <7dc3c47f-0880-46a7-a902-2e3338a06ef4@nl1g2000pbc.googlegroups.com> <8e42b297-741e-4aab-a1ed-03006629e5e6@wt8g2000pbb.googlegroups.com> <ju3qna02krt@enews6.newsguy.com>

Show all headers | View raw


WJ wrote:

> Hugh Aguilar wrote:
> 
> > Your method of learning is completely wrong. You are expecting to read
> > books and manuals to learn, and then with all of this wonderful
> > knowledge begin to write "lean mean programs and utilize abstraction."
> > That is not how it works at all. You must write a program, and then
> > read up on the language to figure out how to improve the program. I
> > already gave you a practice problem:
> > 
> > https://groups.google.com/group/comp.lang.forth/browse_thread/thread/d89e033a1b268772
> > 
> > I said this:
> > 
> > On Jul 7, 2:35 pm, Hugh Aguilar <hughaguila...@yahoo.com> wrote:
> > > Here is a programming challenge easy enough that even Gavino should
> > > succeed at it (although I doubt that John Passaniti or Elizabeth
> > > Rather can do it). Write a program in ANS-Forth to read a seq file
> > > containing one integer per line. Calculate the following: range of
> > > data, average of data, median of data, and how many data are above and
> > > below the average.
> > > 
> > > For extra credit, generate a PostScript file that displays a bar-graph
> > > of the distribution of data with the mean and median marked.
> > > 
> > > Everybody please wait a week or two with your answer, in order to give
> > > Gavino time to write his program --- this challenge is mostly for his
> > > benefit --- it is okay to give him hints (my own hint is to use the
> > > novice package's lists), but let him write his own code before you
> > > show him your code.
> 
> Since Gavino is interested in learning Lisp and Scheme,
> here's a solution in Racket.
> 
> ;; Lines containing numbers must not have whitespace.
> ;; Blank lines are ignored.
> (define numbers 
>   (with-input-from-file "data.txt"  (lambda()
>     (sort (filter-map string->number (sequence->list (in-lines))) <))))
> 
> ;; numbers must be already sorted.
> (define (median numbers)
>   (define i (floor (/ (sub1 (length numbers)) 2)))
>   (if (odd? (length numbers))
>     (list-ref numbers i)
>     (/ (apply + (take (drop numbers i) 2)) 2)))
> 
> 
> (printf "range: ~a .. ~a\n" (first numbers) (last numbers))
> (define average (/ (apply + numbers) (length numbers)))
> (printf "average: ~a\n" average)
> (printf "median: ~a\n" (median numbers))
> (printf "below average: ~a\n" (length (filter (curry > average) numbers)))
> (printf "above average: ~a\n" (length (filter (curry < average) numbers)))
> 
>  ==>
> 
> range: 3 .. 22
> average: 8
> median: 13/2
> below average: 5
> above average: 2

Ruby:

list = IO.read( "data.txt" ).scan( /\d+/ ).map{|s| s.to_i}.sort

average = list.reduce{|sum,n| sum + n}.to_f / list.size

print "Sorted list: "; p list
puts "Range: #{ list.first } .. #{ list.last }"
printf "Average: %.3f\n", average
list.partition{|n| n < average }.zip(["Below average: ","Above average: "]).
  each{|ary, s| print s; p ary}

 ==>

Sorted list: [1, 2, 2, 3, 4, 5, 6, 7, 8]
Range: 1 .. 8
Average: 4.222
Below average: [1, 2, 2, 3, 4]
Above average: [5, 6, 7, 8]

Back to comp.lang.forth | Previous | Next | Find similar


Thread

Re: I am still kinda depressed about learning forth "WJ" <w_a_x_man@yahoo.com> - 2013-06-15 01:21 +0000

csiph-web