Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

So I want to be a haskell wizard

71 views
Skip to first unread message

Turtle Wizard

unread,
Feb 3, 2012, 5:42:34 AM2/3/12
to
Hi,

I was wondering how one could write this scheme program in
a good haskell sense:

(define 'cons 0) ....

(define dispatch
(vector cons
un-cons
car
cdr
set-car!
set-cdr!
pair?))

(lambda (<selector> . <arguments>)
(apply (vector-ref dispatch <selector>) <arguments>))))


After writing some 7 files of code (see below) I wondered if
there are any better solutions:

-- code follows :

type ARGS = String

readfunction :: [ARGS]->Int
readfunction s = length s

writefunction :: [ARGS]->Int
writefunction s = length s

type F = [ARGS]->Int
--type F = String->Int->[Char]
--type F = String->Int->[Int]

dispatchparser :: String -> [ARGS] -> F
dispatchparser msg args = case msg of
"gnuvy" -> readfunction
"foo" -> writefunction

main = do print "Foo"
print $ f([bar]) where
bar = "foo"
f = dispatchparser bar ["tully"]

-- code

type ARGS = String

readfunction :: [ARGS]->Int
readfunction s = length s

writefunction :: [ARGS]->Int
writefunction s = length s

type F = [ARGS]->Int
--type F = String->Int->[Char]
--type F = String->Int->[Int]

dispatchparser :: String -> [ARGS] -> F
dispatchparser msg args = case msg of
"gnuvy" -> readfunction
"foo" -> writefunction

main = do print "Foo"
print $ f([bar]) where
bar = "foo"
f = dispatchparser bar ["tully"]

-- code

type ARGS = String
--data ARGSDATA = Args String
-- -- Args String
-- -- String -- FIX
-- -- Int -- FIX
type ARGSLIST = [ARGS]

readfunction :: ARGSLIST->Int
readfunction s = length s

writefunction :: ARGSLIST->Int
writefunction s = length s

type F = ARGSLIST->Int

dispatchparser :: String -> ARGSLIST -> F
dispatchparser msg args = case msg of
"gnuvy" -> readfunction
"foo" -> writefunction

main = do print "Foo"
print $ f([bar]) where
bar = "foo"
f = dispatchparser bar ["tully"]

-- code

type ARGS = String -- load at run-time
--data ARGSDATA = ARGSDATA String
-- -- Args String
-- -- String -- FIX
-- -- Int -- FIX
type ARGSLIST = [ARGS]

readfunction :: ARGSLIST->Int
readfunction s = length s

writefunction :: ARGSLIST->Int
writefunction s = length s

type F = ARGSLIST->Int

dispatchparser :: String -> ARGSLIST -> F
dispatchparser msg args = case msg of
"gnuvy" -> readfunction
"foo" -> writefunction

main = do print "Foo"
print $ f([bar]) where
bar = "foo"
f = dispatchparser bar ["tully"] -- ST-80 :-)


-- code

type ARGS = String -- load at run-time
data ARGSDATA = ARGSDATA ARGS
type ARGSLIST = [ARGSDATA]

readfunction :: ARGSLIST->Int
readfunction s = length s

writefunction :: ARGSLIST->Int
writefunction s = length s

type F = ARGSLIST->Int

dispatchparser :: String -> ARGSLIST -> F
dispatchparser msg args = case msg of
"gnuvy" -> readfunction
"foo" -> writefunction

main = do print "Foo"
print $ f([argsdata2]) where
bar = "foo"
argsdata :: ARGSDATA
argsdata = ARGSDATA "tully"
argsdata2 :: ARGSDATA
argsdata2 = ARGSDATA "tully2"
f = dispatchparser bar [argsdata] -- ST-80 :-)

-- code


type ARGS = String -- load at run-time
data ARGSDATA = ARGSDATA ARGS
type ARGSLIST = [ARGSDATA]

readfunction :: ARGSLIST->Int
readfunction s = length s

writefunction :: ARGSLIST->Int
writefunction s = length s

type F = ARGSLIST->Int

dispatchparser :: String -> ARGSLIST -> F
dispatchparser msg args = case msg of
"gnuvy" -> readfunction
"foo" -> writefunction

main = do print "Foo"
print $ f(argsdata2) where
bar = "foo"
argsdata :: ARGSLIST
argsdata = [ARGSDATA "tully"]
argsdata2 :: ARGSLIST
argsdata2 = [ARGSDATA "tully2"]
f = dispatchparser bar argsdata -- ST-80 :-)

-- code

readfunction :: Int->Int
readfunction i = i
--equational readfunction = ReadFunction

writefunction :: Int->Int
writefunction i = i
---equational writefunction = readfunction-- WriteFunction

--data F = Int

dispatchparser :: String -> (Int->Int)
dispatchparser msg = case msg of
"gnuvy" -> readfunction
"foo" -> writefunction

main = do print "Foo"
print $ f(1) where
f = dispatchparser "gnuvy"



TW

Paul Rubin

unread,
Feb 3, 2012, 7:09:58 PM2/3/12
to
Turtle Wizard <0wl256...@gmail.com> writes:
> I was wondering how one could write this scheme program in
> a good haskell sense:

Haskell is not Scheme and it doesn't cleanly support every possible
Scheme programming style. In particular, straightforward mappings of
your dispatch vector to Haskell won't work, because the functions don't
have similar type signatures. If you had to, you could write all the
functions as IO or ST actions, represent cons nodes as IORef or STRef
containers, etc. Could you instead say how you'd actually want to use
that function? Otherwise your question sounds sort of like a C
programmer asking how to turn off the Scheme garbage collector.

You might look for the article "write yourself a Scheme in 48 hours".
0 new messages