2008년 7월 31일 목요일

haskell 로 웹페이지 한번 긁어봤다. 그리고 Maybe, Either, mapM 을 배웠다.

-- Network.HTTP 로 구글을 읽어오는 걸 짜봤다.
-- 그런데 302 moved 를 안따라가네.. 불친절하군.
-- response 받아서 2xx 나올때까지 재귀돌려야 하나?
-- 아니면 Network.HTTP 는 로우레벨이고 좀더 하이레벨 함수들이 있을까?
--
-- 어쨌건 이번에 배운건 Maybe 라는 놈이랑 Either 라는 놈
--
-- Maybe a 는 Just a 나 Nothing 이 될수있는데 패턴매칭으로 어떤 경우인지
-- 가려내서 에러처리를 해야 겠지만 샘플이니 그냥 fromJust 을 써먹었다.
--
-- Either a b 는 Left a 나 Right b 로 떨어지는놈.
-- 역시 마찬가지로 패턴매칭으로 적당히 처리를 해야 곘지만 불편하니 fromRight 이란걸 만들어봤다.
--
-- 역시 IO (IO 와 모나드의 정확한 관계는 모르겠다. 모나드중의 IO 인것은
-- 맞는데 모든 모나드가 IO 처럼 시퀀셜한 액션을 나타내는것은 아닌것
-- 같기도 하고.. 단순히 사이드이펙트를 가질수있는 모든것들이 모나드인가??
-- 이부분은 좀더 공부하면 알게되겠지)..
--
-- ..IO 위주의 코딩이 되니 편안하네. 문법만 조금 달라졌을뿐 돌아가는
-- 모양새는 C 와 다를게 없으니. 그런데 아무리 생각해도 내가 만들
-- 대부분의 프로그램들은 이런식의 절차적인 특성을 지닐텐데.. 아님 내
-- 생각이 절차적인 한계를 벗어나지 못하는 건가.
--
--
-- 이 모나드라는게 정말 끝까지 따라다닐거 같은데..
-- lisp 이라면 mapcar 류함수와 와 IO 함수를 쉽게 섞을수 있는데
-- (mapc #'print '(1 2 3 4))
--
-- haskell 의 map 은 IO 함수를 받지를 않네.
-- map putStr ["fucking", "hard"]
--
-- 뭐 저런걸 돌리는 다른 함수가 있긴 하겠지.
--
-- 음. 검색을 좀 해보니 mapM 이란게 있네. M 은 모나드의 M 이겠지.
-- 내 원래 이걸 직접 만들어볼려고 했는데 아직 모나드는 커녕 타입조차도 몰라서 실패했다.
-- 내가 만들어보려고 한 함수는
-- (a -> IO b) -> [a] -> IO [b]
-- 뭐 이런식으로 해볼라고 했는데 이게 터무니 없는 것이었나보다.
-- mapM 의 타입은
-- Monad a => (b -> a c) -> [b] -> a [c]
-- 이렇네..
--
-- Monad a=> 라면 a 가 Monad 클래스에 속한다는걸 나타내던가.. 좀더 읽고 짜봐야겠다.

import Network.HTTP
import Network.URI
import Data.Maybe(fromJust)

fromRight :: (Either a b) -> b
fromRight e = case e of
Right r -> r
Left l -> error "not right"

main =
let req = Request (fromJust $ parseURI "http://www.google.com") GET [] ""
in do
rep <- simpleHTTP req
putStrLn $ show $ fromRight rep
return ()

댓글 4개:

Vicodin without prescription. :

trackback from: Vicodin user message board.
Vicodin detox.

Gucci 1500 Watch :

To be a upright human being is to procure a philanthropic of openness to the world, an ability to group unsure things beyond your own restrain, that can front you to be shattered in hugely outermost circumstances on which you were not to blame. That says something very impressive about the prerequisite of the honest life: that it is based on a corporation in the fitful and on a willingness to be exposed; it's based on being more like a spy than like a prize, something rather dainty, but whose extremely item attractiveness is inseparable from that fragility.

Anna :

In every tom's life, at some time, our inner foment goes out. It is then burst into enthusiasm by an contend with with another magnanimous being. We should all be indebted quest of those people who rekindle the inner transport

Jack :

In harry's life, at some pass‚, our inner foment goes out. It is then burst into flame at hand an encounter with another benign being. We should all be thankful for the duration of those people who rekindle the inner inspiration