[Haskell] Maybe 와 Exception

esrevinu의 이미지

'Purely Functional Data Structures'라는 책을 읽고 있습니다.
책에 코드는 Standard ML을 쓰는데 해스켈 코드도 부록에 있네요. 연습문제 중에
UnbalancedSet의 insert, member 함수를 구현할 때 최악의 경우 비교를 트리의 깊이의 두 배 만큼 해야 하는데
이를 깊이+1만 하도록 하라는 연습문제가 있습니다. 그리고 insert 함수는 이미 Set에 있는 값을 넣는 경우 원래의 Set과
변화가 없으니 Exception을 발생시켜서 새로운 트리를 만들지 말고 기존의 트리를 돌려 주라는 요구가 있습니다.
해스켈은 Pure 함수에서 Exception을 발생시킬 수 없는 것 같고 그래서 Maybe를 사용했습니다.
다른 언어에서 Exception이 어떻게 구현되는지 모르겠지만 Maybe는 재귀 함수를 호출해서 결과를 받을 때마다
Nothing이냐, Just냐를 패턴 매칭해야 하는데 Exception처럼 빠르게 Catch하는 방법이 없을까요?

제 코드입니다. 주석처리한 것은 원래 코드입니다.

{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
 
module UnbalancedSet (UnbalancedSet) where
 
import Set
import Data.Maybe (fromMaybe)
 
data UnbalancedSet a = E | T (UnbalancedSet a) a (UnbalancedSet a)
      deriving Show
 
instance Ord a => Set UnbalancedSet a where
  empty = E
 
  -- member x E = False
  -- member x (T a y b) =
  --  if x < y then member x a
  --  else if x > y then member x b
  --       else True
 
  member x s = remember s Nothing
    where
      remember E Nothing = False
      remember E (Just v) = x == v
      remember (T a y b) c = if x > y then remember b c
                             else remember a (Just y)
  -- insert x E = T E x E
  -- insert x s@(T a y b) =
  --   if x < y then T (insert x a) y b
  --   else if x > y then T a y (insert x b)
  --        else s
 
  insert x s = fromMaybe s $ insert' s Nothing
    where
      insert' E Nothing = Just (T E x E)
      insert' E (Just v) = if x == v then Nothing
                           else Just (T E x E)
      insert' (T a y b) c =
        if x > y then
          insert' b c >>= \k -> return (T a y k)
        else
          insert' a (Just y) >>= \k -> return (T k y b)
esrevinu의 이미지

어렵네요.
continuation passing style로 해 보려고 하는데 잘 모르겠네요.
일단 간단하게 짰습니다.

  insert x s = runCPS s id
    where
      runCPS t@(T a y b) = \k -> k $
        if x < y then
          runCPS a $ \kk -> T kk y b
        else if x > y then
               runCPS b $ \kk -> T a y kk
             else t
      runCPS E = \k -> k $ T E x E

한가지 조건을 만족시키는 코드

  insert x s = runCPS s Nothing id
    where
      runCPS (T a y b) m = \k -> k $
        if x > y then
          runCPS b m $ \kk -> T a y kk
        else 
          runCPS a (Just y) $ \kk -> T kk y b
      runCPS E m = \k -> k $ case m of
                              Nothing -> T E x E
                              Just v -> if x == v then E
                                        else T E x E

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.