Lisp links thread

Coders forum. Discuss techniques, swap code snippets, ask for help etc. relating to all forms of software development.
Post Reply
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Re: Lisp links thread

Post by teamonkey »

It's not Lisp, but has anyone been playing about with F# at all? I had a good read of the first few chapters of Expert F# in Borders yesterday and it seemed to me that the language did not suck at all, as far as I could gather. I've been having a play with it today and I'm very impressed. It's based on O'Caml, which I don't know, but I gather that Microsoft have simplified the syntax a bit and modified it to be a bit more pragmatic and work with the .Net way of doing things. Functional and immutable by default, iterative and mutable when you want it. It's very Lisp-like but without the brackets, and included are Haskell-style comprehensions and composition and native support of .Net. I think it lacks in metaprogramming capabilities compared to Lisp and O'Caml though.

Some things I thought were nice - function parameters are tuples, for example.

Code: Select all

>let myfunc(x,y,z) = (x,y,z);;                   // function takes 3 params, returns them in a tuple. ";;" is for the interpreter.
    val myfunc : 'a * 'b * 'c -> 'a * 'b * 'c

> let x = (1,2,3);;
    val x : int * int * int

> myfunc x;;                                    // Neat!
    val it : int * int * int = (1, 2, 3)
I don't know if that's useful in any way, but it appealed to my sense of aesthetics. :)

It integrates with VS2008, which I don't have, but if you download Visual Studio 2008 Shell (integrated mode), install it, then install the F# compiler, you get a roll-your-own F# Express with all the syntax highlighting and all that (unfortunately no GUI builder though).
adam
Baaaaaa!
Baaaaaa!
Posts: 4418
Joined: Sat Jun 24, 2006 9:15 pm 0

Re: Lisp links thread

Post by adam »

i was going to post about this but may as well bung it in here, it's going to be a series on implementing retrogames in functional languages
http://prog21.dadgum.com/23.html
User avatar
failrate
Baaaaaa!
Baaaaaa!
Posts: 2354
Joined: Sun May 23, 2004 6:54 am 0
Location: Sandy Eggo
Contact:

Re: Lisp links thread

Post by failrate »

Nice blog, but I'm about 50/50 on this guy. I read some of his older posts, and one comment really put me off
The first was that the tutorials were self-absorbed in the accoutrements of functional programming: type systems, fancy ways of using types for generic programming, lambda calculus tricks like currying.
I've started using currying extensively in my Scheme/JavaScript/Flash programming, as it can often remove whole pages of boilerplate code in a pleasantly elegant manner. All you have to do is stop thinking of them as some sort of mystical trick and start thinking of them as simply being function factories, and you're golden.

Just wanted to get that off of my chest :)
adam
Baaaaaa!
Baaaaaa!
Posts: 4418
Joined: Sat Jun 24, 2006 9:15 pm 0

Re: Lisp links thread

Post by adam »

thank you for sharing :P
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Re: Lisp links thread

Post by teamonkey »

Can you give us an example of how currying can be useful for programming tasks? I've been doing some work on writing an interpreter and I can see how every function being curried makes some things much more elegant internally, but I've never quite got why I should be doing it otherwise.
User avatar
failrate
Baaaaaa!
Baaaaaa!
Posts: 2354
Joined: Sun May 23, 2004 6:54 am 0
Location: Sandy Eggo
Contact:

Re: Lisp links thread

Post by failrate »

Sure, for example, the queueing system in my current shmup engine, I want to be able to pull a sprite off of a given bucket if it's inactive, or return null if it is not.

Originally, I had this code, which works just fine,

Code: Select all

function takeBadGuy():Character
{
	var free:Array = badGuys.filter(isInactive);
	if (free.length > 0)
	{
		var c:Character = free.shift();
		c.active = true;
		return c;
	}
	return null;
}
Except that I needed to rewrite the same bit of code for egoBullets, pickUps, and any other new buckets I added, so I rewrote it like so:

Code: Select all

public function curryTake(arr:Array):Function
		{
			return function():Character
			{
				var free:Array = arr.filter(isInactive);
				if (free.length > 0)
				{
					var c:Character = free.shift();
					c.active = true;
					return c;
				}
				return null;
			}
		}
		
		public var takeBadGuy:Function =  curryTake(badGuys);
So, takeBadGuy is defined as the returned value of curryTake, with the argument of the array badGuys. The array argument is bound to the value arr in the function defined in the body of curryTake.

So, now I just add more buckets, like

Code: Select all

public var takeBullet:Function = curryTake(bullets);
Now, I could do all of this by just making a queue object. Then I could pack in both the list of objects and the methods to operate on them. In fact, if my queues ever needed to be more complex, I probably would. However, at this level of complexity, it's 6 on one side, half a dozen on the other. Additionally, in some languages, like Python, additional overhead is incurred for each additional function call and object attribute dereference. I could argue that in this case, currying might save me a little in performance.

