Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.haskell > #167 > unrolled thread

So I want to be a haskell wizard

Started byTurtle Wizard <0wl256NOSPAM@gmail.com>
First post2012-02-03 19:42 +0900
Last post2012-02-03 16:09 -0800
Articles 2 — 2 participants

Back to article view | Back to comp.lang.haskell


Contents

  So I want to be a haskell wizard Turtle Wizard <0wl256NOSPAM@gmail.com> - 2012-02-03 19:42 +0900
    Re: So I want to be a haskell wizard Paul Rubin <no.email@nospam.invalid> - 2012-02-03 16:09 -0800

#167 — So I want to be a haskell wizard

FromTurtle Wizard <0wl256NOSPAM@gmail.com>
Date2012-02-03 19:42 +0900
SubjectSo I want to be a haskell wizard
Message-ID<sa6sjisdwpx.fsf@gmail.com>
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

[toc] | [next] | [standalone]


#168

FromPaul Rubin <no.email@nospam.invalid>
Date2012-02-03 16:09 -0800
Message-ID<7xsjir5ui1.fsf@ruckus.brouhaha.com>
In reply to#167
Turtle Wizard <0wl256NOSPAM@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".

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.haskell


csiph-web