o1xhack - 1 year ago 107

Python Question

I'm learning recursive function in haskell that confused with such conditon:

I got a tuple list here:

`[(0.5,'!'),(1,'*'),(1.5,'#')]`

What I want to do is input a number n and compare with fist number in each tuple of the list

so suppose n=0.1, when it compared 0.5 and find it is smaller than 0.5, it will return char '!'

suppose n=0.7, which is > 0.5 and keep comparing, find that it < 1, then it will return char '*'

and after compare the whole list and find d is still bigger than the last one, it will just return char 'n'

I've been work with such condition lots of time,but still cannot get to this, here is my code:

`find :: Double -> [(Double,Char)] -> Char`

find d [] = ' '

find d xs

| d <= Double(xs[0]) = xs[0]

| d > Double(xs[0]) = find d tail(xs)

please use recursion!

Answer Source

tuple is different from array in Haskell

```
find :: Double -> [(Double,Char)] -> Char
find d [] = ' '
find d (x:xs)
| d <= fst x = snd x
| otherwise = find d xs
```