1

I'm really new to Haskell and programming in general. I'm trying to add up the contents of a list without using the sum function from Data.List. Here's what I've got so far:

module Summ
    where
summ :: [Int] -> Int
summ xs = 
  if null xs == False
    then let y = x + (head xs)
         let xs = tail xs
    else print y

I'm pretty sure there's a lot wrong with this code, but for now the latest error is "parse error on input" for the else statement. What's wrong?

5
  • 1
    y is defined in the first branch of your expression, but used in the second half. What are you expecting to happen? Commented Jun 28, 2018 at 14:42
  • I was thinking that I could use y as sort of storage for the expressions I'm using to add up the elements of the list. I couldn't think of another way to do it... Commented Jun 28, 2018 at 14:53
  • 2
    Take a look at Learn You a Haskell, it will walk you through basics of syntax and problem solving. Commented Jun 28, 2018 at 14:56
  • I've been teaching myself with that and @haskellbook, any other good resources? Commented Jun 28, 2018 at 14:59
  • 1
    Remember that you're working in a purely functional language now. There's no such thing as "storage" or "variables" (at least, not in the usual sense). It's a huge adjustment to make, but in order to program efficiently in Haskell, you have to get used to that paradigm shift. Commented Jun 28, 2018 at 16:05

2 Answers 2

4

The syntax for a let expression is let BINDINGS in EXPRESSION, e.g.

let x = 21 in x + x

It's probably complaining about else because it was expecting to see in.

There's a form of let without in, but that only works in do blocks (or list comprehensions).

Sign up to request clarification or add additional context in comments.

7 Comments

A previous error that showed up was when I didn't have either let. What is an alternative that would still do those operations?
@VeganJoy What do you mean by "those operations"? Alternative to what?
I was referring to the expressions after let in both cases where I used it, and I don't really know a lot of functions. What else could I use there, or is in applicable here?
@VeganJoy I have no idea what you're trying to do there. You're defining y to be the sum of x and the first element of xs, but x doesn't exist. Then you're defining xs to be its own tail. I can't tell you what to use instead because I don't understand what you're trying to do or how any of this would help you with summing up a list.
Um... I don't know what I was thinking with x. I'm trying to take the first part of the list, add it to a variable, then get rid of the first element of xs and repeat until it's empty. Once it's empty I planned on having it print the variable which had been added to.
|
4

That doesn't look quite right to me. print returns IO (), which isn't Int, and the let section doesn't seem to lead up to an expression at all, and x wasn't defined. Were you attempting to write a recursive function?

summ [] = 0
summ (x:xs) = x + summ xs

The recursion isn't syntax; this simply uses two partial function definitions with pattern matching. One of them calls summ again, which is recursion, and eventually (given a finite list) that call will lead to the simpler, non-recursive function. The pattern also deconstructs the list into head and tail, so the function is equivalent to:

summ xs = if null xs
          then 0
          else head xs + summ (tail xs)

Note that both then and else branches are expressions of the same type.

Each of these definitions have type summ :: Num t => [t] -> t

2 Comments

I've seen the recursive syntax before but I don't know how it works. Can you elaborate? Edit: Also in the third line where I'm defining the types should that be a IO () instead of Int?
@VeganJoy You shouldn't have an IO anywhere in your summ function. The compiler may have suggested that, because you had a stray print call in there, but you don't want to print inside your summ function; you want to simply return the value and let main print it out or do whatever it wants to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.