• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    해결됨

Elm의 functor?

23.11.04 06:02 작성 조회수 187

0

안녕하세요.
또 하나의 선생님 챗지피티와 함께 수강하고 있습니다.

그리고 (기대하시는 바와 조금 다르게) 함수형에 대해 전혀 모르지만, 필요와 끌림에 의해 일단 Elm 배우기를 시작했어요.

질문입니다.
"엘름 아키텍쳐 - The Elm Architecture" 강의 중 펑터(functor)라는 표현을 쓰셨는데(정확히 어딘지 다시 못 찾겠네요 ㅜㅜ)
챗지피티는 Elm엔 functor 라는게 없다라더라고요. 그런가요?

챗지피티에 하고 받은 질문과 답변을 첨부합니다:
---
> "String.any Char.isDigit password" in here, "Char.isDigit" is called as functor right?

> In Elm, the term "functor" is not typically used in the same way as it is in other functional programming languages like Haskell. In Haskell, a functor is a type class that implements the fmap function (or <$>) for a data structure, allowing you to apply a function to the values inside it.

However, in the Elm context, Char.isDigit is simply a function that takes a Char and returns a Bool indicating whether the character is a digit or not. When you use it with String.any like String.any Char.isDigit password, Char.isDigit is not referred to as a functor but rather as a predicate function. String.any takes this predicate function and applies it to each character in the string, checking if any of the characters satisfy the predicate.

In functional programming terms, you might say that String.any is a higher-order function because it takes another function (Char.isDigit in this case) as an argument. The combination of String.any and Char.isDigit checks each character of the string to see if it meets the criteria specified by Char.isDigit, which is to be a digit.

So, to clarify:

  • Char.isDigit is a predicate function.

  • String.any is a higher-order function that applies the predicate to each element in a string.

답변 2

·

답변을 작성해보세요.

0

오, 어떤 "필요"에 의해서 Elm을 공부하고 계신지 궁금합니다.

챗지피티말대로, Elm에는 펑터나 모나드 같은 것들이 잘 드러나지 않습니다. 하지만, 사실상 배후에는 펑터나 모나드 개념이 그대로 들어있습니다. Maybe.map, List.map처럼, 각각 별개의 함수처럼 동작하지만, 사실상 하나의 함수를 받아서, 내부의 요소를 변환하는 펑터 map 작업을 합니다.

엘름이 나름 훌륭한 점은, 이용자로 하여금 펑터나 모나드 등을 구체적으로 알지 못하더라도, 문제없이 개발할 수 있다는 점인 것 같습니다. 사실 펑터가 뭔지 전혀 모르고도 Elm 개발을 잘할 수 있죠.

그리고 String.any isDigit은 펑터와 관련있는 함수가 아니기 때문에, 질문 자체가 틀려서, 챗GPT가 제대로 답변할 수 없었다고 봐야겠네요. 고차함수 관련 질문을 하신 것 같습니다.

Is Maybe.map a functor related function in Elm?

이렇게 한번 물어봐 보시면 참고가 될 것 같습니다.

홍한석님의 프로필

홍한석

질문자

2023.11.06

그렇군요. 우선은 펑터나 모나드 같은 함수형 패러다임의 개념을 모른채 학습해도 되겠네요:
- Maybe.map이 함수(function)지만 (마치 하스켈의) 펑터처럼 동작한다.
- 챗지피티의 할루시네이션인지 궁금해서 질문드렸습니다.
- 질문이 틀린건? 역시 펑터 자체를 몰라서... (하지만 일단 몰라도 된다는걸 알았네요ㅎㅎ)

답변 고맙습니다. 챗지피티도 비슷하게 답하는군요:
> Yes, in Elm, Maybe.map can be considered related to the concept of a functor, although Elm does not explicitly use the term "functor" in its documentation or type system.

In functional programming, a functor is a type that implements a map operation. This map operation takes a function and a functor, and applies the function to the value inside the functor, producing a new functor.

Maybe in Elm is a type that can either be Just a (representing a value of type a) or Nothing (representing the absence of a value). The Maybe.map function follows the functor pattern. It takes a function and a Maybe value, applies the function to the value inside the Maybe if it is Just a, and returns Nothing if the Maybe is Nothing.

Here's how Maybe.map works:

  • If you have Maybe.map f (Just x), it applies the function f to x and returns Just (f x).

  • If you have Maybe.map f Nothing, it returns Nothing.

This behavior adheres to the functor laws, where applying a function over a functor (in this case, Maybe) either changes the value inside the functor in a predictable way (without changing the structure of the functor), or leaves it unchanged if the functor represents a missing value or an error state (Nothing in the case of Maybe).

In summary, while Elm doesn't formally categorize Maybe.map as a functor in the way Haskell does with its type classes, it does functionally behave as one according to the general definition of functors in functional programming.

---

p.s.
"필요"라고 좀 거창하게 썼지만, 개인 프로젝트에서 활용해 보려고 배우기 시작했어요.


https://github.com/charmbracelet/bubbletea
위의 go언어 bubbletea라는 CLI 프레임워크를 사용해서 사이드프로젝트를 하다가 관심이 생겼어요:
- 이 프레임워크가 Elm 아키텍처를 기반으로 만들었다고 하고
- 터미널에서 뷰 그리는게 한계가 있어서, go의 모델 코드는 재활용하고 html로 그려보려고요(<- 필요?).
- CLI 프로그램에선 브라우저 뷰를 하나 열어주는 옵션으로 제공

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.11.06

안녕하세요 홍한석님, 인프런 AI 인턴이에요. Elm 강의를 수강하고 있으시다니 멋지네요!

Elm에서는 Haskell과 같이 “펑터(functor)“를 일반적으로 사용하지 않습니다. Haskell에서 펑터는 데이터 구조에 대해 fmap 함수(또는 <$>)를 구현한 타입 클래스로 정의되어 있어서, 값들에 함수를 적용할 수 있게 해줍니다.

하지만 Elm에서는 Char.isDigit와 같은 함수는 단순히 Char를 받아서 해당 문자가 숫자인지를 나타내는 Bool을 반환하는 함수입니다. String.any와 함께 사용할 때, Char.isDigit은 펑터라고 부르는 것이 아니라 “술어 함수(predicate function)“라고 합니다. String.any는 이 술어 함수를 문자열의 각 문자에 적용하여 술어를 만족시키는 문자가 있는지를 확인합니다.

함수형 프로그래밍에서는 String.any와 같이 다른 함수를 인자로 받는 함수를 “고차 함수(higher-order function)“라고 합니다. String.anyChar.isDigit을 조합하여 문자열의 각 문자가 Char.isDigit의 조건을 만족하는지 확인합니다.

요약하면,

  • Char.isDigit은 술어 함수(predicate function)입니다.
  • String.any는 문자열의 각 요소에 술어 함수를 적용하는 고차 함수(higher-order function)입니다.