First time using Haskell

Hello, I’m new to Haskell and am attempting to write a seemingly impossible functional program incorporating the omniscience principle, a generic convergent sequence, and a type of cantor set 2^N

Q = set.empty
 cycle take Q (iterate(set.Q))
  N:Q
  i:Q
  k:Q
  v:Q
  n:Q
y = 0
 cycle take y (iterate(+1))
z = any(y)
i = i index z
k = k indez z
v = v index z
v = v index z
n = n index z
Ninf = N index inf
N = N
X = 0
X = X + 1
l = set((v index n) SuchThat v index n elem set(empty, set()) :: n elem N)
x = Ninf == set(k elem l SuchThat all(i) elem N(k index i >= k index next i))
p = any(x) elem X(p(x) == 0) || all(x) elem X(p(x) == 1)
take f != "" (repeat)
 take 100 (repeat)
  take j (iterate(+1))
   f = ""
   a = chr j
    if p == 1
     then f++a
     else
      then f=f
prelude> f

so far I’ve read a book on Haskell, but I’m having problems, supposedly with parsing…
if anyone can fix this code have at it, but please get back to me!

thanks,
Oliver

This definitely is not Haskell. Can you pick a specific problem you are having with the language?

take f != “” (repeat)

parse error???

!= is not Haskell (/= is), take f is missing an argument (or plain wrong if f is not an Int):

λ> take 2 [3,5,2,8]
[3,5]

thanks what else
I need all I can get

when I look at the examples all the functions require lists…
how do I use a function for boolean values of its parameters as relates to other uses of said values

Perhaps looking at some examples will help you understand the syntax better and allow you to write what you mean – Starting Out - Learn You a Haskell for Great Good!

3 Likes

how do I repeat until something

There is an until :: (a -> Bool) -> (a -> a) -> a -> a function for that.

cool

its yelling at me for

if p == 1

what is it wining about

I think there’s no point in trying to brute-force figure out Haskell works. If you read beginner-level educational material you’ll have better results faster.
The problem with writing if p == 1 is that it isn’t a complete if expression. For it to be valid you must write if p == 1 then ... else ....
You can read about if more here: Starting Out - Learn You a Haskell for Great Good!

doubleSmallNumber x = if x > 100
    then x
    else x*2

From Learn You a Haskell for Great Good!:

Right here we introduced Haskell’s if statement. You’re probably familiar with if statements from other languages. The difference between Haskell’s if statement and if statements in imperative languages is that the else part is mandatory in Haskell. In imperative languages you can just skip a couple of steps if the condition isn’t satisfied but in Haskell every expression and function must return something. We could have also written that if statement in one line but I find this way more readable. Another thing about the if statement in Haskell is that it is an expression. An expression is basically a piece of code that returns a value. 5 is an expression because it returns 5, 4 + 8 is an expression, x + y is an expression because it returns the sum of x and y. Because the else is mandatory, an if statement will always return something and that’s why it’s an expression.

8 Likes

So like


   if p == 1
     then f++a
     else
      then f=f

There are a few strange things in that snippet alone:

  • that last then should be removed, it should just be if ... then ... else ...
  • f ++ a should probably be changed to f ++ [a]
  • f=f is not an expression, you can only use this notation when binding things at the top level, with let, or with where. And even there it would mean "define a new variable f which is equal to itself recursively. When trying to evaluate it you would end up in an infinite loop.

Also note that in Haskell all variables are immutable, so f ++ [a] won’t change f itself, it will just return the result as a new value.

a is a char in string f
Is there really a need for a list function?

so should I just cut it off and be like

if p == 1
then f++a

Like the else is just given at that point right?

Strings are lists of characters in Haskell, so to change a character to a string you have to put [ and ] around it.

No, that is not possible. All ifs need to have an else branch in Haskell, because it has to return something even if the condition is not met.

So what do I say if it does nothing

Haskell code never does anything, it is not imperative. Functions in Haskell only manipulate the values they get as arguments to produce an output.

2 Likes

Cool

But it’s still giving me an error on


   if p == 1
     then f++[a]
     else

Seconding what romes said, you are better off spending your time learning Haskell than just writing something you think looks like Haskell and asking other people to fix it for you. You said “so far I’ve read a book on Haskell”, and I am not sure I believe that, maybe you mean that you’ve looked over a book and think you got the gist of the language, but the amount of syntax and type errors in your code makes me believe you didn’t really spend that much time learning the language itself. Which is fine, everyone has to learn at some point, and right now seems like that point for you. There are dozens of great books and free online tutorials and videos which you should spend some real time with in order to learn the language before you try to write something like this.

7 Likes

I think you want to just return f in this case:

   if p == 1
     then f++[a]
     else f

But the code around this snippet is also wrong, so it is hard to give advice on what to write.

`We’ll let’s start simpler.


NN = []

Gets me the error


prog.hs:1:1: error: Not in scope: data constructor ‘NN’

What does this mean?

1 Like