freq.scm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/guile \
  2. -e main -s
  3. !#
  4. ;; This is a program that checks to see if a string of numbers has
  5. ;; repeatitive numbers...
  6. ;; take any number, if even / 2, if odd * 3 + 1 repeat.
  7. ;; The goal is to plot the number of steps it takes to produce 1 from 1-100
  8. ;; gnuplot can plot things via a command line from standard in via
  9. ;; gnuplot -
  10. ;; To give gnuplot commands directly in the command line, using the "-persist" option so that the plot remains on the screen afterwards:
  11. ;; gnuplot -persist -e "set title 'Sine curve'; plot sin(x)"
  12. ;; To set user-defined variables a and s prior to executing commands from a file:
  13. ;; gnuplot -e "a=2; s='file.png'" input.gpl
  14. ;; use these modules to help my process the command line arguments.
  15. (use-modules (ice-9 getopt-long)
  16. (oop goops)
  17. ;; (use-modules (fibers))
  18. (ice-9 textual-ports) ;;open-output-file
  19. (ice-9 readline))
  20. ;; take a number
  21. ;; if even / 2
  22. ;; if odd * 3 + 1
  23. ;; repeat
  24. ;; You should end at 1
  25. ;;(define counter 0)
  26. ;; Every time you call counter, it returns the next higher positive integer
  27. ;; (counter #:key #t) resets the value to 0
  28. (define counter
  29. (let ((counter 0))
  30. (lambda* (#:key reset)
  31. (if reset
  32. (set! counter 0)
  33. (begin
  34. (set! counter (+ counter 1))
  35. counter)))))
  36. ;; within the set 0-n. return value is '(number number)
  37. ;; this function will create a gnuplot script to draw a line through a scatter plot
  38. (define (create-plot-script data-file)
  39. ;; use a let here instead of all the defines
  40. (letrec ([script-filename "/tmp/plot.dem"]
  41. [output-script-port (open-output-file script-filename)])
  42. (put-string output-script-port "set style fill transparent solid 1 noborder\n")
  43. (put-string output-script-port "set style circle radius 0.5\n")
  44. (put-string output-script-port
  45. (string-append "plot '" data-file "' with circles"))
  46. (force-output output-script-port)
  47. (close-port output-script-port)
  48. script-filename))
  49. ;; graphs a scatter plot from the set of numbers 2-n
  50. ;; what's wrong here? and wow functional programming recursively sure makes for consice code!
  51. (define (graph-collatz-set n)
  52. ;; number-iterations is the list of numbers and their iterations
  53. ;; ie: '((2 . 1) (3 . 8) (4 . 2) ...)
  54. (letrec ([data-filename "/tmp/data.dat"]
  55. [output-data-port (open-output-file data-filename)]
  56. [script-filename (create-plot-script data-filename)])
  57. (let loop ((number 2) (iterations (collatz 2))) ;; loop through the numbers 0-n
  58. (when (>= n number)
  59. ;; write to a file
  60. (put-string output-data-port
  61. (string-append (number->string number) "\t" (number->string iterations) "\n"))
  62. ;;(display (string-append "number is " number " iterations is " iterations "\n"))
  63. (loop (+ number 1) (collatz (+ number 1)))))
  64. (force-output output-data-port)
  65. (close-port output-data-port)
  66. (system (string-append "gnuplot " "-p " script-filename)))
  67. ;;(delete-file data-filename)
  68. ;;(delete-file script-filename)
  69. ;; if guile-charting worked
  70. ;; (make-scatter-plot
  71. ;; "3+1 conjecture"
  72. ;; (list
  73. ;; (append '("numbers")
  74. ;; (let loop ((number 2) (iterations (collatz 2))) ;; loop through the numbers 0-n
  75. ;; (if (>= n number)
  76. ;; (cons (cons number iterations) (loop (+ number 1) (collatz (+ number 1))))
  77. ;; '())))))
  78. )
  79. (define (main args)
  80. ;;the option specification tells getopt-long how
  81. ;; to parse the command line
  82. (let* ((option-spec '((version (single-char #\v) (value #f))
  83. (help (single-char #\h) (value #f))
  84. ))
  85. ;; tell getopt-long to parse the command line and put the
  86. ;; data in options
  87. (options (getopt-long args option-spec))
  88. ;; was --help or -h used?
  89. (help-wanted (option-ref options 'help #f))
  90. (version-wanted (option-ref options 'version #f)))
  91. (if (or number-wanted version-wanted help-wanted set-wanted graph-wanted)
  92. (cond
  93. (version-wanted
  94. (display "freq version 0.1\n"))
  95. (help-wanted
  96. (display "freq [options]\n")
  97. (display "")
  98. (display "-v --version Display version\n")
  99. (display "-h, --help Display this help info\n")
  100. (else
  101. )
  102. )))))