From: Kristof Bastiaensen Date: 2006-07-28T22:30:06+09:00 Subject: Re: For performance, write it in C On Fri, 28 Jul 2006 09:45:01 +0000, Csaba Henk wrote: > On 2006-07-26, Kristof Bastiaensen wrote: >> -------------------- start of latin.curry ---------------------------- >> -- upto is a nondeterministic function that evaluates to >> -- a number from 1 upto n >> upto 1 = 1 >> upto n | n > 1 = n ? upto (n-1) >> >> -- check if the lists r s have no element with the same value at the >> -- same position >> elems_diff r s = and $ zipWith (/=) r s >> >> -- extend takes a list of columns, and extends each column with a >> -- number for the next row. It checks the number agains the column and >> -- against the previous numbers in the row. >> >> extend :: [[Int]] -> Int -> [[Int]] >> extend cols n = addnum cols [] where >> addnum [] _ = [] >> addnum (col:cs) prev >> | x =:= upto n & >> (x `elem` prev) =:= False & >> (x `elem` col) =:= False = (x:col) : addnum cs (x:prev) >> where x free >> >> latin_square n = latin_square_ n >> where latin_square_ 0 = replicate n [] -- initalize columns to nil >> latin_square_ m | m > 0 = extend (latin_square_ (m-1)) n >> >> square2str s = unlines $ map format_col s >> where format_col col = unwords $ map show col >> >> main = mapIO_ (putStrLn . square2str) (findall (\s -> s =:= latin_square 5)) >> ------------------------- end latin.curry ----------------------------- > > It's really nice and compact! > AFAIK Curry is Haskell boosted with logic programming. Yes, exactly! > > I -- who, ATM, just watches these languages from a distance, and can't > tell it by looking at the code -- wonder if have you used here > something specific to Curry, which would be harder/uglier to express in > Haskell? > Yes, the =:= operator unifies terms like in logic languages, and curry makes it possible to write nondeterministic functions. For example the upto function I defined above can evaluate to any number from 1 upto n, while in haskell it could have only one result. In the code that I wrote above: upto n | n > 1 = n ? upto (n-1) is the same as upto n | n > 1 = n upto n | n > 1 = upto (n-1) Then there are search functions that make it possible to extract all outcomes from a nondeterministic function in a lazy way (i.e. findall) In haskell the above would probably be written in a monad that expresses nondeterminism, but I doubt it will be as clear as the Curry code. > And how the Curry compiler looks like? Is it just a hacked GHC? How > Curry performance relates to that of Haskell? > As far as I know the Curry compiler I used (Munster CC) is written from scratch, in Haskell. I doubt it is as fast and optimized as the Haskell compiler, since Haskell has a much large userbase. > Regards, > Csaba Regards, Kristof