Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Functional programming versus imperative

Author: Anthony Cozzie

Date: 07:50:43 02/18/04

Go up one level in this thread


On February 18, 2004 at 10:32:15, Vincent Diepeveen wrote:

>On February 18, 2004 at 08:55:45, Anthony Cozzie wrote:
>
>>At CMU, I took 15-212 (Functional Programming).  Basically, we learned Standard
>>ML (ML stands for metalanguage) and wrote some rather complex stuff, including a
>>parser/typechecker/runtime environment for a small Logo-like language (it
>>included functions, and custom types if I remember correctly).
>>
>>Basically, it took most people a while to transition from C to ML.  Some people
>>never did it.  I had one friend who, while an incredibly sharp C/assembly coder
>>(currently getting his PhD at CMU in signal processing) just didn't "get" ML.
>>It is simply different, and if you try to write C-ish ML you will fail
>>miserably.
>>
>>ML allows you to write some amazingly compact and readable programs.  I seem to
>>remember posting an implementation of regular expressions in ML - it is about 25
>>lines, versus several hundred for a C implementation.  I made far fewer mistakes
>>in ML than in C, and most of those were caught by the type-checker, not at
>>runtime.  If you can get a program to "compile" in ML, there is at least a 50%
>>chance it will work perfectly on the first run.
>>
>>Standard ML is about 10 times slower than C.  OCAML, the french version, is
>>about 50% slower.  I don't think functional languages are a good idea for
>>writing chess programs, but they certainly have their place.
>>
>>anthony
>
>Professor Swierstra (functional programming) focussed for a few years our
>attention upon functional programming languages being so good in parsing.
>
>Especially with a few Haskell scripts he had copied from somewhere you could
>very well parse stuff.
>
>After a few years i was so sick from this guy (he's even more stubborn than Uri
>Blass) that i took a bet.

Impossible.  No one is more stubborn than Uri :)

>Someone who had written his thesis about functional programming and who was
>giving courses functional programming took the bet.
>
>We both would write a CGI script. He in gofer + haskell. Diep in C.
>
>The cgi script had to parse certain things from submissions and needed to get
>parsed in the CGI script then a HTML form had to get shipped back to the WWW
>server. Of course it had to be bugfree.
>
>I took me 30 minutes to write this in C.
>
>It took him a week to get it to work in gofer.
>
>In fact he didn't finish a buggy version until 2 days after the bet.

I think it is a bit premature to dismiss a family of languages based one 1
person's attempt on 1 problem in 1 language.  I have never worked with this
gopher, or even lisp for that matter.

>Additionally his gofer thing was hell slow of course. Lazy evaluation you
>know... ...it's dead slow!

Of course.  Sometimes it doesn't matter, though.

>Anyone defending functional languages should know better. Any nonsense that is
>claimed about the languages doing better than imperative languages is a big
>nonsense.
>
>Even quickbasic + 1 string lib beats the hell out of it.
>
>You see, no one has written huge programs with it. That's the problem. As soon
>as you have to commercially work, you have to be above that 0.5 source code
>lines an hour.
>
>In such functional languages you don't manage, but in C/C++/JAVA/c# you will
>easily get to 10 or more.

In C you need 10 lines of code just to turn around.

This is my ML regular expression evaluator.  If you can do this in C in 40 lines
I'll eat my shorts.

structure Regex :> REGEX =
struct
  datatype regExp =
    Char of char
  | Concat of regExp * regExp
  | Epsilon
  | Union of regExp * regExp
  | Empty
  | Star of regExp
  | Underscore
  | And of regExp * regExp
  | Top
  | Not of regExp

  fun acc (Char(a)) (nil) k = false
     |acc (Char(a)) (b::s) k = if a = b then k s else false
     |acc (Epsilon) s k = k s
     |acc (Empty) s k = false
     |acc (Union(r1,r2)) s k = acc r1 s k orelse acc r2 s k
     |acc (Concat(r1,r2)) s k = acc r1 s (fn s' => acc r2 s' k)
     |acc (r as Star(r1)) s k = k s orelse acc r1 s (fn s' => if s = s' then
false else acc r s' k)
     |acc (Underscore) (nil) k = false
     |acc (Underscore) (b::s) k = k s
     |acc (And(r1,r2)) s k = acc r1 s k andalso acc r2 s k
     |acc (Top) s k = acc (Star(Underscore)) s k
     |acc (Not(r)) s k = if (acc r s (fn s' => if (k s) then false else true)
then false else true

  fun accept r s = acc r (String.explode s) (fn nil => true | x => false)
end

anthony



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.