Skip to main content
added 15 characters in body; edited tags; edited title
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Applicative permutation to generate knight moves

moveKnight :: KnightPos -> [KnightPos]  
moveKnight (c,r) = filter onBoard  
    [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1)  
    ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2)  
    ]  
    where onBoard (c,r) = c `elem` [1..8] && r `elem` [1..8] 
moveKnight :: KnightPos -> [KnightPos]  
moveKnight (c,r) = filter onBoard  
    [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1)  
    ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2)  
    ]  
    where onBoard (c,r) = c `elem` [1..8] && r `elem` [1..8]

I know that this is a massively contrived situation, but they serve me as an exercise to understand the language better.

Applicative permutation

moveKnight :: KnightPos -> [KnightPos]  
moveKnight (c,r) = filter onBoard  
    [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1)  
    ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2)  
    ]  
    where onBoard (c,r) = c `elem` [1..8] && r `elem` [1..8] 

I know that this is a massively contrived situation, but they serve me as an exercise to understand language better.

Applicative permutation to generate knight moves

moveKnight :: KnightPos -> [KnightPos]  
moveKnight (c,r) = filter onBoard  
    [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1)  
    ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2)  
    ]  
    where onBoard (c,r) = c `elem` [1..8] && r `elem` [1..8]

I know that this is a massively contrived situation, but they serve me as an exercise to understand the language better.

Post Reopened by 200_success
fixed code
Source Link

So the above works correctly, but the code looks ugly and doesn't use advanced idioms, such as applicative style.

With applicative approach I don't know how Because every list comprehension is equivalent to do it. I tried the below code, which generates 2 times more elements, because it doesn't know, that if you increment/decrement by 1 on the left sidea lift, you must do it by 2 on the right sideabove code could be rewritten with liftA5:

[(,)]liftA5 <*>:: ([(+),Applicative g => (a -)]> <*>b [6]-> <*>c [1,-> 2])d <*>-> ([(+),e (-> f)] <*>-> [6]g <*>a [1,-> 2])
g b -> g c -> generates:g [(7,7),(7,8),(7,5),(7,4),(8,7),(8,8),(8,5),(8,4),
d -> g e -> g f
liftA5 f a b c d e = f <$> a <*> b <*> c <*> d <*> e

liftA5 (5,7),\c r f g a -> (5,8)c `f` a, r `g` (5,5invert a),(5,4),) [6] [2] [(4,7+), (4,8-),] [(4,5+), (4,4-)] [1,2]
  • HowAre there any way to implementgenerate the generation of suchabove list with applicative stylein a more elegant way?
  • How to generalize it to arbitrary possible permutations? (So that my code could parametrized and not fixed to just numbers 1 and 2, or just + and -, etc)
  • What intuitions, insights and lessons can I get from this exercise?

So the above works correctly, but the code looks ugly and doesn't use advanced idioms, such as applicative style.

With applicative approach I don't know how to do it. I tried the below code, which generates 2 times more elements, because it doesn't know, that if you increment/decrement by 1 on the left side, you must do it by 2 on the right side:

[(,)] <*> ([(+), (-)] <*> [6] <*> [1, 2]) <*> ([(+), (-)] <*> [6] <*> [1, 2])
-- generates: [(7,7),(7,8),(7,5),(7,4),(8,7),(8,8),(8,5),(8,4),
--             (5,7),(5,8),(5,5),(5,4),(4,7),(4,8),(4,5),(4,4)]
  • How to implement the generation of such list with applicative style?
  • How to generalize it to arbitrary possible permutations?
  • What intuitions, insights and lessons can I get from this exercise?

So the above works correctly, but the code looks ugly. Because every list comprehension is equivalent to a lift, the above code could be rewritten with liftA5:

liftA5 :: Applicative g => (a -> b -> c -> d -> e -> f) -> g a -> g b -> g c -> g d -> g e -> g f
liftA5 f a b c d e = f <$> a <*> b <*> c <*> d <*> e

liftA5 (\c r f g a -> (c `f` a, r `g` (invert a))) [6] [2] [(+), (-)] [(+), (-)] [1,2]
  • Are there any way to generate the above list in a more elegant way?
  • How to generalize it to arbitrary possible permutations? (So that my code could parametrized and not fixed to just numbers 1 and 2, or just + and -, etc)
  • What intuitions, insights and lessons can I get from this exercise?
Post Closed as "Not suitable for this site" by Vogel612, user34073, user62850, Mast, Quill
removed tag from title
Link
Malachi
  • 28.7k
  • 11
  • 86
  • 188

Applicative permutation in Haskell

added 132 characters in body
Source Link
Loading
Source Link
Loading