Haskell Hastles

Programming, for all ages and all languages.
Post Reply
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Haskell Hastles

Post 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.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post 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)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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.
C8H10N4O2 | #446691 | Trust the nodes.
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post 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).
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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!
C8H10N4O2 | #446691 | Trust the nodes.
Post Reply