Using a variable 2 times with let


I'm trying to create a custom function in my functions.tidal but I have trouble since i can't use my variable 2 times.

let stretch a b p = slow a $ striate' b (2/b) p  

Starting tidal i get this error:

 Couldn't match type ‘Int’ with ‘Double’
      Expected: Pattern Double
        Actual: Pattern Int
    • In the second argument of ‘striate'’, namely ‘(2 / b)’
      In the second argument of ‘($)’, namely ‘striate' b (2 / b) p’
      In the expression: slow a $ striate' b (2 / b) p

I think it' s kind of obvious what it means but don't know how to get around this. When the function is defined b is creates as an int since this is the type of the first argument of striate but then the second is a double and it can't match the type.

any ideas how to solve this?

Thank you

type conversion in haskell can be a bit tricky sometimes, the following should work:

let stretch a b p = slow a $ striate' b (2 / fromIntegral b) p

1 Like

Cool thanks for the tip!
It now evalutes the function but when I try to use it in tidal I get this error message:

CallStack (from HasCallStack):
  error, called at src/Sound/Tidal/Pattern.hs:206:13 in tidal-1.8.0-CA9xTyU0YqyD7lFlvpFIhd:Sound.Tidal.Pattern
Failed to send. Is the 'SuperDirt' target running? toInteger: not supported for patterns

I’m away from my computer to check this, but that error makes sense. The interpreter has already seen b and typed it as Pattern for striate’. What type do you intend to pass in as b? Is it just meant to be a Double? Or is it a pattern of Doubles?

Ah yes b is a pattern int and we need it to make it to a pattern double so we need to apply fromIntegral to the value inside of the pattern and not the pattern itself:

let stretch a b p = slow a $ striate' b (2 / (fromIntegral <$> b)) p

1 Like

Cool thanks now it's working as it should!