2013-03-20 1 views
0

이 과제를 수행하고 있습니다.이 작업을 수행하려면 필사적입니다. 이것이 가장 현명한 방법은 아니며 가장 효율적인 방법이 아니라는 것을 알고 있습니다. 이 코드가 얼마나 비효율적인지 테스트하고 싶기 때문에이 작업을 수행했습니다.Haskell에서 컴파일 할 때 Parse Error (x + 1)가 발생했습니다.

transition_from_conductor :: Element_w_Coord Cell -> List_2D Cell -> Cell 
transition_from_conductor element world = case num_of_heads_around_conductor (0, element) world of 
    1 -> Head 
    2 -> Head 
    _ -> Conductor 
    where 
     num_of_heads_around_conductor :: (Int, Element_w_Coord Cell) -> List_2D Cell -> Int 
     num_of_heads_around_conductor (i, (cell, (x, y))) ((w_cell, (w_x, w_y): rest)) = case rest of 
      [] -> i 
      _ -> case (w_cell, w_x, w_y) of 
       (Head, (x + 1), y)  -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
       (Head, (x + 1), (y + 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
       (Head, (x + 1), (y - 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
       (Head, (x - 1), y)  -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
       (Head, (x - 1), (y + 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
       (Head, (x - 1), (y - 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
       (Head, x,  (y + 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
       (Head, x,  (y - 1)) -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
       _       -> num_of_heads_around_conductor (i  , (cell, (x, y))) (rest) 

내가 터미널에서 이것을 실행하려고하면, 그것은 나를 내가 잘못 했습니까 무엇

(Head, (x + 1), y) ..... 

에 오류 (X + 1)를 분석 할 수 있습니다? 어떻게 수정해야합니까?

몇 가지 ...

type List_2D e = [Element_w_Coord e] 
type Element_w_Coord e = (e, Coord) 
type Coord = (X_Coord, Y_Coord) 
type X_Coord = Integer 
type Y_Coord = Integer 

고마워 : D

답변

3

두 가지 : 당신은 당신이 될 패턴의 그 부분을 제한하려는 yx에 대해 패턴 일치하려고 할 때

  • 당신이 패턴
  • 에 연산 식을 사용할 수 없습니다 기존 xy 변수와 동일하지만 유를 대신 새로운 변수를 만들 xy

나는 것 경비원.

_ -> case (w_cell, w_x, w_y) of 
    (Head, x', y') 
     | x' == x + 1 && y' == y  -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
     | x' == x + 1 && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
     | x' == x + 1 && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
     | x' == x - 1 && y' == y  -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
     | x' == x - 1 && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
     | x' == x - 1 && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
     | x' == x  && y' == y + 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
     | x' == x  && y' == y - 1 -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
    _        -> num_of_heads_around_conductor (i  , (cell, (x, y))) (rest) 

그리고 단순화 :

_ -> case (w_cell, w_x, w_y) of 
    (Head, x', y') 
     | x' == x + 1 && (y' == y || y' == y + 1 || y' == y - 1) 
      || x' == x - 1 && (y' == y || y' == y + 1 || y' == y - 1) 
      || x' == x  &&   (y' == y + 1 || y' == y - 1) 
     -> num_of_heads_around_conductor ((i + 1), (cell, (x, y))) (rest) 
    _ -> num_of_heads_around_conductor (i  , (cell, (x, y))) (rest) 

이 더 간단하게 할 수 의심의 여지가.

+0

우리는 'abs (x - x') <2 && abs (y - y ') <2'라는 것을 알고 있습니까? OP의 코드를 자세히 읽지는 않았습니다. – dave4420

+0

그리드에있는 셀의 주변 8 개 이웃을 일치시키는 것처럼 보이므로 정확한 일치에 대한 예외를 더한 것으로 충분해야합니다. – hammar

3

당신이 사용했던 어떤 언어에서 제거되었습니다 "N + K"패턴입니다. '+'를 사용하여 더 이상 정수에서 패턴을 일치시킬 수 없습니다. 대부분 패턴 일치는 생성자와 리터럴로 제한됩니다.

이 패턴 일치와 같은 결과를 달성하기 위해, 나는 제안 :이 코드를 더 잘못이 있음을

(Head, x0, y)  -> let x = x0 - 1 in ... 

공지 사항 - 패턴 일치가 중복된다. 예를 들어

(Head, (x + 1), y) 

실패의 다음 패턴 :

(Head, (x + 1), (y + 1)) 

성공에도 N + K 지원 패턴이되는 일은 없다. 즉, 실행될 수없는 많은 경우가 있습니다. 여기에서 잘못

+0

보기 패턴을 사용할 수 있습니다. (머리글, 빼기 1 -> x, y) –

+1

n + k 패턴이 아닌 외부 범위의'x' 및'y' 값과 일치하는 것처럼 보입니다. . dave4420의 대답을 참조하십시오. – hammar

관련 문제