Other languages, like Scheme, don't have objects (sure there are extensions for objects, but let's just assume core), so currying functions is a great tool, right up there with macros. You can use objects if you prefer, but currying is not just a cute trick.
User avatar
lis0r
Baaaa!
Baaaa!
Posts: 894
Joined: Tue Jan 10, 2006 9:26 am 0
Location: The Frozen Wastelands Of The North

Re: Lisp links thread

Post by lis0r »

Feh, objects are only for those who can't deal with closures ;)
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Re: Lisp links thread

Post by teamonkey »

@failrate: I thought currying was reducing functions with multiple parameters so that they accept only one? I think you're just exploiting closures in that example.
User avatar
failrate
Baaaaaa!
Baaaaaa!
Posts: 2354
Joined: Sun May 23, 2004 6:54 am 0
Location: Sandy Eggo
Contact:

Re: Lisp links thread

Post by failrate »

Fair enough :) I could just be misunderstanding the definition of currying, *but*

I don't see much difference between reducing from some arguments to no arguments and reducing from some arguments to one argument.

Would a more appropriate example be an alien movement function generator that takes in some given arguments (such as magnitude) and produces a function that when fed a value, processes a sinusoidal movement?
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Re: Lisp links thread

Post by teamonkey »

failrate wrote:I don't see much difference between reducing from some arguments to no arguments and reducing from some arguments to one argument.
The big difference is internal, as far as I can tell.

In Scheme you have a set of "environments" which are basically look-up tables for variables (implemented as hash tables or whatever), but they also have pointers to the parent environment so a variable lookup will follow up the tree until it finds something (or not). A lambda is a special form which creates a new environment - the named variables are initialised to whatever the params evaluate to. (let, let* and letrec can all be implemented as various combinations of lambda; define in my implementation is a "cheat" that tacks an extra var into the current environment rather than creating a new one.) If you can assume that there will only ever be one parameter (no parameters becomes a 'null' parameter, or something like that) then the environment model becomes much simpler in some respects and look-up tables aren't really necessary, at least not in the same way. Add lazy evaluation into the mix and no lookups are necessary at runtime, which becomes very powerful for a compiled language.

I'm still not sure why you'd want to curry things yourself though.
User avatar
failrate
Baaaaaa!
Baaaaaa!
Posts: 2354
Joined: Sun May 23, 2004 6:54 am 0
Location: Sandy Eggo
Contact:

Re: Lisp links thread

Post by failrate »

Hmm, that's quite a lot of food for thought. I think that instead of telling people that I'm currying, I'll tell them I'm vindalooing or, possibly, masalaing ;)
User avatar
teamonkey
Sheepie Whore
Sheepie Whore
Posts: 12082
Joined: Thu Jun 13, 2002 1:07 am 0
XBL Gamertag: itsthetaste
PSN: teamonkey_ca
Contact:

Re: Lisp links thread

Post by teamonkey »

It's really worthwhile writing a Scheme interpreter, by the way. Not just "scheme in scheme" as described in SICP, but an interpreter in a language that doesn't support closures and lambdas and tail call optimisation natively. I spent more time thinking about how to do things than implementing them (about as hard as a difficult sudoku, I thought). Then suddenly everything starts to make sense and you start to realise why things in lisps and other functional languages are done the way they're done.

F#'s "let", for example, is actually the equivalent of "let*" in scheme. You wonder why there's no plain "let", but of course it's because let is actually just syntax sugar for a lambda function and F# curries functions so each let can only define one variable at a time.

I've got a bit of time off in May. I'll do a proper write-up then.
adam
Baaaaaa!
Baaaaaa!
Posts: 4418
Joined: Sat Jun 24, 2006 9:15 pm 0

Re: Lisp links thread

Post by adam »

http://www2.siba.fi/PWGL/
PWGL is a free cross-platform visual language based on Common Lisp, CLOS and OpenGL, specialized in computer aided composition and sound synthesis. It integrates several programming paradigms (functional, object-oriented, constraint-based) with high-level visual representation of data and it can be used to solve a wide range of musical problems.
adam
Baaaaaa!
Baaaaaa!
Posts: 4418
Joined: Sat Jun 24, 2006 9:15 pm 0

Re: Lisp links thread

Post by adam »

a video about using plt scheme for game development
http://bc.tech.coop/blog/080620.html
User avatar
Firefox
Sheepie Whore
Sheepie Whore
Posts: 11412
Joined: Fri Apr 26, 2002 6:15 pm 0
Location: Sheffield

Re: Lisp links thread

Post by Firefox »

adam wrote:a video about using plt scheme for game development
http://bc.tech.coop/blog/080620.html
Shame it's somewhat spoiled by the crappy VNC connection (why is he doing it over VNC?) and other technical problems, but it still gets across the magic of interactively changing a program while it's still running. That's something a lot of programmers can't see what the fuss is about until they actually experience it for themselves.

Good constructive criticism of PLT v4, too.
"I am a very considerate person. If I want to go out all night on my miniature golf course and shoot lizards, well, no reason to keep the neighbours awake..."
Post Reply