Let's use Bigloo Scheme to improve upon the book.
Currying, function composition, and recursion will be displayed.
Bigloo will need these preliminaries:
(module example (library srfi1)) ; List functions.
(define (curry f . args)
(lambda x (apply f (append args x))))
(define (rcurry f . last-args)
(lambda x (apply f (append x last-args))))
(define (cap f g) (lambda xs (f (apply g xs))))
CL:
(mapcar #'(lambda (x) (* 2 x)) (list 1 2 3)) ==> (2 4 6)
Scheme:
(map (curry * 2) (list 1 2 3)) ==> (2 4 6)
CL:
(remove-if-not #'(lambda (x) (char= (elt x 0) #\f))
#("foo" "bar" "baz" "foom"))
==> #("foo" "foom")
Scheme:
(filter (curry string-prefix? "f")
'("foo" "bar" "baz" "foom"))
==> (foo foom)
CL:
(count-if #'evenp #((1 a) (2 b) (3 c) (4 d) (5 e)) :key #'first)
==> 2
Scheme:
(count (cap even? first) '((1 a) (2 b) (3 c) (4 d) (5 e)))
==> 2
CL:
(remove-if-not #'alpha-char-p
#("foo" "bar" "1baz") :key #'(lambda (x) (elt x 0)))
==> #("foo" "bar")
Scheme:
(filter (cap char-alphabetic? (rcurry string-ref 0))
'("foo" "bar" "1baz"))
==> (foo bar)
CL:
(defun plot (fn min max step)
(loop for i from min to max by step do
(loop repeat (funcall fn i) do (format t "*"))
(format t "~%")))
CL-USER> (plot #'exp 0 4 1/2)
*
*
**
****
*******
************
********************
*********************************
******************************************************
NIL
Scheme:
(define (plot fn low high step)
(when (<= low high)
(print (make-string (flonum->fixnum (fn low)) #\*))
(plot fn (+ low step) high step)))
1:=> (plot exp 0 4 0.5)
*
*
**
****
*******
************
********************
*********************************
******************************************************
#f
(* Poetic license.)
Alan Perlis: 'A LISP programmer knows the value of everything, but the
cost of nothing.'
Dude, do you think there's anybody who won't be able to see through your
bullshit?
Obvious troll is obvious.
W> (module example (library srfi1)) ; List functions.
W> (define (curry f . args)
W> (lambda x (apply f (append args x))))
W> (define (rcurry f . last-args)
W> (lambda x (apply f (append x last-args))))
W> (define (cap f g) (lambda xs (f (apply g xs))))
Believe me or not, but you can define curry and other stuff in CL as well.
Moreover, you could use one of libraries which do it for you.
W> (mapcar #'(lambda (x) (* 2 x)) (list 1 2 3)) ==> (2 4 6)
#' is not necessary here because lambda is a macro.
OK, this time you really convinced me.
Just one (very last, simple) question:
Has there ever been a Scheme Machine?
Now, we have the (physical) evidence that Lisp Machines are possible;
but -- as you clearly will be able to demonstrate, given your superior
intelligence -- the Lisp Machines (very close to pure CL, while pure
Scheme is very close to pure Theory) had to be made using physical
reality (and not using fantastic, perfect, imaginary theory).
You know, it's the same as with Mathematics:
Maths is far, far superior to Reality -- it's all limpid, clear,
shiny, perfect, without errors, without even the tiniest shadow of
imperfection or compromise.
Please, please, (I'm on my knees now), construct us a perfect Scheme
Machine, and we will (all of us) switch immediately!
Thanks so much!!
Trolls trolling trolls (trolling trolls)?
Biggest difference between CL and Scheme in context of these WJ's "examples"
is that CL has a separate namespace for functions while Scheme doesn't.
I think it is a cosmetic difference only and it shouldn't be a problem for
hardware design.
Why is that important for you?
There were processors developed to run Scheme.
Google for AIM-559 and AIM-514. There were others.
Also people have attempted to write operating
systems in Scheme.
It says nothing. Scheme can be used for programming as well as CL or
many others can.
It is just that Scheme and CL differ in preferred style,
libraries, ...
Oh captain my captain, you obviously forgot to kill-file me!
> Biggest difference between CL and Scheme in context of these WJ's
> "examples" is that CL has a separate namespace for functions while
> Scheme doesn't.
> I think it is a cosmetic difference only and it shouldn't be a problem
> for hardware design.
In the context of WJ this might be the case, but there are
significantly bigger differences, for instance full continuations,
which might be expected to affect HW design a lot more.
Clearly given that we have HW that runs Scheme quite satisfactorily,
these are not significant issues.
I don't really care about the (practical) differences, as they are
marginal. So are the theoretical ones.
But, if someone wants to artificially bring both sides to an extreme
(as some troll is very good at), it would be (more or less) the
eternal difference between theory (easy to be perfect), and practice
(impossible without compromises).
Arc:
(def exp (n) (expt 2.718281828459045 n))
(def plot (fun low high step)
(when (<= low high)
(prn (string (n-of (fun low) #\*)))
(plot fun (+ low step) high step)))
>
>
> CL:
>
> (remove-if-not #'(lambda (x) (char= (elt x 0) #\f))
> #("foo" "bar" "baz" "foom"))
> ==> #("foo" "foom")
>
>
>
> Scheme:
>
> (filter (curry string-prefix? "f")
> '("foo" "bar" "baz" "foom"))
> ==> (foo foom)
MatzLisp (Ruby):
%w(foo bar baz foom).select{|s| "f" == s[0,1]}
==>["foo", "foom"]
>
> CL:
>
> (remove-if-not #'alpha-char-p
> #("foo" "bar" "1baz") :key #'(lambda (x) (elt x 0)))
> ==> #("foo" "bar")
>
>
> Scheme:
>
> (filter (cap char-alphabetic? (rcurry string-ref 0))
> '("foo" "bar" "1baz"))
> ==> (foo bar)
MatzLisp (Ruby):
%w(foo bar 1baz).grep /^[[:alpha:]]/
==>["foo", "bar"]
MatzLisp (Ruby):
def plot low, high, delta
low.step( high, delta ){|x|
puts "*" * yield( x )}
end
plot(0, 4, 0.5){|x| Math.exp x}
CL:
CL-USER> (keep (curry 'is-prefix "f") '("foo" "bar" baz" "foom"))
("foo" "foom")
CL-USER> (keep (curry 'is-prefix #(1 2)) '(#(1 2 3) #(4 5 6) #(1 2 0 9)))
(#(1 2 3) #(1 2 0 9))
CL-USER> (keep (curry 'is-prefix [1 2]) [[1 2 3] [4 5 6] [1 2 0 9]])
[[1 2 3] [1 2 0 9]]
Cheers
--
MA
People used to make a huff out of this point, but I guess I don't care?
Dear jvt,
at this point the OP is lost in the midst of time and the meanders of memory. :) But nagging the WJ has its own (perverse as much as you want :) ) rewards.
Cheers
--
MA