Page 1 of 1

Haskell Hastles

Posted: Tue Sep 18, 2007 5:22 pm
by Alboin
I've been working on a brainf*ck interpreter to expand my knowledge of Haskell, and functional programming in general. However, as simple as it seems, I've been having some troubles.

At the moment, I'm working on everything but the '[' and ']' commands. Here is what I have so far (It only does one command per execution at the moment.):

Code: Select all

module Main
	where

import IO

bf (x:xs) '>' = (xs : 0)
bf (x:xs) '<' = [x]
bf (x:xs) '+' = [xs + 1]
bf (x:xs) '-' = [xs - 1]

array = [1..30000]

main = do
	sym <- getChar
	bf array sym
It doesn't compile, but I think I'm on the right path.

Any help would be great. Thanks.

Posted: Wed Sep 19, 2007 4:49 am
by Combuster
assuming that the definition is:
bf :: [Int] -> Char -> [Int] (its good practice to always specify the type)

1. you are trying to prefix an array to a number which gives a type error (line 7, xs : 0 <=> [Int] : Int)
2. you are adding an array and a number. Either you should use map (+1) array or do something to fix the semantics. again, you have two type errors (line 9, [xs + 1], line 10, [xs - 1]

also, haskell is lazy evaluating. you can simply define the stack as an infinite sequence of 0s and it will automagically extend it when needed.

you might want to use a different approach for the stack, like

Code: Select all

array :: ([Int], Int, [Int])
array = ([], 0, repeat 0)

bf :: ([Int], Int, [Int]) -> Char -> ([Int], Int, [Int])
bf (l,   i, x:xs) '>' = (i:l, x, xs)
bf (x:xs,i, r   ) '<' = (xs, x, i:r)
bf (l,   x, r   ) '+' = (l, x+1, r)
bf (l,   x, r   ) '-' = (l, x-1, r)
(I haven't done this for a long time, I probably missed some things)

Posted: Fri Sep 21, 2007 4:52 pm
by Alboin
Could I ask what s and xs actually are? What do they contain? Two halves of the list? The most I've found is this (From wikibooks):
(x: xs) is a pattern that matches something (which gets bound to x), which is cons'd, using the function (: ), onto something else (which gets bound to the variable xs).
To me this means that it breaks the list in two, but I'm not sure...

I'm sorry if I'm asking simplistic questions, it's just a little different. Thanks.

Posted: Fri Sep 21, 2007 5:05 pm
by urxae
Alboin wrote:Could I ask what s and xs actually are? What do they contain? Two halves of the list? The most I've found is this (From wikibooks):
(x: xs) is a pattern that matches something (which gets bound to x), which is cons'd, using the function (: ), onto something else (which gets bound to the variable xs).
To me this means that it breaks the list in two, but I'm not sure...

I'm sorry if I'm asking simplistic questions, it's just a little different. Thanks.
IIRC it means x is the head (first element) of the list, and xs is the tail (a list containing the rest of the elements).

Posted: Fri Sep 21, 2007 5:07 pm
by Alboin
urxae wrote:
Alboin wrote:Could I ask what s and xs actually are? What do they contain? Two halves of the list? The most I've found is this (From wikibooks):
(x: xs) is a pattern that matches something (which gets bound to x), which is cons'd, using the function (: ), onto something else (which gets bound to the variable xs).
To me this means that it breaks the list in two, but I'm not sure...

I'm sorry if I'm asking simplistic questions, it's just a little different. Thanks.
IIRC it means x is the head (first element) of the list, and xs is the tail (a list containing the rest of the elements).
Ahh. Yes... :cry: I actually just found it else where, and was attempting to delete my post when you posted. Thanks for the reply however!