Funtional Programming

No Comments

Functional programming languages I ask you this; why do we have to putĀ  lambda’s everywhere? Just because Lambda calculus is fun doesn’t mean we need to over do it. For example look at this piece of Scheme code.

(define find-last-zero
  (lambda (ls)
    (- (- (length ls) 1) (find-first-zero (reverse ls)))))

(define makemove
  (lambda (board player col)
    (let ((row (find-last-zero (list-ref board col))))
      (if (< row 0)
	  #f                 ; no room left!
	  (setsquare board row col player)))))

(define getmove-random
  (lambda (board player)
    (let* ((cols (length board))
	   (c (random cols))
	   (newboard (makemove board player c)))
      (if newboard
	  c
	  (getmove-random board player)))))

(define getmove-human
  (lambda (board player)
    (let ((cols (length board)) (move 0) (newboard #f))
      (displays "\nEnter column [0-" (- cols 1) "]: " )
      (set! move (read))
      (set! newboard (makemove board player move))
      (if newboard
	  move
	  (begin
	    (displays "INVALID MOVE - TRY AGAIN!\n")
	    (getmove-human board player))))))

Someone needs to sort that syntax out.

SORRY, i may be a tad angry at Scheme because my text editor does not highlight any of the syntax and coding with just black text and white background is hard on the eyes. How did they do it in the 70’s.

Comments are closed.