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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:14:04+00:00 2026-06-18T08:14:04+00:00

I am new to Haskell and am trying to learn the basics. I am

  • 0

I am new to Haskell and am trying to learn the basics. I am having a hard time understanding how to manipulate the contents of a list.

Assume I have the following list and I would like to create a function to subtract 1 from every element in the list, where I can simply pass x to the function, how would this be done?

Prelude>let x = 1:2:3:4:5:[]

Something like:

Prelude>subtractOne(x)
  • 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-18T08:14:05+00:00Added an answer on June 18, 2026 at 8:14 am

    (You can write 1:2:3:4:5:[] more simply as [1,2,3,4,5] or even [1..5].)

    Comprehensions

    You’d like to use list comprehensions, so here it is:

    subtractOne xs = [ x-1 | x <- xs ]
    

    Here I’m using xs to stand for the list I’m subtracting one from.

    The first thing to notice is x <- xs which you can read as “x is taken from xs“. This means we’re going to take each of the numbers in xs in turn, and each time we’ll call the number x.

    x-1 is the value we’re calculating and returning for each x.

    For more examples, here’s one that adds one to each element [x+1|x<-xs] or squares each element [x*x|x<-xs].

    More than one list

    Let’s take list comprehension a little further, to write a function that finds the squares then the cubes of the numbers we give it, so

    > squaresAndCubes [1..5]
    [1,4,9,16,25,1,8,27,64,125]
    

    We need

    squaresAndCubes xs = [x^p | p <- [2,3], x <- xs]
    

    This means we take the powers p to be 2 then 3, and for each power we take all the xs from xs, and calculate x to the power p (x^p).

    What happens if we do that the other way around?

    squaresAndCubesTogether xs = = [x^p | x <- xs, p <- [2,3]]
    

    We get

    > squaresAndCubesTogether [1..5]
    [1,1,4,8,9,27,16,64,25,125]
    

    Which takes each x and then gives you the two powers of it straight after each other.

    Conclusion – the order of the <- bits tells you the order of the output.

    Filtering

    What if we wanted to only allow some answers?

    Which numbers between 2 and 100 can be written as x^y?

    > [x^y|x<-[2..100],y<-[2..100],x^y<100]
    [4,8,16,32,64,9,27,81,16,64,25,36,49,64,81]
    

    Here we allowed all x and all y as long as x^y<100.


    Since we’re doing exactly the same to each element, I’d write this in practice using map:

    takeOne xs = map (subtract 1) xs
    

    or shorter as

    takeOne = map (subtract 1)
    

    (I have to call it subtract 1 because - 1 would be parsed as negative 1.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am completely new to Haskell. I have been trying to learn how to
New to Haskell and have a stumbling block. I'm trying to filter a list
Hello I am new at Haskell and i'm having problems trying to get this
I'm new to Haskell and STM and I'm trying to understand the basics concept.
Hi i'm new to haskell and i'm trying to implement the following and I
Still new to Haskell, I have hit a wall with the following: I am
I'm new to haskell, and so I'm trying to recreate the following C++ code
I am new to Haskell and trying to fiddle with some test cases I
I am pretty new to Haskell but I feel like I have a decent
Still quite new to Haskell.. I want to read the contents of a file,

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.