0

For example I have a table tab = [(1,11), (2,22), (3,33)] and two functions. The first one will take as parameter the table and an Integer and will replace the Integer with the value from the table. Let's assume the Integer will be on the table.

replace :: [(Int, Int)] -> Int -> Int
replace t i = snd $ head [x | x <- t, fst x == i]

The second function will replace all the occurrences of 1, 2, 3 from a list with the values from the table.

replaceAll :: [(Int, Int)] -> [Int] -> [Int]

First I got:

replaceAll = \tab lst -> foldl(\acc x -> acc ++ [replace tab x]) [] lst

then,

replaceAll = \tab -> foldl(\acc x -> acc ++ [replace tab x]) []

Is there a way to write this without the lambda function? EDIT: Not necessary using the fold function.

1
  • 4
    You really don't want a pointfree version of that code: replaceAll = flip foldl ([]) . flip ((.) . (++)) . flip flip ([]) . ((:) .) . replace Commented Apr 10, 2016 at 15:50

1 Answer 1

2

This works for me

replaceAll = map . replace

Although I also like removing params, I think this is a bit easier to understand if you fill them the params for this definition....

replaceAll index vals = map (replace index) vals
Sign up to request clarification or add additional context in comments.

Comments

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.