Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:03:12+00:00 2026-06-12T08:03:12+00:00

I am struggeling with an assignment we have been given. I have written this

  • 0

I am struggeling with an assignment we have been given. I have written this code slightly based on a different guide here: Not in scope: data constructor

The problem i have is a the pipe here:

| x == "+" = (Sum y y',xs'') where

The problem seems to be related to having 3 pipes or a pipe following a “where”. If i swap the 2 last pipes.
Putting

x == "+"  = (Sum y y' (...))

before

x == "*"  = (Prod y y' (...))

causes the error to move to that code. If i comment out any one of these two code segments everything works fine, but i need both of them for the assignment we have been given.

Quick summary:

| x == "*" = (Prod y y',xs'') where
                (y,xs') = ast xs
                (y',xs'') = ast xs'

and

| x == "+" = (Sum y y',xs'') where
                (y,xs') = ast xs
                (y',xs'') = ast xs'

both work 100% alone, but when i put them together my program does not compile.

Full code:

import Data.Char

data AST = Leaf Int 
            | Sum AST AST 
            | Min AST 
            | Prod AST AST
            deriving Show

tokenize::String -> [String]
tokenize[] = []
tokenize('+':xs) = "+": tokenize xs
tokenize('-':xs) = "-": tokenize xs
tokenize('*':xs) = "*": tokenize xs
tokenize(x:xs) = if isDigit x then (takeWhile isDigit (x:xs)) : tokenize (dropWhile isDigit xs) else tokenize(xs)

ast :: [String] -> (AST,[String])
ast [] = error "Empty string"
ast (x:xs) | all isDigit x = (Leaf (read x),xs)
    | x == "-" = let (y,xs') = ast xs in (Min y,xs')
    | x == "*" = (Prod y y',xs'') where
            (y,xs') = ast xs
            (y',xs'') = ast xs'
    | x == "+" = (Sum y y',xs'') where
            (y,xs') = ast xs
            (y',xs'') = ast xs'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:03:14+00:00Added an answer on June 12, 2026 at 8:03 am

    The problem in

    ast [] = error "Empty string"
    ast (x:xs) | all isDigit x = (Leaf (read x),xs)
        | x == "-" = let (y,xs') = ast xs in (Min y,xs')
        | x == "*" = (Prod y y',xs'') where
                (y,xs') = ast xs
                (y',xs'') = ast xs'
        | x == "+" = (Sum y y',xs'') where
                (y,xs') = ast xs
                (y',xs'') = ast xs'
    

    is that you can have only one where clause per equation in the function definition. So after the where in the x == "*" alternative, the parser expects the equation for the pattern (x:xs) to be complete.

    Just remove the offending where, the where clause is scoped over all the alternatives in the equation, and both where clauses have the same contents (and indent it nicer, the where belongs on its own line according to my preferences). Since the let in the first alternative uses a binding also present in the where clause, that can also be removed:

    ast [] = error "Empty string"
    ast (x:xs) | all isDigit x = (Leaf (read x),xs)
        | x == "-" = (Min y,xs')
        | x == "*" = (Prod y y',xs'')
        | x == "+" = (Sum y y',xs'')
          where
            (y,xs') = ast xs
            (y',xs'') = ast xs'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a been set an assignment in university for which i have
I have been struggeling on how to get the valid filesize of a file
I have been struggeling writing a solution excisting out of an c++ win32console and
Hi I've been struggeling with this too many hours now. I can't see whats
i 've been struggling for days on this thing....but to no avail. I'm not
I'm not sure if this kind of question is appropriate, but its been suggested
I have been struggeling all day to read a json file from server and
I'm a student in JavaScript and we have an assignment due tonight. I've been
This isn't a school assignment or anything, but I realize it's a mostly academic
I have the following tables (this is abbreviated) tableAssignments id timestamp // updated when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.