좌표 목록으로 작업하고 싶습니다 [Haskell] (I want to work with a list of coordinates [Haskell])


문제 설명

좌표 목록으로 작업하고 싶습니다 [Haskell] (I want to work with a list of coordinates [Haskell])

좌표 목록으로 작업해야 하므로 이미 다음과 같은 유형을 만들었습니다.

type Pont = (Float, Float)

그리고 얻은 점에서 계산된 Float 목록을 반환해야 합니다. 지금까지 한 작업:

szamol :: Pont ‑> Float
szamol 0.0 = 0.0
szamol (x,y) = 10^(1/2)*((x^2)+(y^2))

ossz :: [Pont] ‑> [Pont]
ossz [] = []
ossz (h,t) = szamol h ++ ossz t

이 오류가 발생합니다.

ERROR "Hazi.hs":6 ‑ Cannot justify constraints in explicitly typed binding
*** Expression    : szamol
*** Type          : Pont ‑> Float
*** Given context : ()
*** Constraints   : (Integral a, Fractional a)

참조 솔루션

방법 1:

The pattern 0.0 in:

szamol 0.0 = 0.0

makes no sense. A Pont Point is a 2‑tuple of Floats, not a single Float, so you can define this as:

szamol :: Pont ‑> Float
szamol (0.0, 0.0) = 0.0
szamol (x,y) = 10^(1/2)*((x^2)+(y^2))

Using 10^(1/2) will fail, since the ^ operator expects the second operand to be of a type that is a member of the Integral typeclass. You can use 10**(1/2).

Using 10**(1/2) will give you the square root of 10 (so ≈ 3.16), and will not calculate the square root of the sum of squares.

You thus likely want to use:

szamol :: Pont ‑> Float
szamol (0.0, 0.0) = 0.0
szamol (x,y) = sqrt (x*x + y*y)

In your ossz function, you make three mistakes:

  1. the return type should be Float here;
  2. you sum up with (+), not with (++) and
  3. the data constructor for a list "cons" is (:), not (,):
ossz :: [Pont] ‑> Float
ossz [] = []
ossz (h : t) = szamol h + ossz t

Here it might be better to use a combination of sum :: (Foldable t, Num a) => t a ‑> a and map :: (a ‑> b) ‑> [a] ‑> [b]:

ossz :: [Pont] ‑> Float
ossz = sum . map szamol

EDIT: if you want to return a list of Floats, then you can map:

ossz :: [Pont] ‑> [Float]
ossz = map szamol

or with explicit recursion:

ossz :: [Pont] ‑> [Float]
ossz [] = []
ossz (h : t) = szamol h : ossz t

(by Bery PurdaWillem Van Onsem)

참조 문서

  1. I want to work with a list of coordinates [Haskell] (CC BY‑SA 2.5/3.0/4.0)

#coordinates #hugs #haskell






관련 질문

지구의 모든 좌표를 생성하시겠습니까? (Generate all coordinates of earth?)

Spritekit이 부모를 변경하면 노드가 사라집니다. (Spritekit changing parent makes the node disappear)

C++의 파일에서 읽은 값을 사용하는 데 도움이 되나요? (Some help using my values read from a file in C++?)

Java의 캔버스에 마우스가 그리는 좌표만 화면에 인쇄하려면 어떻게 해야 합니까? (How do I only print to the screen the coordinates of what the mouse is drawing on a canvas in Java?)

좌표가 영역 내부에 있는지 감지하는 방법은 무엇입니까? (How to detect if coordinate is inside region?)

대상 사각형을 포함하여 그리드의 두 사각형 사이의 거리를 계산하는 방법 (How do I calculate the distance between two squares on a grid, including the target square)

두 좌표 배열 간의 대응 관계 찾기 (Find correspondence between 2 arrays of coordinates)

Postgres 쿼리에서 포인트 유형을 사용하는 방법 (Postgres how to use point type in query)

좌표 목록으로 작업하고 싶습니다 [Haskell] (I want to work with a list of coordinates [Haskell])

C 프로그램에서 해당 선의 특정 점에서 특정 거리, 선 위의 점 찾기 (Find a point on a line, a certain distance from a certain point on that line in c program)

점이 있는 geopandas 데이터 프레임에서 다각형 만들기 (Creating a polygon from a geopandas dataframe with points)

Netlogo는 특정 xycor를 설정합니다. (Netlogo set specific xycor)







코멘트