Showing posts with label haskell. Show all posts
Showing posts with label haskell. Show all posts

2007-09-25

Links: Functional programming ebooks

In a subscribe Spb Haskell User Group I've met several interesting links to files garbage with books on the functional programming.

I've dig out Introduction to Functional Programming by Mike Gordon. Judge by the first chapter a lot of text and samples but less mathematics is expecting further. Such a real programmers book.

I do not understand somehow what do they want...

Find an example to show that if V1=V2 then even if V2 is not free in E1 it is not necessarily the case that
(\V1 V2.E)E1E2 = E[E1/V1][E2/V2]

2007-09-17

For young programmers only...

My little sister has started to study C++ at University.

She is in her second year but in their tasks with big black letters is required to give human readable names to variables and to module code. It's strange... evidently, Pascal at the first year didn't school what is good and how to make job easy first of all to yourself.

I've installed Eclipse to my sister, told about Unit testing a little. It's convenient - to write 5 or 10 lines of code right away tests are running for the checking. It's almost REPL. Testing is a guarantee of not turning program text into one long main function. And if something wrong happens then a couple of key pressing brings you to the debugger to the required place with required data.
It seemed to me she liked it.


I liked to demonstrate the popularity of the approach and was surprised by a huge list of frameworks for tests creation.

As for purpose for student projects there is a tiny file QuickTest.h.

if one is to judge nothing except of three elementary macros QT_TEST(testName),QT_CHECK_EQUAL(value1, value2), and QT_RUN_TESTS is required

P.S The first students C++ sounds in haskell about as follows:

minUncommonWord :: String -> String -> String
minUncommonWord s1 s2 =
foldl1 (\a b -> if length a < length b then a else b)
[x | x <- words s1, x `notElem` words s2]

2007-07-23

Haskell : Record update is not first class

Gradually, I continue to study Haskell. During the try to avoid the trouble Record update is not first class looked into the library HList. Damn, it's a little bit verbose.

What for? To have two functions on entry: String->Maybe Setter и String->Maybe Getter, and hence further everything would be beautiful and safety.

Update 24.07.2007 Damn, it could be more easy

The problem of elegant modeling a state with neither more nor less than 10 fields:


data Slots = Slots {
slot1 :: SlotData
slot2 :: SlotData
slot3 :: SlotData
....
}

and the understanding of аn outer world is done by:

mkGetter n | n == 1 = slot1
| n == 2 = slot2
...

mkSetter n | n == 1 = \s v -> s{slot1=v}
| n == 2 = \s v -> s{slot2=v}
...

Though of course if it could be done without lambdas it would be more convenient.

2007-05-18

Haskell: Picker Combinators

One more illustration of combinatorial approach to a problem - Pickler Combinators.
This time the matter is serialization/deserialization of data.

Most of all I was surprised at the unusual using of parameter data types.
 data PU a = PU {appP :: (a,String) -> String,
appU :: String -> (a, String)}

Where I'd declare a class without a hesitation, a type consisting of two functional type fields is used instead.

2007-05-07

Parser Combinators

It's not too bad, code comes out rather beautiful, short and clear. True, for that purpose I had to quickly look into haskell syntax.

I've written a piece of tasks from the first chapter of SICP in haskell as the warm-up, read the sixth chapter of the first book of _darkus_

And by motifs I've gotten the following parser of pl/sql packages specifications:

terminal = (spaces . utoken)
where utoken t s | lower t == lower (take n s) = [(lower (drop n s),t)]
| otherwise = []
where n = length t
lower = map toLower

-- Разрешенные идентификаторы
ident = spaces ((satisfy isAlpha) <:*> zeroOrMore leastChars) <@ (map toLower)
where leastChars = choise [satisfy isAlpha,
satisfy isDigit,
symbol '.',
symbol '_']

comment = single_line_comment <|> multi_line_comment
where single_line_comment = pack (token "--")
(zeroOrMore (satisfy (\_->True)))
(zeroOrMore (symbol '\n'))
multi_line_comment = reverse . p
where p = pack (token "/*") (zeroOrMore (satisfy (\_->True))) (token "*/")

-- Базовые типы
pls_type = choise [terminal "integer",
terminal "varchar2",
terminal "number",
terminal "date",
field_type]
where field_type = ident <* spaces(symbol '%') <*> terminal "type"

-- Разделители
sep_semicolon = spaces (symbol ';')
sep_comma = spaces (symbol ',')

-- Общие части выражений
def_var = ident <*> ((param_type) <|> succeed "in") <*> spaces (pls_type)
where param_type = choise [terminal "in",
terminal "out",
(terminal "in" <*> terminal "out") <@ (\_ -> "in out")]

def_params = spaces ((parens var_list) <|> succeed [])
where var_list = (listOf def_var sep_comma) <|> succeed []

def_fun = ident <*> def_params

-- Спецификации
spec_procedure = terminal "procedure" *> def_fun <*> (succeed "None") <* sep_semicolon
spec_function = terminal "function" *> def_fun <*> terminal "return"
*> ident <* sep_semicolon
spec_declaration = spec_procedure <|> spec_function

spec_create_package = cr_or_repl <*> (terminal "package") *> ident <*> terminal "as"
*> zeroOrMore spec_declaration
<* terminal "end" <* sep_semicolon <* terminal "/"
where cr_or_repl = terminal "create" <:*> option (terminal "or" *> terminal"replace")


Evidently, parser in python intends to the same approach.
Now I have to look into the system of haskell types and classes to have a possibility of digestible using the parser results.