Comments: 
From: (Anonymous) 20070417 08:33 pm (UTC)
Prolog  (Link)

Prolog is sometimes rather more concise than Haskell  consider coding the legal moves for a knight:
%Positions are Cartesian coordinates from (1,1) to (8,8)
%kreach(Pos1,Pos2) holds when a knight can move from Pos1 to Pos2 in one step
%kreach(Pos1,Pos2) assumes Pos1 a valid square
kreach(pair(X1,Y1),pair(X2,Y2)) :
diff(X1,X2,1), diff(Y1,Y2,2), valsquare(X2,Y2).
kreach(pair(X1,Y1),pair(X2,Y2)) :
diff(X1,X2,2), diff(Y1,Y2,1), valsquare(X2,Y2).
diff(X,Y,N) : plus(X,N,Y); plus(Y,N,X).
valsquare(X,Y) :
1 <= X, X <=8, 1<= Y, Y<=8.
I don't see how this can be coded more concisely in Haskell. This is from a bit of code I wrote finding solutions to the Knights Tour to test my knowledge of Prolog  it was much harder for me to get reasonable performing code from Prolog than Haskell, but the code was rather easier to read, even with all the cuts. If Perl is a writeonly language, perhaps Prolog is readonly...
 From: darius 20070417 10:27 pm (UTC)
Re: Prolog  (Link)

Oh goody, a challenge. :)
This is about as concise, but it's inelegant:
knight_from = valid_moves knight_moves
knight_moves = [(1,2),(2,1),(1,2),(2,1),(1,2),(2,1),(1,2),(2,1)]
valid_moves moves (x, y) = filter on_board [(x+dx, y+dy)  (x, y) < moves]
on_board (x, y) = 1<=x && x<=8 && 1<=y && y<=8
Reducing it to something like extreme normal form actually expands it:
valid_moves moves (x, y) = filter on_board $ map (move (x, y)) moves
on_board (x, y) = 1<=x && x<=8 && 1<=y && y<=8
move (x, y) (dx, dy) = (x+dx, y+dy)
apply symmetry moves = moves ++ map symmetry moves
flip_horz (dx, dy) = (dx, dy)
flip_vert (dx, dy) = ( dx, dy)
flip_diag (dx, dy) = ( dy, dx)
knight_moves = apply flip_horz $ apply flip_vert $ apply flip_diag [(1, 2)]
knight_from = valid_moves knight_moves
Incidentally I'd tweak the Prolog a bit:
knight_move(A, B) : knight_probe(A, B), on_board(B).
knight_probe((X1,Y1), (X2,Y2)) : diff(X1, X2, 1), diff(Y1, Y2, 2).
knight_probe((X1,Y1), (X2,Y2)) : diff(X1, X2, 2), diff(Y1, Y2, 1).
diff(X, Y, N) : plus(X, N, Y); plus(Y, N, X).
on_board((X,Y)) : 1 =< X, X =< 8, 1 =< Y, Y =< 8.
From: (Anonymous) 20070418 01:44 am (UTC)
Re: Prolog  (Link)

Hi Darius, I don't like the line: knight_moves = [(1,2),(2,1),(1,2),(2,1),(1,2),(2,1),(1,2),(2,1)]
...since it breaks my own private notion of "conciseness"  you've expanded a definition to its result  I guess this might be what you mean by "inelegant". Would you be able to generate knight_moves more concisely in Haskell? The Prolog tweak is OK, though I actually prefer to use ";" (disjunction) in such a case. Charles Stewart (who forgot to sign before) http://www.advogato.org/person/chalst
From: (Anonymous) 20070712 02:08 am (UTC)
Re: Prolog  (Link)

Neither concise nor readable, but maybe funny: import Data.Ix
import Control.Arrow
import Control.Monad
kreach (row, col) =
filter (both (inRange (1, 8))) $
map ((+ row) *** (+ col)) $
[id, swap] `ap` ([1, 1] `times` [2, 2])
both a = a *** a >>^ uncurry (&&)
swap = uncurry $ flip (,)
times = liftM2 (,)
From: (Anonymous) 20080206 02:26 am (UTC)
Re: Prolog  (Link)

I don't know if I misunderstood this problem, but this seems way too easy to translate to haskell to me. distance x n = [x + n, x  n]
kreach (x1,y1) = [(x2,y2)  x2 < distance x1 1, y2 < distance y1 2, valsquare x2 y2] ++ [(x2,y2)  x2 < distance x1 2, y2 < distance y1 1, valsquare x2 y2]
valsquare x y = (1 <= x) && (x <= 8) && (1 <= y) && (y <= 8)
(define (fact n) (fold * 1 (iota (+ n 1) 1))) ?
I wouldn't call that idomatic Scheme though. There should be a linebreak, and even to a confessed heapallocation junkie like me it's too much to have factorial running in linear space.
Fair enough. I didn't mean to imply that leaving out whitespace was a good idea; I'm just not a fan of messing with the text formatting system that LJ uses, and I don't post often enough to make the attention space/time of internalizing it worthwhile.
Comparing apples to oranges (rather than to goats, shipping containers or freedom), then, how about the following, shamelessly ripped from the SRFI42 "examples.scm":
(define (factorial n)
(* n (productec (:range k 1 n) k)))
Admittedly it's not as brief as the Haskell version, but it's arguably idiomatic Scheme, as far as list comprehension goes, and it'll run in O(n) time and O(1) space.
(I've got to admit that I'm more interested in your response than in playing Devil's advocate to win. This is because I find your insight very interesting, not because I'm trying to troll. I hope you don't mind, and beg your pardon if you do)
My impression of SRFI42 is bad. Too much boilerplate.
Why can't we just have lazycons that doesn't evaluate its arguments until they're accessed? Then we could write lazyiota and make a plain fold over that in constant space. Or?
 From: shae 20070711 11:27 pm (UTC)
Yay!  (Link)

Glad we could meet after so long!
I never did take that 24" Kris Holm unicycle to the USA with me, are you interested in borrowing it for six months or so?  