Need help writing simple Haskell program please

  • Thread starter Thread starter chuy52506
  • Start date Start date
  • Tags Tags
    Program Writing
Click For Summary
SUMMARY

The discussion focuses on writing a Haskell program that filters and sums pairs of integers based on a specified minimum distance. The user aims to implement the function addPairs which takes an integer and a list of tuples, returning a list of sums for pairs that meet the distance requirement. The provided code snippet includes a function diffTuple to calculate the absolute difference between tuple elements and an incomplete function addDistantPairs that checks this condition. The expected output for the input addPairs 10 [(2,3),(4,15),(87,92),(23,45)] is [19, 68].

PREREQUISITES
  • Understanding of Haskell syntax and functions
  • Familiarity with tuples in Haskell
  • Knowledge of list processing in Haskell
  • Basic concepts of conditional expressions in Haskell
NEXT STEPS
  • Implement the complete addDistantPairs function to handle multiple pairs
  • Learn about Haskell list comprehensions for more concise code
  • Explore Haskell's filter function for conditional filtering of lists
  • Study Haskell's type system to ensure type safety in function definitions
USEFUL FOR

Haskell developers, programming students, and anyone interested in functional programming concepts and list manipulation in Haskell.

chuy52506
Messages
77
Reaction score
0
So for my program I want to input this line of code:

addPairs 10 [(2,3),(4,15),(87,92),(23,45)]

Where the 10 after addPairs is the minimum distance the pair (x,y) must have in order to be outputted as [x+y]
For example :
addPairs 10 [(2,3),(4,15),(87,92),(23,45)]

would output:
[ 19, 68 ]

SO far this is what I have:

diffTuple :: (Int,Int) -> Int
diffTuple (x,y) = abs (x-y)

addDistantPairs :: addDistantPairs :: Int -> [(Int,Int)] -> [ Int ]
addDistantPairs n [(x,y)]
| diffTuple(x,y)>= n = addPairs[(x,y)]
| otherwise = [0]
 
Technology news on Phys.org
anyone??
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
Replies
12
Views
2K
Replies
53
Views
5K
Replies
10
Views
2K
Replies
20
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
55
Views
7K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K