Warum funktioniet das nicht (Haskell) - where

Disclaimer: Dieser Thread wurde aus dem alten Forum importiert. Daher werden eventuell nicht alle Formatierungen richtig angezeigt. Der ursprüngliche Thread beginnt im zweiten Post dieses Threads.

Warum funktioniet das nicht (Haskell) - where

addVectors :: (Num a) => (a,a) -> (a,a) -> (a,a)
addVectors (px, py) (qx, qy) = (sumx, sumy)
    where   sumx = (px+qx)
            sumy = (py+qy)

Natürlich kann man das auch direkt reinschreiben ohne where, aber darum gehts grad nicht.


Funktioniert genau so wie dus hingeschrieben hast in
GHCi, version 6.12.1
und in
Hugs, Version September 2006
mit dem Aufruf addVectors (3,4) (3,4)

Gibt es denn eine Fehlermeldung?


Ok, nachdem ich es nochmal geschrieben habe, funktionierts jetzt :smiley:

Fehlermeldung war:

ghci> :r
[1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:28:8: parse error on input `='
Failed, modules loaded: none.

Hab nochmal undo gemacht bisses nichtmehr funktioniert. Klares Problem von ‘\t’ vs. ’ ’


Woran lags? Formatierung?


ya


Ja, die Whitespace-sensitive Syntax ist eines der ekligsten Miss-Features an Haskell. 1 Tab = 8 Spaces, dann mags auch der Compiler weils so im Standard steht.
Wenn man im Editor den Tab auf was anderes stellt (haeufig: 4), dann funktioniert visuelles Ausrichten nicht mehr und produziert schwer zu findende Fehler.

nächstes :smiley:
Warum?

-- Baut zwei Listen zusammen
myconcat :: [a] -> [b] -> [c]
myconcat xs ys = xs ++ ys

http://assets.diylol.com/hfs/0b5/cdc/89e/resized/y-u-no-meme-generator-myconcat-y-u-no-working-31620b.jpg

Prelude> :load list.hs 
[1 of 1] Compiling Main             ( list.hs, interpreted )

list.hs:8:17:
    Couldn't match expected type `c' against inferred type `a'
      `c' is a rigid type variable bound by
          the type signature for `myconcat' at list.hs:7:27
      `a' is a rigid type variable bound by
          the type signature for `myconcat' at list.hs:7:13
      Expected type: [c]
      Inferred type: [a]
    In the first argument of `(++)', namely `xs'
    In the expression: xs ++ ys

list.hs:8:23:
    Couldn't match expected type `a' against inferred type `b'
      `a' is a rigid type variable bound by
          the type signature for `myconcat' at list.hs:7:13
      `b' is a rigid type variable bound by
          the type signature for `myconcat' at list.hs:7:20
      Expected type: [c]
      Inferred type: [b]
    In the second argument of `(++)', namely `ys'
    In the expression: xs ++ ys
Failed, modules loaded: none.

Ok, ohne Typdeklaration und mit

:t myconcat

ist es

myconcat :: [a] -> [a] -> [a]

Also muss man hier gleiche Typen verwenden, da ja Listen sonst kaputt gehen, richtig?


Achte auf die Typen.

:t (++)


danke


Du musst gleiche Typen verwenden weil (++) als Typ [a] → [a] → [a] hat und damit auch deine Argumente den selben Typ brauchen.

(++) hat diesen Typ da in Haskell Listen nur Elemente vom gleichen Typ haben können.


war keine ironie dabei :>


Hab deine Antwort von 15:50 zuerst übersehen, deswegen wollte ich noch etwas genauer antworten :wink:


ok, nxt one:

Switch soll also:

switch( int ergebnisVonS, Funktion[] f) {
wenn ergebnisVonS > f.length-1 return ergebnisVonS
else return fergebnisVonS
}

Wie geht man da ran?


da fehlt noch der test auf < 0 und ein element auf welches f angewandt wird

switch :: (a -> Int) -> [(a -> a)] -> (a -> a)
switch s fs x | ((s x) < 0 || (s x) >= (length fs)) = x
              | otherwise = (fs !! (s x)) x

brrr, war ja nur der Veranschaulichung halber so geschrieben