I'm trying to understand how functional programming (map, reduce,
apply etc.) can work with .NET objects - could someone please point
out what's wrong with this code? As it doesn't work.
(= myList '("Hello" "World"))
(apply (WriteLine Console) myList)
Also, a general question - this (method class parameters) style of
notation is interesting - clearly cleaner than JScheme's ugly dot
notation etc. But... the problem is, this way it's completely
impossible for an IDE to provide IntelliSense - meaning that with
LSharp all we can do is to look up every framework class and method
and all it's overloads on MSDN? Any better ideas?
thank you in advance,
Miklos Hollender
John W. Backus
Here is a simple example that should help clarify things regarding map
and reduce. Apply doesn't seem relevant to a discussion of functional
programming to me, but currying a function certainly is, so I've
included a simple version of that too.
Hope this helps,
Ian D
---------------------
; Simple example of a currying function in L#
; This one takes a function with two parameters and
; returns a function with one parameter
(= curry-2 (fn (f p2) (fn (p) (f p p2))))
; Simple example of a reduce function in L#
; This is called fold in some functional languages (e.g. Haskell)
; Takes three parameters - an initial value, an operation (two
parameter)
; and a list of items to reduce
(= reduce
(fn (init op items)
(if (eql (cdr items) null)
(op init (car items))
(reduce (op init (car items)) op (cdr items)))))
; Giving String.Concat a more "lispy" name - this is to
; simplify the example code below.
(= concat-string (fn (x y) (Concat String x y)))
; Curry the multiply function to create a function that multiplies a
parameter by two
(= times-two (curry-2 * 2))
; Curry the concat-string function to create a function that adds
"foo!" on the end
(= add-foo (curry-2 concat-string "foo!"))
; Define some data
(= strlist '("Hello" "World"))
(= numlist '(1 2 3 4))
; Map Examples
(map prl strlist) ; prl is a short name for Console.Writeline
(map times-two numlist) ; returns (2 4 6 8)
(map add-foo strlist) ; returns ("Hellofoo!" "Worldfoo!")
; Reduce Examples
(reduce "" concat-string strlist) ; Concatenates all strings in the
list to "HelloWorld"
(reduce 1 * numlist) ; Calculate the product of the list (24)
(reduce 0 + numlist) ; Calculate the sum of the list (10)