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 7800735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:44:00+00:00 2026-06-02T00:44:00+00:00

I have a problem with a Haskell code, I have the following: takeMeHere cs

  • 0

I have a problem with a Haskell code, I have the following:

takeMeHere cs m =
    |(find (==E) cs == Nothing && (checkNextStep pozxCrt+1 pozyCrt m) == True) = (Just E,[(pozxCrt+1,pozyCrt)] ++ m)
    |(find (==S) cs == Nothing && (checkNextStep pozxCrt pozyCrt-1 m) == True) = (Just S,[(pozxCrt,pozyCrt-1)] ++ m)
    |(find (==W) cs == Nothing && (checkNextStep pozxCrt-1 pozyCrt m) == True) = (Just W,[(pozxCrt-1,pozyCrt)] ++ m)
    |(find (==N) cs == Nothing && (checkNextStep pozxCrt pozyCrt+1 m) == True) = (Just N,[(pozxCrt,pozyCrt+1)] ++ m)
    |otherwise = (Nothing,m)
where
    pozxCrt=fst(head m)
    pozyCrt=snd(head m)

checkNextStep x y m = if(find (== (x,y)) m == Nothing) then True
   else False

I get a parse error on input "|" . If I write the code with something like if then else if then…it works. But I want to use the | for a more compact coding.What seems to be the problem here ?

  • 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-02T00:44:02+00:00Added an answer on June 2, 2026 at 12:44 am

    To fix parsing error, remove = from first line. The = sign is put after the guards.

    Next, you should indent “where”

    takeMeHere cs m
        |(find (==E) cs == Nothing && (checkNextStep pozxCrt+1 pozyCrt m) == True) = (Just E,[(pozxCrt+1,pozyCrt)] ++ m)
        |(find (==S) cs == Nothing && (checkNextStep pozxCrt pozyCrt-1 m) == True) = (Just S,[(pozxCrt,pozyCrt-1)] ++ m)
        |(find (==W) cs == Nothing && (checkNextStep pozxCrt-1 pozyCrt m) == True) = (Just W,[(pozxCrt-1,pozyCrt)] ++ m)
        |(find (==N) cs == Nothing && (checkNextStep pozxCrt pozyCrt+1 m) == True) = (Just N,[(pozxCrt,pozyCrt+1)] ++ m)
        |otherwise = (Nothing,m)
      where
        pozxCrt=fst(head m)
        pozyCrt=snd(head m)
    

    This will at least parse, yet it won’t compile. The (checkNextStep pozxCrt pozyCrt+1 m) should be (checkNextStep pozxCrt (pozyCrt+1) m).

    Let me add that you can fix a lot of verbose code:

    • find (==E) cs == Nothing can be changed to E `notElem` x
    • You do not need to compare with True: change x == True to x
    • if x then True else False can be changed to x
    • [x]++y can be changed to x:y
    • You can use pattern matching like this: (pozxCrt, pozyCrt) = head m or (pozxCrt, pozyCrt):_ = m

    The result is:

    takeMeHere cs m                                                                 
        | E `notElem` cs && checkNextStep (pozxCrt+1) pozyCrt m = (Just E,(pozxCrt+1,pozyCrt):m)
        | S `notElem` cs && checkNextStep pozxCrt (pozyCrt-1) m = (Just S,(pozxCrt,pozyCrt-1):m)
        | W `notElem` cs && checkNextStep (pozxCrt-1) pozyCrt m = (Just W,(pozxCrt-1,pozyCrt):m)
        | N `notElem` cs && checkNextStep pozxCrt (pozyCrt+1) m = (Just N,(pozxCrt,pozyCrt+1):m)
        | otherwise = (Nothing,m)                                                   
      where                                                                         
        (pozxCrt, pozyCrt) = head m                                                 
    
    checkNextStep x y m = (x,y) `notElem` m
    

    You have a lot of repetition in the guards. A lot of repetition is a sign to create new functions.

    move E (x, y) = (x+1, y) 
    move S (x, y) = (x, y-1)
    move N (x, y) = (x, y+1)
    move W (x, y) = (x-1, y)
    
    takeMeHere cs m
        | canGo E = go E
        | canGo S = go S
        | canGo W = go W
        | canGo N = go N
        | otherwise = (Nothing,m)
      where
        pos = head m
        canGo dir = dir `notElem` cs && checkNextStep (move dir pos) m
        go dir = (Just dir, move dir pos:m)
    
    checkNextStep (x, y) m = (x,y) `notElem` m
    

    Next step: use find canGo [E,S,W,N] to get rid of the guards:

     takeMeHere cs m =                                                               
        case find canGo [E,S,W,N] of                                                
          Just dir -> (Just dir, move dir pos:m)                                    
          Nothing -> (Nothing, m) 
        where ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following haskell code: fac n = product [1..n] taylor3s w0 f
I have been writing a growing code base in Haskell. My problem is that
I wrote a Haskell code which has to solve the following problem : we
Spoiler alert: this is related to Problem 14 from Project Euler. The following code
I have an haskell problem. putStrLn is supposed to take a [Char], or a
i have a question regarding this Haskell code: module Queue (Queue, emptyQueue, queueEmpty, enqueue,
I have some troubles with Haskell's type system. Situation: Following program is taking list
I'm Haskell newbie and I have the little problem. I'm trying to write a
I just have started to learn Haskell and combine reading books and tutorials with
I have a problem. I wrote a big Haskell program, and it always works

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.