zaig
zaig

Reputation: 421

Is it possible to write any function without explicit parameters in Haskell?

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.

Upvotes: 0

Views: 117

Answers (1)

jamshidh
jamshidh

Reputation: 12070

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

Upvotes: 2

Related Questions