Two similar shell functions that run different programs
Two simple bash solutions I can think of are:


Use a shell script and $0/$BASH_SOURCE to determine the calling name and a symlink from one name to the other.
Two small wrapper functions.

h() {
    local DIR=$PWD
    local TARGET="cabal.sandbox.config"
    while [[ ! -e $DIR/$TARGET -a $DIR != "/" ]]; do
        DIR=$(dirname $DIR)
    done
    if [[ $DIR != "/" ]]; then
        local DB=$(sed -ne '/^package-db: */{s///p;q;}' "$DIR/$TARGET")
        set -- "${@:1:1}" -no-user-package-db -package-db="$DB" "${@:2}"
        # or this is the order of original arguments and added arguments don't matter
        set -- "$@" -no-user-package-db -package-db="$DB"
    fi

    "$@"
}

runhaskells() {
    h runhaskell "$@"
}

ghcs() {
    h ghc "$@"
}



Slightly DRYier version of the above which uses bash's FUNCNAME array.

    h() {
        local DIR=$PWD
        local TARGET="cabal.sandbox.config"
        while [[ ! -e $DIR/$TARGET -a $DIR != "/" ]]; do
            DIR=$(dirname $DIR)
        done
        if [[ $DIR != "/" ]]; then
            local DB=$(sed -ne '/^package-db: */{s///p;q;}' "$DIR/$TARGET")
            set -- "${@:1:1}" -no-user-package-db -package-db="$DB" "${@:2}"
            # or this is the order of original arguments and added arguments don't matter
            set -- "$@" -no-user-package-db -package-db="$DB"
        fi

        "${FUNCNAME[1]%s}" "$@"
    }

    runhaskells() {
        h "$@"
    }

    ghcs() {
        h "$@"
    }